3.5.4 · D4Sequence Models

Exercises — Long Short-Term Memory (LSTM) cells

2,007 words9 min readBack to topic

Throughout, recall the six LSTM equations (with , = element-wise product):


Level 1 — Recognition

Exercise 1.1 — Name the gate

Match each phrase to its gate (forget / input / output): (a) "decides how much of the old memory survives" (b) "decides how much of the external memory to reveal to the next layer" (c) "decides how much of the new candidate to write in".

Recall Solution 1.1

(a) forget gate — it multiplies , so it scales the old memory. (b) output gate — it multiplies to build , the exposed value. (c) input gate — it multiplies the candidate before it is added. Anchor picture: figure below. Forget is the valve on the incoming belt, input is the valve on the new-cargo chute, output is the window letting you peek at the belt.

Figure — Long Short-Term Memory (LSTM) cells

Exercise 1.2 — Range of each quantity

State the output range of , , , , and .

Recall Solution 1.2

— all pass through sigmoid, whose graph rises from to . — passes through tanh, which is signed so the memory can go up or down. — squashed for the same signed-but-bounded reason. Note itself has no bound — it is the raw conveyor belt and may grow past .

Exercise 1.3 — Why sigmoid for gates, tanh for candidate?

In one sentence each, say why gates use and the candidate uses .

Recall Solution 1.3

Gates answer "what fraction to keep?" — a fraction lives in , exactly sigmoid's range. The candidate answers "what value to write?" — memory content can be positive or negative, so we need the signed range that tanh gives.


Level 2 — Application

Exercise 2.1 — One forward step, forget gate

Given , , , . Compute .

Recall Solution 2.1

Apply the cell update directly: Notice : the belt is unbounded, confirming Exercise 1.2.

Exercise 2.2 — Compute the hidden state

Continuing 2.1 with and output gate , find . Use .

Recall Solution 2.2

So even though the belt holds , the world only sees — squashed by tanh, then masked by .

Exercise 2.3 — Sigmoid by hand

The pre-activation of a forget gate is . Compute .

Recall Solution 2.3

Interpretation: about of each old-memory coordinate survives this step.

Exercise 2.4 — Two steps of the counting cell

Set , , at every step (the counting configuration from the parent's Example 2), with . Compute .

Recall Solution 2.4

The update collapses to : The cell state is literally an integer counter — impossible for a vanilla RNN whose caps values at .


Level 3 — Analysis

Exercise 3.1 — The gradient through one cell step

Show that (treating as held fixed), and explain in words why this beats the vanilla-RNN factor .

Recall Solution 3.1

Differentiate with respect to . The second term does not contain , so its derivative is . The first term is element-wise multiplication by the constant , whose derivative is itself: Why it wins: across steps the RNN multiplies copies of , and since and is generic, that product decays (or blows up) exponentially. The LSTM instead multiplies ; if the network learns , that product stays near over hundreds of steps — a gradient highway.

Exercise 3.2 — 100-step gradient survival

Suppose every forget gate equals . What fraction of the gradient survives steps back? Compare to a vanilla RNN whose per-step factor magnitude is .

Recall Solution 3.2

LSTM: — about survives. RNN: — effectively . The gap of roughly four orders of magnitude is the reason LSTMs learn long dependencies.

Figure — Long Short-Term Memory (LSTM) cells

Exercise 3.3 — When the forget gate is a leak

If constantly and (no new writes), find the closed form of from , and after how many steps the memory drops below of .

Recall Solution 3.3

With no writes, . Below means . Take logs: . So the memory dips under at . A half-open forget gate is a fast leak — long memory needs near , not near .


Level 4 — Synthesis

Exercise 4.1 — Parameter count

An LSTM has input dim , hidden dim , and . Using , compute the total number of parameters.

Recall Solution 4.1

Inside the bracket: ; then ; add the bias to get . Multiply by the gate/candidate blocks: So trainable parameters — four times a same-size vanilla RNN block, the price of the gates.

Exercise 4.2 — Design a "latch" (store one bit forever)

You want a 1-D cell that, once it sees the token , stores and never changes, ignoring everything after. Give the ideal gate values () for (a) the step that sees (with ), and (b) every later step, and give after each.

Recall Solution 4.1... 4.2

(a) On seeing : open the input gate and write , forget nothing (there's nothing yet): (b) Every later step: freeze — keep the old value, write nothing: The memory is latched at permanently. This is exactly why gives a lossless gradient path for the stored bit.

Exercise 4.3 — Sentiment "not bad" trace

Reproduce the parent's Example 1. Step 1 ("not"): , , , . Step 2 ("bad"): , and the input contribution . Compute and .

Recall Solution 4.3

The negation stored at step 1 () survives almost intact to step 2 (), because . That preserved value is what flips "bad" into an overall positive judgement.


Level 5 — Mastery

Exercise 5.1 — Full single-step forward pass (1-D)

All scalars. Inputs: , , . Weights collapse to precomputed pre-activations: , , (goes through tanh), . Compute . Use , computed from your .

Recall Solution 5.1

Gates first (all sigmoids): Candidate: Cell update: Hidden state: , so Full answer:

Exercise 5.2 — Contrast the two architectures' memory

In one paragraph, explain to a beginner why the parent's counting task ("[[]]" → 4) is possible for an LSTM but impossible for a vanilla RNN, referring to the exact update equations.

Recall Solution 5.2

The LSTM's cell update is additive and unbounded — set and it becomes , a pure integer accumulator (Exercise 2.4). The vanilla RNN update is saturating: no matter how many it sees, clamps the state into , so it can never represent "4". The additive highway is what turns memory from a squashed blur into an exact register. This is the same additive structure that later inspired the residual/skip connections used in 4.2.01-Transformers.

Exercise 5.3 — Relate to GRU

The 3.5.05-Gated-Recurrent-Units-(GRU) merges forget and input into a single update gate , using . If an LSTM wanted to mimic this coupling, what constraint would it place on and ? Verify with a number: if the GRU's , what LSTM matches?

Recall Solution 5.3

The GRU ties keep-fraction and write-fraction together: and so the two coefficients sum to . For : and , and indeed . The LSTM is strictly more expressive because it can choose and independently (e.g. for counting, which violates ), at the cost of the extra parameters counted in Exercise 4.1.

Recall Self-test one-liners

Range of a gate output ::: because it is a sigmoid. The single-step cell gradient ::: equals . Fraction of gradient surviving 100 steps at ::: about . LSTM params for ::: . GRU-matching LSTM gates when ::: .


Related paths: 3.5.06-Bidirectional-RNNs (running an LSTM in both directions) and 3.6.01-Attention-Mechanisms (letting the model look back directly instead of relying only on the cell-state highway).