Processing math: 5%

[WEEK02-DAY2] 역전파의 전체 과정

2024. 8. 19. 12:46Machine Learning/[TIL] Naver Boost Camp

하나의 은닉층과 하나의 출력층을 가지고 있다.

입력 x는 은닉층으로 전달된다.

은닉층에서 가중치 W1과 시그모이드 활성화 함수 σ를 통해 출력 h = \sigma(W_1 \cdot x)가 계산된다.

은닉층 출력 h는 출력층에서 또 다른 가중치 W_2와 곱해져 최종 출력 \hat{y}가 계산된다.

손실함수는 L = (\hat{y}-y)^2로 정의된다.

우리는 이 손실을 최소화하기 위해 W_1의 가중치를 조정해야한다.

  1. chain-rule의 적용

    \frac{\partial \mathcal{L}}{\partial W_1} = \frac{\partial \mathcal{L}}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial h} \cdot \frac{\partial h}{\partial W_1}

  2. \frac{\partial \mathcal{L}}{\partial \hat{y}} : 손실의 예측값에 대한 편미분

    \frac{\partial \mathcal{L}}{\partial \hat{y}} = 2(\hat{y}-y)

    손실 함수 \mathcal{L}이 예측값 \hat{y}에 따라 어떻게 변하는지를 나타낸다.

  3. \frac{\partial \hat{y}}{\partial h}: 출력이 은닉층 출력 h에 미치는 영향

    \hat{y}hW_2의 곱으로 표현되므로, h에 대한 미분은 W_2가 된다.

    \frac{\partial \hat{y}}{\partial h}=W_2

  4. \frac{\partial h}{\partial W_1}: 은닉층 출력이 W_1에 미치는 영향

    은닉층에서의 출력 hh = \sigma(W_1 \cdot x)로 정의된다.

    여기서 σ는 시그모이드 함수로, 시그모이드 함수의 미분은 다음과 같다:

    \frac{\partial\sigma(z)}{\partial z} = \sigma(z)(1 - \sigma(z))

    h에 대해 미분하면:

    \frac{\partial h}{\partial z} = h(1 - h)

    z=W_1 \cdot x이다.

    \frac{\partial h}{\partial z} \cdot \frac{\partial z}{\partial W_1} = h(1 - h) \cdot x

    z=W_1 \cdot x에서 W_1은 행렬이고 x는 벡터이기 때문에, 행렬의 각 요소에 대해 미분을 할 때 벡터 x의 각 요소가 그대로 나타난다.

    따라서 W_1에 대한 h의 미분은 다음과 같다.

    \frac{\partial h}{\partial W_1} = h(1 - h) \cdot x

  1. W_1에 대한 손실의 편미분

    \frac{\partial \mathcal{L}}{\partial W_1} = 2(\hat{y} - y) \cdot W_2 \cdot h(1 - h) \cdot x