DEEP LEARNING / 5. RNNS & LSTMS
RNNs & LSTMs
Sequential memory — before transformers took over
EXPLANATION
RNNs process sequences by maintaining a hidden state that gets updated at each timestep. The same weights are applied at every step — weight sharing across time. Problem: vanilla RNNs suffer from vanishing gradients over long sequences. Gradients shrink exponentially as they backpropagate through time (BPTT), making it impossible to learn long-range dependencies. LSTM (Long Short-Term Memory) fixes this with a cell state — a highway for gradients that runs through the sequence with only multiplicative interactions (gates). Three gates: • Forget gate → what to erase from cell state • Input gate → what new info to write • Output gate → what to expose as hidden state In practice today: LSTMs are still used for time series and streaming tasks. For NLP, transformers replaced them entirely.
DATA FLOW
RNN:
h0 → [RNN cell] → h1 → [RNN cell] → h2 → [RNN cell] → h3
↑ x1 ↑ x2 ↑ x3
Same W applied every step. Gradient vanishes over long sequences.
LSTM cell:
c(t-1) ──────────────────────────→ c(t)
↑forget ↑input ↑tanh
h(t-1), x(t) → [forget gate][input gate][output gate]
↓
h(t)CODE