Oct 6, 2025
CS231n Lecture 4 - Neural Networks and Backpropagation LECTURE 글 목록
← 이전글
다음글 →
목차 핵심 한 줄 정리 이전 강의와의 연결 Two-layer Neural Network Activation Function Dead Neuron Sigmoid와 Tanh Fully Connected Network와 MLP Network Capacity와 Regularization Computational Graph Backpropagation 자주 나오는 Gate Pattern 헷갈린 수식 / shape Two-layer Neural Network Shape Activation Function이 없을 때 Backpropagation 예제: f ( x , y , z ) = ( x + y ) z f(x, y, z) = (x+y)z f ( x , y , z ) = ( x + y ) z Sigmoid Derivative Scalar, Vector, Matrix Gradient Shape ReLU Vector Backpropagation Matrix Multiplication Backpropagation Jacobian을 직접 만들지 않는 이유 과제에서 확인할 것 핵심 한 줄 정리
Neural network는 여러 linear transformation 사이에 nonlinearity를 넣어 복잡한 함수를 표현하고, backpropagation은 chain rule을 이용해 loss가 각 parameter에 미치는 영향을 효율적으로 계산하는 방법이다.
이전 강의와의 연결
Lecture 3에서는 loss function을 정의하고, regularization과 optimization으로 좋은 weight를 찾는 방법을 봤다.
하지만 지금까지의 모델은 여전히 linear classifier라서, 선 하나로 나눌 수 없는 데이터에는 한계가 있다.
이제 Lecture 4에서는 이 한계를 넘기 위해 여러 개의 linear layer 사이에 nonlinearity를 넣는 neural network를 본다.
Two-layer Neural Network
기본 형태:
h = max ( 0 , W 1 x ) h = \max(0, W_1x) h = max ( 0 , W 1 x )
s = W 2 h s = W_2h s = W 2 h
한 줄로 쓰면:
s = W 2 max ( 0 , W 1 x ) s = W_2 \max(0, W_1x) s = W 2 max ( 0 , W 1 x )
x x x : input vector
W 1 W_1 W 1 : input -> hidden layer weight
h h h : hidden layer activation
W 2 W_2 W 2 : hidden -> output layer weight
s s s : output score
max ( 0 , ⋅ ) \max(0, \cdot) max ( 0 , ⋅ ) : ReLU activation
Bias 포함:
h = max ( 0 , W 1 x + b 1 ) h = \max(0, W_1x + b_1) h = max ( 0 , W 1 x + b 1 )
s = W 2 h + b 2 s = W_2h + b_2 s = W 2 h + b 2
Activation Function
Activation function의 역할:
Linear transformation 사이에 nonlinearity를 추가한다.
복잡한 decision boundary를 만들 수 있게 한다.
Neural network가 nonlinear problem을 풀 수 있게 한다.
ReLU:
ReLU ( x ) = max ( 0 , x ) \text{ReLU}(x) = \max(0, x) ReLU ( x ) = max ( 0 , x )
Piecewise form:
ReLU ( x ) = { x if x > 0 0 if x ≤ 0 \text{ReLU}(x) =
\begin{cases}
x & \text{if } x > 0 \\
0 & \text{if } x \le 0
\end{cases} ReLU ( x ) = { x 0 if x > 0 if x ≤ 0
ReLU gradient:
d d x ReLU ( x ) = { 1 if x > 0 0 if x < 0 \frac{d}{dx}\text{ReLU}(x) =
\begin{cases}
1 & \text{if } x > 0 \\
0 & \text{if } x < 0
\end{cases} d x d ReLU ( x ) = { 1 0 if x > 0 if x < 0
주의
x = 0 x = 0 x = 0 에서는 미분 불가능하지만, 구현에서는 보통 0 또는 1 중 하나로 처리한다.
Dead Neuron
ReLU의 문제:
입력이 계속 음수이면 output이 계속 0이다.
Gradient도 0이므로 weight update가 잘 안 된다.
이 상태를 dead neuron이라고 한다.
Leaky ReLU:
LeakyReLU ( x ) = { x if x > 0 α x if x ≤ 0 \text{LeakyReLU}(x) =
\begin{cases}
x & \text{if } x > 0 \\
\alpha x & \text{if } x \le 0
\end{cases} LeakyReLU ( x ) = { x α x if x > 0 if x ≤ 0
α \alpha α : 작은 양수
음수 영역에서도 gradient가 완전히 0이 되지 않는다.
Sigmoid와 Tanh
Sigmoid:
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1 + e^{-x}} σ ( x ) = 1 + e − x 1
output range: 0 0 0 부터 1 1 1 사이이다.
Tanh:
tanh ( x ) = e x − e − x e x + e − x \tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} tanh ( x ) = e x + e − x e x − e − x
output range: − 1 -1 − 1 부터 1 1 1 사이이다.
주의:
값을 좁은 범위로 squash한다.
입력 절댓값이 커지면 gradient가 작아진다.
Vanishing gradient 문제가 생길 수 있다.
Hidden layer에서는 보통 ReLU 계열을 많이 쓴다.
Fully Connected Network와 MLP
MLP 구조:
h 1 = ϕ ( W 1 x + b 1 ) h_1 = \phi(W_1x + b_1) h 1 = ϕ ( W 1 x + b 1 )
h 2 = ϕ ( W 2 h 1 + b 2 ) h_2 = \phi(W_2h_1 + b_2) h 2 = ϕ ( W 2 h 1 + b 2 )
s = W 3 h 2 + b 3 s = W_3h_2 + b_3 s = W 3 h 2 + b 3
ϕ \phi ϕ : activation function
h 1 h_1 h 1 , h 2 h_2 h 2 : hidden layer activation
s s s : output score
핵심:
Fully connected network는 이전 layer의 모든 neuron이 다음 layer의 모든 neuron과 연결된 구조이다.
Layer가 많아지면 표현력은 커진다.
하지만 학습 난이도와 overfitting 위험도 커진다.
Network Capacity와 Regularization
Hidden neuron 수가 많아지면:
model capacity가 커진다.
더 복잡한 function을 표현할 수 있다.
decision boundary가 복잡해질 수 있다.
overfitting 위험이 커진다.
강의 핵심:
보통 network size 자체를 주된 regularizer로 쓰지는 않는다.
어느 정도 큰 network를 두고 regularization strength를 조절하는 경우가 많다.
Computational Graph
Computational graph:
node: 연산
edge: 값이 흐르는 방향
input에서 loss까지 계산 과정을 graph로 표현한다.
예시:
s = f ( x , W ) s = f(x, W) s = f ( x , W )
L = L data ( s , y ) + R ( W ) L = L_{\text{data}}(s, y) + R(W) L = L data ( s , y ) + R ( W )
장점:
복잡한 함수를 작은 연산 단위로 나눌 수 있다.
각 node의 local gradient만 계산하면 된다.
Chain rule로 전체 gradient를 구할 수 있다.
Backpropagation
Forward pass:
input -> output 방향으로 값을 계산한다.
Backward pass:
loss -> input 방향으로 gradient를 전파한다.
용어:
upstream gradient:
뒤쪽 node에서 현재 node로 들어오는 gradient이다.
local gradient:
현재 node의 output을 input에 대해 미분한 값이다.
downstream gradient:
현재 node가 앞쪽 node로 전달하는 gradient이다.
핵심 식:
downstream gradient = upstream gradient × local gradient \text{downstream gradient}
=
\text{upstream gradient}
\times
\text{local gradient} downstream gradient = upstream gradient × local gradient
직관
뒤에서 온 gradient에 현재 node의 local derivative를 곱해서 앞쪽으로 넘긴다.
자주 나오는 Gate Pattern
Add gate:
z = x + y z = x + y z = x + y
∂ z ∂ x = 1 \frac{\partial z}{\partial x} = 1 ∂ x ∂ z = 1
∂ z ∂ y = 1 \frac{\partial z}{\partial y} = 1 ∂ y ∂ z = 1
Upstream gradient가 양쪽 input으로 그대로 전달된다.
Multiply gate:
z = x y z = xy z = x y
∂ z ∂ x = y \frac{\partial z}{\partial x} = y ∂ x ∂ z = y
∂ z ∂ y = x \frac{\partial z}{\partial y} = x ∂ y ∂ z = x
한쪽 gradient를 구할 때 반대쪽 값이 곱해진다.
Swap처럼 생각하면 된다.
Copy gate:
하나의 값이 여러 경로로 사용되는 경우이다.
Backward pass에서는 여러 경로에서 온 gradient를 더한다.
∂ L ∂ x = ∂ L 1 ∂ x + ∂ L 2 ∂ x \frac{\partial L}{\partial x}
=
\frac{\partial L_1}{\partial x}
+
\frac{\partial L_2}{\partial x} ∂ x ∂ L = ∂ x ∂ L 1 + ∂ x ∂ L 2
Max gate:
z = max ( x , y ) z = \max(x, y) z = max ( x , y )
Forward pass에서 선택된 쪽으로만 gradient가 흐른다.
선택되지 않은 쪽 gradient는 0이다.
헷갈린 수식 / shape
Two-layer Neural Network Shape
입력 차원 D D D , hidden neuron 수 H H H , class 수 C C C 라고 하면:
x ∈ R D x \in \mathbb{R}^{D} x ∈ R D
W 1 ∈ R H × D W_1 \in \mathbb{R}^{H \times D} W 1 ∈ R H × D
h ∈ R H h \in \mathbb{R}^{H} h ∈ R H
W 2 ∈ R C × H W_2 \in \mathbb{R}^{C \times H} W 2 ∈ R C × H
s ∈ R C s \in \mathbb{R}^{C} s ∈ R C
흐름:
x x x : D D D 차원 input
W 1 x W_1x W 1 x : H H H 차원 hidden representation
W 2 h W_2h W 2 h : C C C 차원 class score
체크
Matrix multiplication이 가능한지 항상 shape으로 확인해야 한다.
Activation Function이 없을 때
Activation function이 없으면:
s = W 2 W 1 x s = W_2W_1x s = W 2 W 1 x
합치면:
W 3 = W 2 W 1 W_3 = W_2W_1 W 3 = W 2 W 1
결국:
s = W 3 x s = W_3x s = W 3 x
핵심
Nonlinearity가 없으면 layer를 여러 개 쌓아도 linear model이다.
Backpropagation 예제: f ( x , y , z ) = ( x + y ) z f(x, y, z) = (x+y)z f ( x , y , z ) = ( x + y ) z
함수:
f ( x , y , z ) = ( x + y ) z f(x, y, z) = (x+y)z f ( x , y , z ) = ( x + y ) z
중간 변수:
q = x + y q = x + y q = x + y
f = q z f = qz f = q z
값:
x = − 2 , y = 5 , z = − 4 x = -2,\quad y = 5,\quad z = -4 x = − 2 , y = 5 , z = − 4
Forward pass:
q = x + y = − 2 + 5 = 3 q = x + y = -2 + 5 = 3 q = x + y = − 2 + 5 = 3
f = q z = 3 ⋅ ( − 4 ) = − 12 f = qz = 3 \cdot (-4) = -12 f = q z = 3 ⋅ ( − 4 ) = − 12
Local gradient:
∂ q ∂ x = 1 \frac{\partial q}{\partial x} = 1 ∂ x ∂ q = 1
∂ q ∂ y = 1 \frac{\partial q}{\partial y} = 1 ∂ y ∂ q = 1
∂ f ∂ q = z \frac{\partial f}{\partial q} = z ∂ q ∂ f = z
∂ f ∂ z = q \frac{\partial f}{\partial z} = q ∂ z ∂ f = q
값 대입:
∂ f ∂ q = − 4 \frac{\partial f}{\partial q} = -4 ∂ q ∂ f = − 4
∂ f ∂ z = 3 \frac{\partial f}{\partial z} = 3 ∂ z ∂ f = 3
Chain rule:
∂ f ∂ x = ∂ f ∂ q ∂ q ∂ x = ( − 4 ) ( 1 ) = − 4 \frac{\partial f}{\partial x}
=
\frac{\partial f}{\partial q}
\frac{\partial q}{\partial x}
=
(-4)(1)
=
-4 ∂ x ∂ f = ∂ q ∂ f ∂ x ∂ q = ( − 4 ) ( 1 ) = − 4
∂ f ∂ y = ∂ f ∂ q ∂ q ∂ y = ( − 4 ) ( 1 ) = − 4 \frac{\partial f}{\partial y}
=
\frac{\partial f}{\partial q}
\frac{\partial q}{\partial y}
=
(-4)(1)
=
-4 ∂ y ∂ f = ∂ q ∂ f ∂ y ∂ q = ( − 4 ) ( 1 ) = − 4
최종:
∂ f ∂ x = − 4 \frac{\partial f}{\partial x} = -4 ∂ x ∂ f = − 4
∂ f ∂ y = − 4 \frac{\partial f}{\partial y} = -4 ∂ y ∂ f = − 4
∂ f ∂ z = 3 \frac{\partial f}{\partial z} = 3 ∂ z ∂ f = 3
헷갈린 점
x x x , y y y 는 f f f 에 직접 연결된 것이 아니라 q q q 를 거쳐 연결된다. 그래서 chain rule이 필요하다.
Sigmoid Derivative
함수:
f ( w , x ) = 1 1 + e − ( w 0 x 0 + w 1 x 1 + w 2 ) f(w, x) = \frac{1}{1 + e^{-(w_0x_0 + w_1x_1 + w_2)}} f ( w , x ) = 1 + e − ( w 0 x 0 + w 1 x 1 + w 2 ) 1
Linear part:
a = w 0 x 0 + w 1 x 1 + w 2 a = w_0x_0 + w_1x_1 + w_2 a = w 0 x 0 + w 1 x 1 + w 2
Sigmoid 적용:
f = σ ( a ) f = \sigma(a) f = σ ( a )
Sigmoid:
σ ( a ) = 1 1 + e − a \sigma(a) = \frac{1}{1 + e^{-a}} σ ( a ) = 1 + e − a 1
Derivative:
d σ ( a ) d a = σ ( a ) ( 1 − σ ( a ) ) \frac{d\sigma(a)}{da}
=
\sigma(a)(1-\sigma(a)) d a d σ ( a ) = σ ( a ) ( 1 − σ ( a ))
핵심
Forward pass에서 σ ( a ) \sigma(a) σ ( a ) 를 저장해두면 backward pass에서 derivative를 바로 계산할 수 있다.
Scalar, Vector, Matrix Gradient Shape
Scalar to scalar:
d y d x \frac{dy}{dx} d x d y
Vector to scalar:
x ∈ R N x \in \mathbb{R}^{N} x ∈ R N
L ∈ R L \in \mathbb{R} L ∈ R
∂ L ∂ x ∈ R N \frac{\partial L}{\partial x} \in \mathbb{R}^{N} ∂ x ∂ L ∈ R N
gradient는 x x x 와 같은 shape이다.
Vector to vector:
x ∈ R N x \in \mathbb{R}^{N} x ∈ R N
y ∈ R M y \in \mathbb{R}^{M} y ∈ R M
Jacobian:
∂ y ∂ x ∈ R M × N \frac{\partial y}{\partial x}
\in
\mathbb{R}^{M \times N} ∂ x ∂ y ∈ R M × N
원소별 의미:
J i j = ∂ y i ∂ x j J_{ij}
=
\frac{\partial y_i}{\partial x_j} J ij = ∂ x j ∂ y i
핵심
Loss L L L 은 scalar이므로, 어떤 variable에 대한 gradient는 그 variable과 같은 shape을 가진다.
ReLU Vector Backpropagation
ReLU는 element-wise operation이다.
y = max ( 0 , x ) y = \max(0, x) y = max ( 0 , x )
원소별:
y i = max ( 0 , x i ) y_i = \max(0, x_i) y i = max ( 0 , x i )
Jacobian은 diagonal matrix이다.
∂ y i ∂ x j = 0 if i ≠ j \frac{\partial y_i}{\partial x_j}
=
0
\quad
\text{if } i \ne j ∂ x j ∂ y i = 0 if i = j
대각 원소:
∂ y i ∂ x i = { 1 if x i > 0 0 if x i ≤ 0 \frac{\partial y_i}{\partial x_i}
=
\begin{cases}
1 & \text{if } x_i > 0 \\
0 & \text{if } x_i \le 0
\end{cases} ∂ x i ∂ y i = { 1 0 if x i > 0 if x i ≤ 0
ReLU backward:
d x i = { d y i if x i > 0 0 if x i ≤ 0 dx_i =
\begin{cases}
dy_i & \text{if } x_i > 0 \\
0 & \text{if } x_i \le 0
\end{cases} d x i = { d y i 0 if x i > 0 if x i ≤ 0
d y i dy_i d y i : upstream gradient
d x i dx_i d x i : downstream gradient
주의
실제 구현에서는 큰 Jacobian을 만들지 않고, x i > 0 x_i > 0 x i > 0 인 위치에만 gradient를 통과시킨다.
Matrix Multiplication Backpropagation
Forward:
Y = X W Y = XW Y = X W
Shape:
X ∈ R N × D X \in \mathbb{R}^{N \times D} X ∈ R N × D
W ∈ R D × M W \in \mathbb{R}^{D \times M} W ∈ R D × M
Y ∈ R N × M Y \in \mathbb{R}^{N \times M} Y ∈ R N × M
원소별:
Y n , m = ∑ d = 1 D X n , d W d , m Y_{n,m}
=
\sum_{d=1}^{D} X_{n,d}W_{d,m} Y n , m = d = 1 ∑ D X n , d W d , m
Upstream gradient:
d Y = ∂ L ∂ Y dY = \frac{\partial L}{\partial Y} d Y = ∂ Y ∂ L
Backward rule:
d X = d Y W T dX = dY W^T d X = d Y W T
d W = X T d Y dW = X^T dY d W = X T d Y
Shape 확인:
d Y ∈ R N × M dY \in \mathbb{R}^{N \times M} d Y ∈ R N × M
W T ∈ R M × D W^T \in \mathbb{R}^{M \times D} W T ∈ R M × D
d X ∈ R N × D dX \in \mathbb{R}^{N \times D} d X ∈ R N × D
따라서 d X dX d X 는 X X X 와 같은 shape이다.
또한:
X T ∈ R D × N X^T \in \mathbb{R}^{D \times N} X T ∈ R D × N
d W ∈ R D × M dW \in \mathbb{R}^{D \times M} d W ∈ R D × M
따라서 d W dW d W 는 W W W 와 같은 shape이다.
핵심
Matrix multiplication의 backward는 반드시 shape으로 검산해야 한다.
Jacobian을 직접 만들지 않는 이유
Matrix multiplication을 Jacobian으로 직접 표현하면 너무 크다.
예시:
Mini-batch size: 64
Feature dimension: 4096
Jacobian을 직접 저장하거나 곱하는 것은 비효율적이다.
대신 operation별 backward rule을 사용한다.
d X = d Y W T dX = dY W^T d X = d Y W T
d W = X T d Y dW = X^T dY d W = X T d Y
정리
Deep learning framework는 거대한 Jacobian을 직접 만들지 않는다. 연산별로 효율적인 backward function을 쓴다.
과제에서 확인할 것
놓치지 말 것은 lecture 2 reading assignment / slides에 포함된 hinge loss, SVM loss 예시이다.
Softmax loss만 보는 것이 아니라, hinge loss가 score를 probability로 바꾸지 않는다는 점을 같이 확인한다.
L i = ∑ j ≠ y i max ( 0 , s j − s y i + Δ ) L_i = \sum_{j \ne y_i} \max(0, s_j - s_{y_i} + \Delta) L i = j = y i ∑ max ( 0 , s j − s y i + Δ )
이번 강의에서 구현과 직접 연결되는 부분은 two-layer neural network의 forward pass와 analytical gradient 계산이다.
특히 강의에서 강조한 흐름은 다음이다.
forward pass then backward pass \text{forward pass}
\quad
\text{then}
\quad
\text{backward pass} forward pass then backward pass
과제에서 따로 체크할 만한 강의 언급은 다음 정도이다.
hinge loss / SVM loss는 lecture 2 reading assignment에 예시가 있음
optimizer 세부 내용은 lecture 3 참고
two-layer neural network 구현은 dimension 설정, forward pass, loss 계산, analytical gradient 계산 흐름을 확인
이전글 CS231n Lecture 3 - Regularization and Optimization Oct 4, 2025 다음글 CS231n Lecture 5 - Image Classification with CNNs Oct 7, 2025