Explain how RNNs work and where they fit today
Written by the mlprep team · Updated
Explain how RNNs work and where they still fit in the ML landscape today.
An RNN processes a sequence one token at a time, maintaining a hidden state that summarizes what it has seen so far. At each step t:
h_t = tanh(W_h · h_{t-1} + W_x · x_t + b)
The hidden state h_t is a function of the current input and the previous hidden state. This recurrence gives RNNs memory — in theory, h_t encodes the entire history of the sequence. For output at each step (sequence labeling) or at the end (classification), you pass h_t through a linear layer.
Training: backpropagation through time#
To train, you unroll the RNN across all timesteps and backpropagate through the unrolled graph. Gradients flow backward through each timestep by multiplying by W_h at each step. If you have a sequence of 100 steps, the gradient of the loss with respect to the initial hidden state has been multiplied by W_h 100 times.
This causes two failure modes:
- Vanishing gradients: if the largest eigenvalue of W_h is less than 1, gradients shrink exponentially. The network can't learn that what happened 80 steps ago matters for the current output.
- Exploding gradients: if it's greater than 1, gradients blow up. This is handled by gradient clipping, but vanishing is harder.
Where RNNs still appear#
Transformers dominate NLP, but RNNs and their variants (LSTM, GRU) still see use in:
- Low-latency streaming inference — processing audio/sensor data one sample at a time with fixed compute per step, rather than recomputing full attention over a growing context
- On-device / edge ML — smaller memory footprint than transformer KV caches
- Structured state space models (S4, Mamba) — recent work reframes sequence modeling as a linear recurrence with selective state updates, recovering RNN-like inference efficiency while matching transformer training quality
- Time series forecasting and anomaly detection, where local patterns matter more than global context
You mentioned vanishing gradients — how do LSTMs actually solve this, and what's the core mechanism that makes the difference?
The LSTM adds a second recurrent vector — the cell state c_t — that acts as a gradient highway running alongside the hidden state. The critical difference: the cell state update is additive, not multiplicative through a squashing function.
The gates#
forget gate: f_t = σ(W_f · [h_{t-1}, x_t] + b_f)
input gate: i_t = σ(W_i · [h_{t-1}, x_t] + b_i)
candidate: g_t = tanh(W_g · [h_{t-1}, x_t] + b_g)
output gate: o_t = σ(W_o · [h_{t-1}, x_t] + b_o)
Cell state update#
c_t = f_t ⊙ c_{t-1} + i_t ⊙ g_t
The forget gate f_t decides how much of the previous cell state to keep. The input gate i_t and candidate g_t decide what new information to write in. Crucially, this update is a sum, not a product through tanh.
Why this helps gradients#
When backpropagating through the cell state, the gradient of c_ with respect to the loss has to flow through ∂c_t/∂c_ = f_t — just the forget gate values, which are between 0 and 1 but not forced through tanh. If the forget gate is near 1 (the model decides to remember), the gradient passes through largely unchanged, no matter how long the sequence.
Compare this to a vanilla RNN where the gradient flows through tanh(W_h · h_ + ...) at every step — tanh has gradient at most 0.25 at its peak, and W_h multiplies again, so 100 steps in, the gradient can be astronomically small.
GRU as a simpler alternative#
The GRU (Gated Recurrent Unit) collapses cell state and hidden state into one, using just a reset gate and update gate. Fewer parameters, similar performance on most tasks. When in doubt, start with GRU; use LSTM if you need the explicit memory/forget separation.
The key takeaway unlocks after you reveal an expert answer.
Key Takeaway
RNNs model sequences with a recurrent hidden state but suffer from vanishing gradients over long sequences. LSTMs fix this with a cell state that updates additively, not multiplicatively — the forget gate acts as a gradient valve, letting relevant signals flow backward across hundreds of steps unchanged.
Follow-up Questions to Test Yourself
Interviewers often pivot here once you cover the core concept.
- How would you decide between an LSTM and a transformer for a new time-series forecasting task?
- What is gradient clipping and why is it necessary even with LSTMs?
- How do modern state space models like Mamba differ from LSTMs, and what problem are they trying to solve?
How confident do you feel explaining this?
Track your understanding or flag it to revisit before your next loop.
Related Questions
Walk me through how transformers work
What happens step-by-step inside an LLM during token generation?
How do embedding models differ from generative models architecturally and in use?
Explain attention mechanisms beyond transformers — types and trade-offs