Braj Kishore
JourneyVaultML FoundationsAI PM GuideAgenticNewsWork With MeMentorshipPulseNow
Say hi

Hiring · Building · Investing

Have a system — or a team —that needs to scale?

info@avyxon.ai

Usually replies within a day · track record here

JourneyVaultML FoundationsAI PM GuideAgenticNewsWork With MeMentorshipPulseNow
LinkedInXYouTubeSubstackInstagramThreadsFacebookDiscordGitHubTopmate
© 2026 Braj Kishore · Bengaluru, INThink Big · Start Small · Scale Fast
ML Foundations
11Track 3 · Architectures & the walls they broke

Recurrent networks (RNN, LSTM, GRU)

Adding time: hidden state as memory, backprop through time, why gradients vanish over long sequences, and the gating that bought another few hundred timesteps.

50 min1 interactive5 sources

"The cat, which had been missing since the storm that flooded the village last winter, was ___". To fill that blank correctly you must still be holding 'cat' — singular — after nineteen intervening words. An RNN has to carry that through nineteen multiplications, and by then it usually hasn't.

The intuition

Reading a sentence through a letterbox, one word at a time, with a single notepad. You can write on the notepad after each word and you can read it before the next, but you never see the earlier words again. Everything you'll ever know about the beginning of the sentence has to survive on that notepad — and each step you rewrite it.

Unlearn this

That attention was invented for transformers. Attention arrived in 2014, in Bahdanau, Cho and Bengio's work on neural machine translation, as a fix for the fixed-length bottleneck in RNN encoder-decoders. 'Attention Is All You Need' three years later kept the attention and threw away the recurrence — the title is literally a claim about what could be deleted.

01The hidden state: memory as a recurrence

An RNN processes a sequence one element at a time, maintaining a hidden state vector that is updated at each step: hₜ = tanh(W_hh·hₜ₋₁ + W_xh·xₜ + b). The new state is a function of the previous state and the current input, so in principle it summarises everything seen so far.

Crucially, the same weight matrices are used at every timestep. That's parameter sharing across time, exactly analogous to a CNN's sharing across space, and it's what lets a single small model process sequences of any length.

That flexibility gives the classic RNN configurations: one-to-many (image → caption), many-to-one (review → sentiment), many-to-many aligned (per-token tagging), and many-to-many via encoder-decoder (translation, where input and output lengths differ).

The Unreasonable Effectiveness of Recurrent Neural NetworksAndrej Karpathy · 2015

02Backpropagation through time, and why gradients die

To train, you unroll the network across the sequence — a 50-token sentence becomes a 50-layer-deep computation graph that happens to share weights — and run ordinary backpropagation over it. That's backpropagation through time.

Now the vanishing-gradient problem from Lesson 6 arrives with full force. The gradient flowing back to timestep 1 from timestep 50 passes through roughly 50 multiplications by the same recurrent weight matrix. If its largest eigenvalue is below 1, the gradient decays geometrically and is numerically zero long before it arrives; above 1 and it explodes.

The consequence is precise and worth stating this way: the network can still read long sequences, it just cannot learn long-range dependencies, because no training signal reaches back that far. In practice, plain RNNs learn dependencies over roughly ten timesteps and nothing useful beyond.

Exploding gradients are the easy half — clip the global norm and carry on. Vanishing is structural, and it needed an architectural fix.

Try it

How far back can the gradient reach?

Words fade by how much training signal survives the trip back to them. Then switch to the LSTM cell state.

03LSTM: gates and the gradient highway

Hochreiter and Schmidhuber's 1997 LSTM adds a second state — the cell state — that runs along the top of the unit modified only by addition and element-wise multiplication by a gate, not by a full weight matrix. That additive path is a gradient highway: it lets gradients travel many timesteps with far less attenuation. It is the same idea residual connections later brought to depth.

Three gates, each a sigmoid producing values in 0–1 that multiply a vector element-wise. The forget gate decides what to drop from the cell state. The input gate decides what of the new candidate to write. The output gate decides how much of the cell state to expose as this step's hidden output.

The intuition worth carrying: the gates are learned, differentiable, soft switches. The network learns when to remember and when to forget, rather than being forced to overwrite its notepad completely at every word. That's what bought hundreds of timesteps instead of ten, and it's why LSTMs dominated sequence modelling from about 2014 to 2017.

