IQ Lab
← all posts
AI 2026.04.28 · 11 min read Advanced

RNN의 Vanishing Gradient는 왜 피할 수 없는가

Spectral radius가 vanishing과 exploding을 결정하는 원리부터 gradient clipping, orthogonal init, IRNN까지 — RNN이 긴 의존성을 학습하지 못하는 근본 원인을 추적한다.


RNN은 시간 방향으로 같은 행렬 WhhW_{hh}를 반복해서 곱한다. 이 반복적 행렬 곱이 gradient를 소멸시키거나 폭발시키는 원인이다. 단순히 “gradient가 작아진다”는 관찰이 아니라, spectral radius라는 정확한 조건이 그 경계를 결정한다. 왜 ρ(Whh)=1\rho(W_{hh}) = 1을 유지하는 것이 그토록 어려운가?

Jacobian 곱과 Spectral Radius

BPTT에서 시각 kk부터 tt까지의 gradient 전달은 Jacobian 곱으로 표현된다.

hthk=j=k+1tWhhdiag(σ(zj))\frac{\partial h_t}{\partial h_k} = \prod_{j=k+1}^{t} W_{hh}^\top \mathrm{diag}(\sigma'(z_j))

Pascanu et al. (2013)은 이 곱의 장기 행동이 WhhW_{hh}의 spectral radius ρ(Whh)=maxiλi\rho(W_{hh}) = \max_i |\lambda_i|에 의해 결정됨을 보였다.

정리 1 · Pascanu의 Vanishing/Exploding 조건

σmax=maxzσ(z)\sigma'_{\max} = \max_z \sigma'(z)로 정의할 때:

ρ(Whh)σmax<1    j=k+1tJj0(tk)\rho(W_{hh}) \cdot \sigma'_{\max} < 1 \implies \left\|\prod_{j=k+1}^{t} J_j\right\| \to 0 \quad (t - k \to \infty)ρ(Whh)σmax>1    j=k+1tJj\rho(W_{hh}) \cdot \sigma'_{\max} > 1 \implies \left\|\prod_{j=k+1}^{t} J_j\right\| \to \infty
▷ 증명

submultiplicative 부등식 JjJj\|\prod J_j\| \le \prod \|J_j\|로 시작한다. 각 JjWhhσmax\|J_j\| \le \|W_{hh}\| \cdot \sigma'_{\max}이므로 곱 전체는 (Whhσmax)tk(\|W_{hh}\| \cdot \sigma'_{\max})^{t-k}로 상한이 잡힌다. 대칭 행렬에서 Whh=ρ(Whh)\|W_{hh}\| = \rho(W_{hh})이고, 일반 행렬에서는 Gelfand의 공식 ρ(A)=limkAk1/k\rho(A) = \lim_{k} \|A^k\|^{1/k}로 충분히 큰 tkt-k에서 effective rate가 ρσmax\rho \cdot \sigma'_{\max}에 수렴한다. \square

T=100T = 100 스텝에서 ρ=0.9\rho = 0.9이면 gradient norm은 0.91002.7×1050.9^{100} \approx 2.7 \times 10^{-5}로 사실상 0이다. ρ=1.1\rho = 1.1이면 1.11001.4×1041.1^{100} \approx 1.4 \times 10^4로 폭발한다.

ρ = 1 유지가 어려운 이유

