C.W.K.
Stream
Lesson 03 of 05 · published

역전파: bookkeeping 하는 chain rule

~12 min · backprop, chain-rule, gradients

Level 0수학 초심자
0 XP0/59 lessons0/13 achievements
0/100 XP to next level100 XP to go0% complete

Backprop 이 답하는 질문

Forward pass 했어. Loss 얻음. 이제 알아야 함: "이 망의 각 weight — 수십억 개일 수 — 살짝 까딱이면 loss 어떻게 변해?" 그 숫자들이 gradient. 있으면 gradient descent 가 모든 weight 갱신해 loss 줄임.

Naive 하게는 한 번에 하나씩 weight perturbation 으로 각 gradient 따로 계산. 그건 weight 에 추가 forward pass. 수십억 weight 면 영원.

Backprop 의 트릭

역전파 가 사실상 한 번의 추가 망 통과 — 거꾸로 — 로 모든 gradient 계산. 뒤의 수학 = chain rule (Calculus 트랙). 엔지니어링 = forward pass 중 중간값 캐시, 거꾸로 걸으며 국소 Jacobian 곱.

Layer 의 각 weight 에 대해:

도함수의 chain. Backprop 이 각각 한 번 계산, 모든 weight 가로질러 재사용.

책임-게임 비유

Backprop 을 책임 분산 으로 생각. Loss 가 "이만큼 빗나감." Backprop 이 망 가로질러 거꾸로, 각 layer 한테 묻기: "miss 에 얼마나 기여?" 각 layer 가 weight × 위에서 흐르는 gradient 비례로 답. 그러고 (chain) 책임을 뒤 layer 로 전달.

끝까지 가면 모든 weight 가 자기 오차 share 알아. 각 weight 를 share 반대로 조정 = 다음 forward pass 가 과녁에 더 가까이.

Backprop = chain rule + 동적 프로그래밍. 중간 도함수 재사용해 한 backward pass 로 모든 파라미터 gradient 계산.

Code

한 backward 호출, 모든 gradient·python
import torch

# Backprop 작동 보려고 수동 2-layer 망
x = torch.randn(1, 4)
W1 = torch.randn(4, 8, requires_grad=True)
W2 = torch.randn(8, 1, requires_grad=True)

# Forward
h = torch.relu(x @ W1)
y_hat = h @ W2
loss = (y_hat ** 2).sum()        # 데모용 임의 loss

# Backward — chain rule, 자동
loss.backward()
print(W1.grad.shape)             # torch.Size([4, 8])
print(W2.grad.shape)             # torch.Size([8, 1])
# 두 gradient 가 한 backward 호출로.

External links

Exercise

단순 표현 y = (x*w1 + b1)**2, x=2, w1=3, b1=1. Chain rule 로 dy/dw1 손계산, PyTorch autograd 로 검증.
Hint
Chain rule 로: dy/dw1 = 2(x*w1 + b1) * x = 2*7*2 = 28 (주어진 값). Autograd 가 정확히 동의.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.