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

Four canonical networks: today, recurrent and attention

A series in time has an order, and the past informs the future. Recurrence applies the same rule at every day and carries a memory; attention lets every day look at every other day.

This lecture in the literature

Memory → attention → attention only; then baselines first, rivers, the weather.

Hochreiter & Schmidhuber (1997) Neural Comput. 9, 1735–1780 · Bahdanau, Cho & Bengio (2015) ICLR, arXiv:1409.0473 (2014) · Vaswani et al. (2017) NeurIPS 30 · Makridakis, Spiliotis & Assimakopoulos (2018) PLOS ONE 13, e0194889 · Kratzert et al. (2019) Hydrol. Earth Syst. Sci. 23, 5089–5110 · Bi et al. (2023) Nature 619, 533–538. Icons drawn.

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.

Inside an RNN: one day at a time, one memory

The same cell reads day 1 to day 90 and updates a memory h of 32 numbers; a linear head turns the last memory into the next 30 days.

The recurrence by hand

h = tanh(0.8 × x + 0.6 × h of the day before). Day 3 has no input, yet h = 0.22: the network still carries day 1.

How long a memory lasts

A spike fades by w_h each day: below 1% after 7 days (0.5), 44 days (0.9), 459 days (0.99). The same factor, multiplied over the days, shrinks the gradient.

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.

How many weights to fit?

RNN cell 1,120, LSTM cell 4,480 (four gates); with the head, 2,110 and 5,470, as notebook 4.4 prints. The 90 days appear nowhere: the same weights every day.

LSTM: a memory changed by addition

Gates between 0 and 1 decide what to forget, what to write and what to show. The cell state is changed by addition, so the gradient is not multiplied by a small factor every day.

Attention by hand: every day looks at every day

Scores q·k, softmax weights that add up to 1, then a weighted average of the values: 3.40 for day 5. No chain of steps, so nothing fades; positions must be added.

Stacked and bidirectional layers

Stacking adds depth: layer 2 reads the memories of layer 1. A bidirectional layer also reads backward, from the future: right for a finished record, a leak in a forecast.

The head: a forecast, a class, or one output per day

From the last memory: 30 forecast days (regression) or one class per window. From every memory: one output per day, such as the probability of a P arrival.

Leaderboard: 30 days ahead, three seeds (4.4)

Model Weights Validation error (mm) Training (s)
attention, one head 4,126 1.871 ± 0.003 9
recurrent (RNN) 2,110 1.880 ± 0.058 5
LSTM 5,470 1.901 ± 0.045 14
Transformer encoder 18,142 1.950 ± 0.131 25
persistence (no change) 0 2.097 0
seasonal naive (a year earlier) 0 15.529 0

All four beat persistence by 7 to 11%, and tie with each other. The Transformer’s worst seed (2.096) equals persistence.

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 50% detected at SNR 1.4 against 9
Today — sequence GNSS forecasters persistence, 0 parameters 7–11% better; the four tie

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.

Best practices: recurrent networks and attention

Step Practice Rubric
Frame windows from the past only; split in time C3.1, A4
Design context window matches the memory needed C3.2
Baseline persistence and seasonal naive, each horizon C3.3
Evaluate error against forecast horizon C3.4
Evaluate seeds; gaps inside the spread are ties D7, E3

Now race them yourself — open 4.4

  1. pixi run jupyter lab → mlgeo_4.4_RNN.ipynb: the three Your-turn cells (recurrence, LSTM line, one attention query)
  2. Train all four models; publish the leaderboard with its baseline rows; add a second seed
  3. Double the horizon to 60 days, retrain: which errors grow, and why?
  4. HW-DL assigned today (due Fri Dec 4): a CNN detector on real miniPNW seismograms; download the 671 MB at home first

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

Where recurrent networks and attention fit

Recurrent and attention: for sequences where the past informs the future. Here both beat persistence, and neither beat the other.

Next deck: graph networks, for stations at irregular places (lecture 23b, notebook 4.5)