ml_framework

fit(delta)

명징직조지훈 2023. 4. 24. 15:04

하나의 데이터에 대한 연산이 끝난 후

역전파를 통한 delta 값의 계산

 

비용 함수의 미분 함수로부터 시작,

        # 비용 함수
        if(loss == 'mse'):
          cost_result = self.cost.errer_squared_sum(data, target)

          cost_diff = self.cost.diff_error_squared_sum(data, target)

비용 함수를 계산하면서 동시에 비용 함수의 미분함수의 계산 수행, 

각 레이어에 대한 활성화 함수의 미분 함수의 연산으로 역전파 계산 수행

비용 함수 - 활성화 미분 함수 - 노드 입력값 - 활성화 미분 함수 - 노드 입력값 - 활성화 미분 함수 - ... - 노드 입력 - 활성화 미분 함수 순

# 비용 함수
        if(loss == 'mse'):
          cost_result = self.cost.errer_squared_sum(data, target)

          cost_diff = self.cost.diff_error_squared_sum(data, target)

          activation_function = self.layer[-1].get('activation')

          if(activation_function == 'sigmoid'):
            delta = cost_diff * self.activation.sigmoid_diff(self.layer[-1].get('hidden_input'))

먼저 마지막 레이어의 경우,

이후 셀에서의 반복,

계산한 delta 값의 갱신 


        # 레이어 역순 순환 마지막 층 제외
        for layer_num in range(len(self.layer) - 2, -1, -1):
          
          layer_type = self.layer[layer_num].get('layer_type')

          delta = self.layer[layer_num].get('weight') @ delta

          activation_function = self.layer[layer_num].get('activation')

          if(activation_function == 'sigmoid'):
            delta = delta * self.activation.sigmoid_diff(self.layer[layer_num].get('hidden_input'))
          
          self.layer[layer_num]['delta'] = delta

노드 출력의 변화에 대한 비용 함수의 변화량을 구하기 위해 가중치와의 연산 이후,

활성화 함수의 미분 함수와의 계산,

해당 델타값의 누적,

초기 layer 까지의 delta 값이 갱신된다.