The GRU (2014) merges the forget and input gates into a single update gate and drops the separate cell state. Fewer parameters, faster to train, and typically comparable accuracy — so GRU when data or compute is tight, LSTM when you have both and want the extra capacity.

fₜ = σ(W_f·[hₜ₋₁, xₜ] + b_f)

Forget gate: what fraction of each cell-state element survives this step.

Cₜ = fₜ ⊙ Cₜ₋₁ + iₜ ⊙ C̃ₜ

The cell state updated by multiplication and addition only — the additive path is the gradient highway.

Understanding LSTM NetworksChristopher OlahLong Short-Term MemoryHochreiter & Schmidhuber, Neural Computation · 1997Learning Phrase Representations using RNN Encoder-Decoder (GRU)Cho et al., EMNLP · 2014

04The encoder-decoder bottleneck, and the birth of attention

For translation, the standard 2014 design encoded the whole source sentence into a single fixed-length vector and let the decoder generate from it. That vector is a bottleneck: the entire meaning of a forty-word sentence, compressed into a few hundred numbers, with performance degrading sharply as sentences got longer.

Bahdanau, Cho and Bengio's fix was to let the decoder look back at all the encoder's hidden states and compute, at each output step, a weighted average of them — the weights being a learned relevance score. When generating the French word for 'bank', the decoder can weight the source words that disambiguate it. That is attention, and it was invented three years before transformers as a patch on recurrence.

It worked, and it left the sequential bottleneck untouched. You still had to process the source one token at a time, so training time still scaled with sequence length and none of it parallelised across positions.

Neural Machine Translation by Jointly Learning to Align and TranslateBahdanau, Cho & Bengio, ICLR · 2014

05The wall that ended recurrence: no parallelism

Computing hₜ requires hₜ₋₁. That dependency is unavoidable and it means a sequence of length n takes n sequential steps, no matter how many GPUs you own. Doubling the hardware does not halve the training time.

Once datasets reached internet scale, this became the binding constraint. Transformers process every position simultaneously during training — the whole sequence in one parallel operation — so more hardware translates directly into more throughput. That, more than any accuracy gain, is why the field switched. GPT-scale pretraining is simply not reachable with a recurrent architecture.

The footnote for 2026: recurrence has come back at the margins. State-space models like Mamba deliver linear-time inference with parallelisable training, targeting exactly the O(n²) weakness of attention. Knowing that recurrence-versus-attention is an active trade-off rather than settled history is a good sign in a senior conversation.

The vocabulary, defined once
Hidden state
The vector carried between timesteps — the network's running summary of everything it has seen.
BPTT
Backpropagation through time: unroll the sequence into a deep shared-weight graph and backprop over it.
Vanishing gradient over time
Repeated multiplication by the same recurrent matrix decays the signal, so long-range dependencies never get a training signal.
LSTM gates
Forget, input and output — learned soft switches over an additively-updated cell state that acts as a gradient highway.
GRU
A two-gate simplification of the LSTM with no separate cell state. Fewer parameters, usually comparable accuracy.
Encoder-decoder bottleneck
Compressing a whole input into one fixed vector. The problem attention was invented (in 2014) to solve.
Teacher forcing
Feeding the true previous token during training rather than the model's own output. Speeds training; causes exposure bias at inference.

The lab

Everything above, with the dials exposed. Break it deliberately — the failure modes are where the understanding is.

Try it

How far back can the gradient reach?

Words fade by how much training signal survives the trip back to them. Then switch to the LSTM cell state.

What they actually ask

Recurrent networks (RNN, LSTM, GRU) — the interview questions

QWhy did transformers replace RNNs?Answer it out loud first, then open this

Parallelism, primarily. An RNN's hidden state at step t depends on step t−1, so training is inherently sequential and can't be spread across hardware. A transformer computes all positions at once during training, so throughput scales with compute — which is what made internet-scale pretraining possible. Secondarily, attention connects any two positions in one hop, while an RNN has to carry information through every intervening step, so long-range dependencies are learned far better.

What they're listening for
Lead with parallelism, not accuracy. Candidates who lead with 'attention is better at long context' have half the answer.

QExplain the LSTM gates.Answer it out loud first, then open this

