Skip to content
Lambdian

Recurrent networks and LSTMs

Networks with memory for sequences: recurrence, backpropagation through time, vanishing gradients and gates.

Animated explanation

The idea

Sentences, sensor streams and stock prices are sequences: order matters and lengths vary. A recurrent neural network reads them one element at a time, carrying a hidden state ht\mathbf{h}_t, a running summary of everything seen so far. At each step it combines the previous state with the new input using the same weights, so it handles any length.

Training unrolls the loop into a deep network, one layer per time step, and backpropagates through it (BPTT). That depth is the problem: gradients are multiplied by the recurrent weights at every step and tend to vanish or explode, so plain RNNs forget after a few dozen steps. LSTMs and GRUs add gates and an additive memory cell that lets information and gradients survive long gaps. Today, attention-based transformers have taken over most language tasks, but the ideas of state and gating remain central.

Shapes of sequence problems

  • Many-to-one: read a sequence, output one label (sentiment, anomaly detection).
  • One-to-many: generate a sequence (captioning).
  • Many-to-many, aligned: a label per step (tagging, frame classification).
  • Sequence-to-sequence: an encoder RNN summarises, a decoder RNN generates (translation), the setting where attention was invented.

Training tips

Clip the gradient norm (e.g. at 1–5) to stop explosions; use LSTM/GRU cells; truncate BPTT to a window for very long streams; initialise the forget-gate bias to around 1 so the cell remembers by default; use teacher forcing for generation.

The algorithm, step by step

  1. 1
    Initialise h0=0\mathbf{h}_0 = \mathbf{0}.
  2. 2
    For t=1..Tt = 1..T: ht=tanh⁡(Whht−1+Wxxt+b)\mathbf{h}_t = \tanh(W_h\mathbf{h}_{t-1} + W_x\mathbf{x}_t + \mathbf{b}) (or the LSTM update).
  3. 3
    Compute outputs from the relevant states (last state, or every state).
  4. 4
    Backpropagate through the unrolled steps (BPTT), clip the gradient norm, update the shared weights.

The maths

RNN cell

ht=tanh⁡(Whht−1+Wxxt+b)\mathbf{h}_t = \tanh(W_h\mathbf{h}_{t-1} + W_x\mathbf{x}_t + \mathbf{b})

LSTM gates

[i,f,o]=σ(W[ht−1;xt]+b),g=tanh⁡(Wg[ht−1;xt]+bg)[\mathbf{i}, \mathbf{f}, \mathbf{o}] = \sigma(W[\mathbf{h}_{t-1}; \mathbf{x}_t] + \mathbf{b}),\quad \mathbf{g} = \tanh(W_g[\mathbf{h}_{t-1}; \mathbf{x}_t] + \mathbf{b}_g)

LSTM state

ct=f⊙ct−1+i⊙g,ht=o⊙tanh⁡(ct)\mathbf{c}_t = \mathbf{f}\odot\mathbf{c}_{t-1} + \mathbf{i}\odot\mathbf{g},\quad \mathbf{h}_t = \mathbf{o}\odot\tanh(\mathbf{c}_t)

Gradient clipping

g←g⋅min⁡(1,τ∥g∥)\mathbf{g} \leftarrow \mathbf{g}\cdot\min\Big(1, \frac{\tau}{\lVert\mathbf{g}\rVert}\Big)

Strengths and weaknesses

Strengths

  • Handles variable-length sequences with shared weights
  • Natural for streaming (constant memory per step)
  • LSTMs capture long-range dependencies far better than plain RNNs

Weaknesses

  • Sequential computation: slow to train on long sequences
  • Vanishing/exploding gradients in plain RNNs
  • Largely superseded by transformers for language

Reach for it when…

Streaming or on-device sequence models, time series, and when understanding the path to attention and transformers.

Check your understanding

Question 1

Q1Why do plain RNNs struggle with long-range dependencies?

Question 2

Q2What does the LSTM forget gate do?

Question 3

Q3Gradient clipping mainly protects against…

Lambdian Lab

RNN and LSTM forward passes

Implement the forward computations (no training).

  • rnn_forward(X, Wh, Wx, b, h0): X has shape (T, d); return all hidden states, shape (T, h).
  • lstm_step(x, h, c, W, b): one LSTM step where W has shape (4h, h + d) stacking the [input, forget, output, candidate] blocks applied to concat([h, x]); return (h_new, c_new).
  • running_sum_weights(): choose (Wh, Wx, b) for a 1-unit RNN with identity activation so that h_T equals the sum of a 1-D sequence.
  • gradient_factor(w, steps): ∣w∣steps|w|^{steps}, the scalar BPTT factor.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.