Networks with memory:
recurrence, gates, and attention

Session 23 · Mon Nov 23 · Book section 4.4

🔁

Today’s question

A forecast reads the past to write the future.

How does a network carry information across 90 days — and does it beat “tomorrow equals today”?

Reading: 4.4 Sequence models · HW-DL assigned today

This lecture in the literature

📖 The gated memory cell that let gradients survive long sequences — 1997, still everywhere.

Hochreiter, S., & Schmidhuber, J. (1997). Long short-term memory. Neural Computation, 9(8), 1735–1780.

📖 Attention entered as an add-on to recurrent translation models; three years later the Transformer removed the recurrence entirely.

Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural machine translation by jointly learning to align and translate. ICLR 2015 (arXiv:1409.0473). — Vaswani, A., et al. (2017). Attention is all you need. Advances in Neural Information Processing Systems, 30.

A field’s ML forecasters, finally raced against simple statistical baselines — and losing. Years of skipped baselines, recalibrated.

Makridakis, S., Spiliotis, E., & Assimakopoulos, V. (2018). Statistical and machine learning forecasting methods: Concerns and ways forward. PLOS ONE, 13(3), e0194889.

Attention at planetary scale: a 3-D transformer forecaster judged head-to-head against the operational weather baseline — and winning honestly.

Bi, K., et al. (2023). Accurate medium-range global weather forecasting with 3D neural networks. Nature, 619, 533–538.

Memory (1997) → attention (2014) → attention only (2017) → the weather (2023). One thread, and today we build each link.

The task: 90 days in, 30 days out

Context window = what the model reads; forecast horizon = what it must write. Temporal split, no window crossing the boundary — lecture 18’s rule, unchanged.

Why the plain recurrent network forgets

Training pushes the gradient back through all 90 steps — a product of ~90 factors. Slightly under one → vanishes; slightly over → explodes. Simple recurrent memory tops out at tens of steps.

Two escapes: gates, or drop the recurrence

  • LSTM (1997): a cell state updated by addition, guarded by learned forget / input / output gates — gradients cross long sequences without shrinking
  • Attention (2014): every day looks directly at every other day — each position asks a query, offers a key, passes a value; softmax-weighted average, no 90-step product anywhere
  • Transformer (2017): attention only, stacked, plus positional encodings — because attention alone is blind to order

One attention head is ~15 lines of code — and you will write them, from scratch, today.

The shootout — baselines first

Model Parameters Val MAE, 30-day horizon
Persistence (tomorrow = today) 0 2.10 mm
Seasonal naive (value 365 d ago) 0 15.53 mm
Vanilla RNN 2,110 1.83 mm
LSTM 5,470 1.86 mm
Attention (from scratch) 4,126 1.87 mm
Transformer encoder 18,142 1.84 mm

Same windows, same loss, same optimizer, same metric — MAE in millimeters over the 30-day horizon, so the numbers are directly comparable.

The learned models beat persistence by ~13% — real but modest. Seasonal naive fails because the 12 mm/yr trend makes last year systematically low: a baseline informs only when it matches the structure it targets.

Same discipline, three lectures

Session Learned model The baseline it faced Verdict
Wed — MLP PNW source classifier Chapter 3 random forest tie on tabular features
Fri — CNN earthquake detector STA/LTA at matched false alarms wins half a decade of SNR
Today — sequence GNSS forecasters persistence, 0 parameters +13%, all four architectures

Architecture follows data structure: dense for tables, convolution for patterns that move, memory or attention for sequences. The baseline row decides whether any of it mattered.

Now race them yourself — open 4.4

  1. pixi run jupyter labmlgeo_4.4_RNN.ipynb: build the windows, check no pair crosses the 8-year boundary
  2. Train all four models; reproduce the comparison table with its baseline rows
  3. Double the horizon to 60 days, retrain: which errors grow, and why?
  4. HW-DL assigned today (due Fri Dec 4): the full pipeline on new data — start this week

PyTorch names live here: nn.RNN, nn.LSTM, nn.TransformerEncoderLayer, register_buffer for positional encodings · classification leaderboard closes Tue · Wed: training lab I (4.5)