Three sigmoid gates over an additively-updated cell state. Forget decides what fraction of the existing cell state to keep. Input decides what part of the new candidate to write in. Output decides how much of the cell state becomes this step's hidden output. The key structural point is that the cell state is modified only by multiplication by a gate and addition — no repeated weight-matrix multiply — which is why gradients survive many timesteps.

QWhen would you still use an RNN or GRU in 2026?Answer it out loud first, then open this

Streaming or on-device inference with tight latency and memory, where a constant-size state beats a growing KV cache; short sequences where a transformer's overhead isn't repaid; and small datasets where the recurrent inductive bias helps. I'd also mention state-space models like Mamba, which revive linear-time recurrence with parallelisable training and are a live alternative for very long sequences.

Before you move on

Recall beats re-reading, every time. Answer before you flip.

Recurrent networks (RNN, LSTM, GRU) · self-test1 / 3

Why do vanilla RNNs fail on long sequences?

Go to the primary sources

Where each claim above comes from. Open them alongside the lesson rather than after it — the papers are shorter than you think, and the figures usually carry the whole argument.

Paper
  • Long Short-Term Memory(1997)Hochreiter & Schmidhuber, Neural ComputationThe original LSTM paper, written explicitly as a solution to the vanishing-gradient analysis in its own section 3.
  • Neural Machine Translation by Jointly Learning to Align and Translate(2014)Bahdanau, Cho & Bengio, ICLRAttention, three years before transformers, invented to fix the fixed-length encoder bottleneck. The alignment heatmaps in Figure 3 are the ancestor of every attention visualisation since.
  • Learning Phrase Representations using RNN Encoder-Decoder (GRU)(2014)Cho et al., EMNLPWhere the GRU and the encoder-decoder framing come from.
Read
  • Understanding LSTM NetworksChristopher OlahThe single best explanation of LSTM gates ever written. The step-by-step gate diagrams are what almost every later explainer is redrawing.
  • The Unreasonable Effectiveness of Recurrent Neural Networks(2015)Andrej KarpathyCharacter-level RNNs writing Shakespeare, LaTeX and Linux source. The best argument for why sequence models felt like magic in 2015.

External links open in a new tab. Every URL was reachable when this page was written; links rot, so treat the list as a map rather than a guarantee.

Got it? Tick it off — progress saves on this device.

← PreviousConvolutional networks (CNN)Next →Transformers & self-attention
How far back can the gradient reach?19 timesteps
Cell

Predicting the last word — how much signal reaches each earlier word

The cat, which had been missing since the storm that flooded the village last winter, was finally found ___

The blank needs cat — singular, nineteen words back. If that word has faded out, the network has no way to learn the agreement, however clearly it is written in the input.

first wordlast wordgradient magnitude (log scale) ↑

Decay factor per step

0.600

Gradient at step 1

0.0001

Effective memory

≈ 14 steps

With |w| = 0.60, the gradient reaching the first word is 1.0e-4 — for practical purposes, zero. The network reads "cat" perfectly well on the forward pass; it simply never receives a training signal telling it that "cat" mattered. That distinction, reading versus learning from, is the answer to why vanilla RNNs cap out at roughly ten steps of useful memory. Now switch to the LSTM cell state.

Each word is shaded by how much training signal survives the trip back to it from the end of the sentence. Faded words are ones the network can read but cannot learn from.

How far back can the gradient reach?19 timesteps
Cell

Predicting the last word — how much signal reaches each earlier word

The cat, which had been missing since the storm that flooded the village last winter, was finally found ___

The blank needs cat — singular, nineteen words back. If that word has faded out, the network has no way to learn the agreement, however clearly it is written in the input.

first wordlast wordgradient magnitude (log scale) ↑

Decay factor per step

0.600

Gradient at step 1

0.0001

Effective memory

≈ 14 steps

With |w| = 0.60, the gradient reaching the first word is 1.0e-4 — for practical purposes, zero. The network reads "cat" perfectly well on the forward pass; it simply never receives a training signal telling it that "cat" mattered. That distinction, reading versus learning from, is the answer to why vanilla RNNs cap out at roughly ten steps of useful memory. Now switch to the LSTM cell state.

Each word is shaded by how much training signal survives the trip back to it from the end of the sentence. Faded words are ones the network can read but cannot learn from.