By the end
- Create chronological input windows and targets.
- Trace a simple recurrent hidden-state update.
- Explain how LSTM gates change the memory path.
- Compare a naive baseline, RNN, and LSTM using one temporal split.
Week 11 · Hands-on foundations
How do recurrent and gated models use earlier observations without leaking future information?
The historical recurrent unit contains separate notebooks for RNNs, LSTMs, text processing, sentiment, and generation. With PBL presentation weeks restored, this active week keeps the durable comparison: chronological windows, recurrent hidden state, LSTM gates, a naive baseline, and one held-out temporal evaluation. Large NLP pipelines move to the stretch lane.
This chapter condenses materials taught in the 2023–2025 Advanced Data Science course into a smaller core path. Optional depth remains available in the companion notebook.
Use the explorer and three short Python examples before changing a longer model.
Use three seeds or a longer synthetic dependency to determine whether an LSTM advantage is stable rather than a one-run accident.
Core ideas
A sequence example uses earlier values to predict a later value. Splitting by time and fitting preprocessing on the training period prevent future information from entering the model.
An RNN combines the current input with a previous hidden state using shared parameters. Repeated multiplication can weaken or destabilize long-range influence.
An LSTM adds gates controlling retained and written information. This changes the path through which information and gradients travel, but it does not remove the need for a baseline and valid split.
Mathematical intuition
The LSTM cell retains a gated part of old memory and adds a gated candidate. The simple RNN remains the essential comparison because added memory machinery must improve held-out behavior to be justified.
Identify the input, learned quantity, output, and aggregation. Re-create the relationship with a tiny hand-checkable example before using a library layer.
Interactive explorer
Predict the direction of change before moving a control.
Change a control to inspect the relationship.
Hands-on path
Minimal Python
Each example is independent. Explain its output before copying it into a larger workflow.
series = [2, 4, 6, 8, 10]
windows = [(series[i:i+2], series[i+2]) for i in range(3)]
print(windows)Predict the result, then click Run.
import math
x, previous = 0.5, 0.2
hidden = math.tanh(1.2*x + 0.8*previous)
print(round(hidden, 3))Predict the result, then click Run.
old, forget, candidate, input_gate = 1.0, 0.8, 0.5, 0.3
new = forget*old + input_gate*candidate
print(round(new, 2))Predict the result, then click Run.
Failure checks
What goes wrong: Nearby future patterns appear in training and make performance optimistic.
Check: Split by time and inspect the last training date and first test date.
What goes wrong: An LSTM is accepted because it is more sophisticated.
Check: Compare against persistence and a simple RNN under identical conditions.
What goes wrong: A bidirectional model uses future context for a real-time decision.
Check: Match model direction to the actual information boundary.
Check your understanding
Answer in your own words before opening each panel.
Standard answer: A strong answer connects the central idea to a visible computation and a held-out or shape-based check. Large NLP pipelines move to the stretch lane.
Standard answer: A sequence example uses earlier values to predict a later value. Splitting by time and fitting preprocessing on the training period prevent future information from entering the model.
Standard answer: An RNN combines the current input with a previous hidden state using shared parameters. Repeated multiplication can weaken or destabilize long-range influence.
Standard answer: An LSTM adds gates controlling retained and written information. This changes the path through which information and gradients travel, but it does not remove the need for a baseline and valid split.
Standard answer: The LSTM cell retains a gated part of old memory and adds a gated candidate. The simple RNN remains the essential comparison because added memory machinery must improve held-out behavior to be justified. It does not by itself prove useful behavior on unseen intended-use cases.
Standard answer: The visible calculation and interpretation should change together. Predict the direction first, then use the result to correct your mental model.
Standard answer: Define what information is available at prediction time.
Standard answer: Split by time and inspect the last training date and first test date.
Standard answer: Record data and split assumptions, input/output shapes, settings, the baseline, held-out metrics, representative failures, and the decision supported by that evidence.
Standard answer: Week 12 replaces recurrent information flow with direct attention across positions and introduces the Transformer block.
Terminology
Go further
Next: Week 12 replaces recurrent information flow with direct attention across positions and introduces the Transformer block.