C.W.K.
Stream
Lesson 10 of 12 · published

LSTM 과 GRU

~18 min · lstm, gru, gating

Level 0Curious
0 XP0/73 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Gating insight

Vanilla RNN 이 long sequence 에 gradient vanish — 같은 weight matrix 가 모든 timestep 통해 곱해져서. LSTM (1997) 이 별도 cell state c_t 추가, 시간 통해 거의 unchanged 흐르게 하고, learned gate 3 개 (forget, input, output) 가 뭐 잊고 뭐 더하고 뭐 read out 할지 결정.

Forget gate 가 key piece. Sigmoid-valued vector (0~1, dimension 당) 가 elementwise 로 이전 cell state 곱해. Dimension 의 forget gate 가 1 근처면, 그 memory piece 가 많은 step 살아남아. 0 근처면 즉시 wipe. 이 gate 통한 backprop 이 tanh layer 통한 backprop 보다 훨씬 친화적.

팁: 'LSTM' 을 black box label 로 본 적 있고 demystify 하고 싶으면: vanilla RNN cell + sigmoid gate 3 개 + 추가 state vector. Math 가 한 slide 에 fit. 직관은 'controllable update 로 많은 timestep 살아남는 값'.

GRU — leaner cousin

GRU (Cho et al., 2014) 가 forget 과 input gate 를 하나의 update gate 로 merge, 별도 cell state drop. Parameter 적고, 일부 task 에 약간 덜 expressive, 실전에서 LSTM 에 훨씬 가까움. Recurrence 원하고 생각하기 싫으면 좋은 default.

PyTorch 에서 한 줄 — nn.LSTM 또는 nn.GRU — 로 쓰고 서로 drop-in replacement 로 다뤄. 선택이 거의 큰 차이 안 남, 본인 팀이 이미 편한 거 골라.

2026 년에 여전히 이기는 곳

Constant per-token cost 의 streaming inference (transformer attention 이 token 당 O(n), RNN 이 O(1)). Sequential observation 소비하는 RL policy. 일부 embedded ASR pipeline. 대부분 일반 sequence work 가 transformer 로 옮김.

원칙: LSTM 과 GRU 가 2026 년에 competent veteran, central character 아냐. Legacy 코드 읽을 만큼 잘 알아두기, 새 work 엔 transformer (또는 Mamba-class state-space model) 로 reach.

Code

LSTM and GRU as drop-in sequence encoders·python
import torch
import torch.nn as nn

x = torch.randn(8, 50, 64)              # [B, T, in_dim]

lstm = nn.LSTM(input_size=64, hidden_size=128, num_layers=2,
               batch_first=True, bidirectional=False)
out_lstm, (h, c) = lstm(x)
print("LSTM out:", out_lstm.shape)       # [8, 50, 128]
print("LSTM h:",   h.shape)              # [num_layers, B, hidden]
print("LSTM c:",   c.shape)              # [num_layers, B, hidden]

gru = nn.GRU(input_size=64, hidden_size=128, num_layers=2,
             batch_first=True)
out_gru, h_gru = gru(x)
print("GRU out:", out_gru.shape)         # [8, 50, 128]
print("GRU h:  ", h_gru.shape)           # [num_layers, B, hidden]

External links

Exercise

같은 task 에 LSTM 과 GRU sequence classifier train. Validation accuracy 와 training time 비교. 매우 가까워야 — 인터넷에서 사람들이 주장하는 만큼 선택이 자주 중요하지 않음.

Progress

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

댓글 0

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

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