Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.A memory with valves
A plain RNN rewrites its whole memory at every step, so old information fades. An LSTM keeps a separate memory, the cell, controlled by three valves (gates):
- the forget gate decides how much of the old memory to keep;
- the input gate decides how much new information to add;
- the output gate decides how much of the memory to show.
If the forget gate stays open, a fact written at the start can survive hundreds of steps. The demo compares how long a written value survives in a plain RNN, an LSTM and a GRU.
LSTMs powered speech recognition, machine translation and handwriting recognition for a decade, and are still common for sensor time series, anomaly detection and control.
- 1. The LSTM's long-term memory is the…
- 2. To keep old memory the forget gate should be…
- 3. The input gate controls…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Equations: with z = [xₜ, hₜ₋₁],
- i = σ(zWᵢ + bᵢ), f = σ(zW_f + b_f), o = σ(zWₒ + bₒ), g = tanh(zW_g + b_g);
- cₜ = f ⊙ cₜ₋₁ + i ⊙ g;
- hₜ = o ⊙ tanh(cₜ).
- Example: c = 2, f = 0.9, i = 0.5, g = −1 gives c′ = 1.8 − 0.5 = 1.3.
- Parameters: 4(n(d + n) + n), four times a vanilla RNN. For d = 10 and n = 32: 5504. PyTorch has two bias vectors, giving 5632.
- Gradient along the cell: ∂cₜ/∂cₜ₋₁ = f directly, a gate value rather than a product with a weight matrix and a tanh slope.
- If f ≈ 1 the gradient passes unchanged: the “constant error carousel”.
- Forget bias:
- at initialisation f ≈ σ(b_f);
- b_f = 0 gives f ≈ 0.5, so after 10 steps 0.5¹⁰ ≈ 0.001 remains;
- b_f = 3 gives f ≈ 0.95, so after 50 steps 0.088 remains;
- keeping half the memory over 100 steps needs b_f ≈ 4.97.
- 1. New cell for c = 2, f = 0.9, i = 0.5, g = −1:
- 2. LSTM parameters relative to a vanilla RNN layer:
- 3. Retention after 50 steps with forget bias 3:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.LSTMs in code
Exercise 1.
- With the forget gate open and the input gate closed, an LSTM step kept the cell exactly.
- With them reversed, it overwrote the cell with tanh(0.7).
- The parameter count was 5504 for d = 10 and n = 32.
Practice.
nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)returns outputs and (h, c). Gate order in the weights is input, forget, candidate, output.- Set the forget-gate bias to 1 or more. Many libraries’ defaults do not, PyTorch included, but Keras’s
unit_forget_bias=Truedoes. - Use dropout between layers, and on recurrent connections only in the variational form (the same mask at every step).
- cuDNN kernels make LSTMs fast on GPUs; custom variants lose this.
lstm = nn.LSTM(input_size=1, hidden_size=64, batch_first=True)
for name, p in lstm.named_parameters():
if "bias" in name:
p.data[64:128].fill_(1.5) # forget-gate slice of each bias vector
out, (h, c) = lstm(x)- 1. PyTorch's LSTM gate order:
- 2. Keras option for forget bias 1:
- 3. Recurrent dropout should use…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why the cell helps.
- In a vanilla RNN, ∂hₜ/∂hₜ₋₁ = diag(1 − h²)Wₕ, so every step multiplies by a matrix and a slope.
- Along the LSTM cell the direct path gives ∂cₜ/∂cₜ₋₁ = diag(f), plus terms through the gates. Long-range gradients are products of forget-gate values, which the network controls and can hold near 1.
- Hochreiter and Schmidhuber’s original LSTM had no forget gate (f = 1), a pure constant error carousel. Gers et al. added the forget gate so cells could reset.
What the gates buy.
- The input gate protects memory from irrelevant inputs.
- The output gate protects downstream units from memory not currently needed.
- Greff et al. (2017) compared eight variants on three tasks: the forget gate and the output activation mattered most, and no variant clearly beat the standard LSTM.
Initialisation matters. With b_f = 0 the forget gate starts near 0.5, which halves memory and gradient every step, as badly as a vanilla RNN. A positive bias starts the network in remembering mode (Gers et al., 2000; Jozefowicz et al., 2015).
- 1. Along the cell, ∂c_t/∂c(t−1) directly equals…
- 2. The original 1997 LSTM had…
- 3. Greff et al. found most important:
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Does the forget-gate bias matter?
History.
- S. Hochreiter and J. Schmidhuber introduced LSTM (Neural Computation, 1997).
- F. Gers, J. Schmidhuber and F. Cummins added the forget gate (2000).
- A. Graves applied LSTMs to handwriting and speech, with CTC (2006), and H. Sak et al. at Google deployed LSTM acoustic models (2014–15).
- I. Sutskever, O. Vinyals and Q. Le used LSTMs for sequence to sequence translation (2014).
- R. Jozefowicz, W. Zaremba and I. Sutskever (2015) recommended forget bias 1.
Exercise 2. Same memory task as E11-1: remember a ±1 bit through lag steps of unit noise.
- (a) At initialisation (16 units), the loss gradient with respect to the first input, relative to the last input.
- (b) An 8-unit LSTM trained at lag 30 for 200 Adam steps, 3 seeds.
| lag | forget bias 0 | forget bias 1 | forget bias 3 |
|---|---|---|---|
| 10 | 2.9 × 10⁻³ | 5.3 × 10⁻² | 0.48 |
| 20 | 1.8 × 10⁻⁴ | 2.8 × 10⁻² | 1.2 |
| 50 | 2.5 × 10⁻⁸ | 9.3 × 10⁻³ | 3.3 |
| 100 | 9.7 × 10⁻¹⁵ | 1.1 × 10⁻³ | 0.87 |
Trained at lag 30: forget bias 0 gave accuracy 0.537, 0.528 and 0.505 (chance); forget bias 3 gave 0.844, 0.990 and 1.000.
- An LSTM is not automatically long-memoried. With forget bias 0 its gradient vanishes about as fast as the vanilla RNN’s in E11-1 (2.5 × 10⁻⁸ at lag 50 against 3.7 × 10⁻⁸).
- A positive forget bias opens the carousel. At bias 3 the first input’s gradient stays of the same order as the last input’s even at lag 100.
- Training follows the gradient. Lag 30, which the vanilla RNN could not learn, is learned with bias 3 and not with bias 0.
- Bias 1, the common default advice, helps but still decays by 10⁻³ over 100 steps.
LSTMs in AI, robotics and aerospace
- Speech and handwriting: LSTM–CTC models were the standard before transformers.
- Prognostics: LSTMs predict remaining useful life of turbofan engines from sensor histories (NASA’s C-MAPSS benchmark).
- Control and robotics: LSTM policies give agents memory under partial observability, as in OpenAI’s dexterous hand (2018).
References
- S. Hochreiter, J. Schmidhuber (1997). “Long short-term memory.” Neural Computation 9(8).
- F. A. Gers, J. Schmidhuber, F. Cummins (2000). “Learning to forget: continual prediction with LSTM.” Neural Computation 12(10).
- K. Greff, R. K. Srivastava, J. Koutník, B. R. Steunebrink, J. Schmidhuber (2017). “LSTM: a search space odyssey.” IEEE TNNLS 28(10).
- R. Jozefowicz, W. Zaremba, I. Sutskever (2015). “An empirical exploration of recurrent network architectures.” Proc. ICML.