이상적인 조건은 ρeff=ρ(Whh)E[σ(z)]=1\rho_{\text{eff}} = \rho(W_{hh}) \cdot \mathbb{E}[\sigma'(z)] = 1이다. 그러나 실전에서 이 조건은 두 방향에서 동시에 무너진다.

Saturation의 압박. tanh(z)=1tanh2(z)\tanh'(z) = 1 - \tanh^2(z)z|z|가 커질수록 0에 가까워진다. 학습이 진행되며 weight magnitude가 증가하면 zz도 커지고, effective spectral radius는

ρeff=ρ(Whh)E[σ(zt)]ρ(Whh)\rho_{\text{eff}} = \rho(W_{hh}) \cdot \mathbb{E}[\sigma'(z_t)] \ll \rho(W_{hh})

로 추가 감쇠된다. ρ(Whh)=1\rho(W_{hh}) = 1로 초기화해도 saturation이 vanishing을 야기한다.

ReLU의 반대편 위험. ReLU는 σ(z){0,1}\sigma'(z) \in \{0, 1\}로 positive region에서 gradient를 완벽히 보존하지만, hidden state가 unbounded여서 ρ>1\rho > 1이면 ht\|h_t\|가 지수적으로 증가한다. tanh\tanh는 bounded hidden state 덕분에 exploding에 강하지만 saturation에 취약하고, ReLU는 그 반대다.

학습 중 spectral drift. Gradient update ΔWhh=ηL/Whh\Delta W_{hh} = -\eta \partial L / \partial W_{hh}ρ(Whh)\rho(W_{hh})를 perturbation theory에 따라 변화시킨다. δρu(ΔWhh)v\delta\rho \approx u^\top (\Delta W_{hh}) v (u,vu, v는 dominant eigenvector). 학습이 길어질수록 ρ\rho는 1에서 멀어지는 random walk를 수행한다.

Edge of Chaos

Dynamical system 관점에서 ρ=1\rho = 1은 ordered phase(ρ<1\rho < 1, 모든 trajectory가 attractor로 수렴)와 chaotic phase(ρ>1\rho > 1, 작은 perturbation이 지수 증폭)의 경계다. 이 경계를 학습 내내 유지하는 것은 구조적으로 불안정하다.

증상 치료: Gradient Clipping

Exploding gradient의 직접 대응은 Pascanu가 제안한 norm-based clipping이다.

ggmin ⁣(1,θg)g \leftarrow g \cdot \min\!\left(1,\, \frac{\theta}{\|g\|}\right)

element-wise clipping과의 결정적 차이는 방향 보존이다. α=min(1,θ/g)0\alpha = \min(1, \theta/\|g\|) \ge 0이므로 clipped gradient는 원래 gradient와 같은 방향을 유지한다. LL-smooth 함수에서 η<2/L\eta < 2/L이면 단조 감소가 보장된다.

# PyTorch 표준 학습 루프
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()

Pascanu는 PTB 실험에서 θ=1.0\theta = 1.0을 권장했다. Adam + clipping의 조합이 가장 안정적이다 — Adam의 second moment가 effective learning rate를 자동 조정하고, clipping이 spike만 억제한다.

단, clipping은 exploding의 증상만 치료한다. g0\|g\| \to 0인 vanishing gradient는 cap할 것이 없다.

근본 치료: Orthogonal Init과 IRNN

Orthogonal initialization (Saxe et al., 2014)은 ρ=1\rho = 1을 초기화 시점에 정확히 달성한다. QR 분해로 uniform Haar measure에서 샘플한 WhhW_{hh}WhhWhh=IW_{hh}^\top W_{hh} = I를 만족하고, 따라서 모든 singular value가 1이다.

WW=I    σi(W)=1  i    ρ(W)=1W^\top W = I \implies \sigma_i(W) = 1 \;\forall i \implies \rho(W) = 1

Linear regime에서 Wkh0=h0\|W^k h_0\| = \|h_0\|이 모든 kk에 대해 성립한다. Gelfand 공식으로는 limkWk1/k=ρ=1\lim_k \|W^k\|^{1/k} = \rho = 1, 그리고 normal matrix이므로 finite-time에서도 Wk2=1\|W^k\|_2 = 1이 정확히 유지된다. Gaussian init은 asymptotic으로 ρ1\rho \approx 1이지만 non-normal 행렬의 transient instability가 있고 finite-size variance도 크다.

IRNN (Le et al., 2015)은 다른 각도에서 같은 문제를 푼다. Whh=IW_{hh} = I, ReLU activation으로 초기화하면:

ht=ReLU(ht1+Wxhxt)h_t = \mathrm{ReLU}(h_{t-1} + W_{xh} x_t)

ht1h_{t-1}이 positive인 dimension에서 ReLU가 identity로 작동해 ht=ht1+Wxhxth_t = h_{t-1} + W_{xh} x_t, 즉 ResNet의 RNN 버전이 된다. Jacobian은 diag(1[zt>0])I\mathrm{diag}(\mathbf{1}[z_t > 0]) \cdot I로, active neuron에서 gradient가 정확히 보존된다. Le et al.은 T=300T = 300 Adding Problem에서 IRNN이 LSTM과 거의 동등한 성능을 냈음을 보였다 — LSTM 파라미터의 1/4로.

트레이드오프

위의 해법들은 각자 다른 비용을 지불한다.

트레이드오프 요약
  • Gradient clipping: 구현 한 줄, exploding만 해결, vanishing 무력.
  • Orthogonal init: ρ=1\rho = 1 정확히 보장하지만 학습 중 drift가 발생한다. Saturation이 있으면 ρeff1\rho_{\text{eff}} \ll 1로 다시 vanishing.
  • IRNN: 파라미터 효율적, 그러나 dead unit과 exploding 위험이 있어 clipping 필수. Selective memory가 없다.
  • LSTM: gating으로 spectral 분석을 우회 — ct/ct1=ft\partial c_t / \partial c_{t-1} = f_t (element-wise scalar)로 matrix product가 제거된다. 가장 robust하지만 4배 파라미터.

Pascanu의 조건은 충분조건이다. 필요조건이 아니라는 점이 중요하다 — spectral radius만으로 모든 방향의 gradient dynamics를 포착하지는 못한다. LSTM은 이 spectral 분석의 전제인 matrix product 반복을 구조적으로 제거한다. Transformer는 더 나아가 반복 자체를 attention으로 대체한다.

정리

  • ρ(Whh)σmax<1\rho(W_{hh}) \cdot \sigma'_{\max} < 1이면 gradient가 지수적으로 소멸하고, >1> 1이면 폭발한다 (Pascanu 2013).
  • tanh\tanh saturation이 ρeff\rho_{\text{eff}}를 추가로 감쇠시켜 ρ(Whh)=1\rho(W_{hh}) = 1로 초기화해도 vanishing이 발생한다.
  • Gradient clipping은 exploding의 증상 치료, orthogonal init과 IRNN은 구조적 개선, LSTM은 근본 원인(matrix product 누적) 제거다.
  • 세 접근은 상호 배타적이지 않다 — 현대 LSTM 학습 루프는 orthogonal init + forget bias = 1 + gradient clipping + Adam을 모두 결합한다.
REF
Pascanu, Mikolov, Bengio · 2013 · On the Difficulty of Training Recurrent Neural Networks · ICML