5.6.13 · D4Machine Learning (Aerospace Applications)

Exercises — LSTM — gates, cell state

2,870 words13 min readBack to topic

Before we start, one shared reference card. Everything on this page runs on these six lines.

Two numbers you will use so often we plot them once. The figure below shows the two squashing curves so you can read values off by eye instead of trusting a formula blindly.

Figure — LSTM — gates, cell state

Notice: at input , (valve half-open) and (writes nothing). As input grows large positive, (valve wide open) and . As input goes large negative, (valve shut) and . Keep this picture in your head — most exercises are just "which part of these curves are we on?"


L1 — Recognition

Goal: name the pieces and match symbols to roles. No arithmetic.

Exercise 1.1. An LSTM carries two vectors from one timestep to the next. Name both, and say which one is the protected long-term memory and which one leaves the cell as output.

Recall Solution 1.1

The two carried vectors are the ==cell state and the hidden state ==.

  • is the protected long-term memory (the "conveyor belt"), updated additively.
  • is the short-term / output vector; it is what actually leaves the cell for prediction and feeds the next step's gates. Only leaves — never the raw .

Exercise 1.2. For each of the four learned quantities , state its activation function and its output range.

Recall Solution 1.2
Quantity Activation Range Why
forget fraction of old memory kept
input fraction of candidate written
output fraction of memory read out
candidate signed value to possibly write
Gates are valves so they need ; the candidate must be able to push memory up or down, so it needs the signed range .

Exercise 1.3. Which line of the equations is additive and which parts of it are multiplicative? Point to where "forgetting" and "writing" happen.

Recall Solution 1.3

The additive line is the cell update: The two products are multiplicative gating. Forgetting happens in (scaling the old memory down); writing happens in (scaling the proposed new value). The plus sign in the middle is the whole trick — memory is added to, not overwritten.


L2 — Application

Goal: plug real numbers into the six equations. Scalar states so you can do it by hand.

Exercise 2.1. Scalar LSTM. Given , , , , . Compute and . Use .

Recall Solution 2.1

Cell: . Output: (to 3 dp). Reading: we kept of old memory, added of the candidate, then read out of the squashed result.

Exercise 2.2. A pre-activation of goes into a gate. What is the gate value, and what does it do to whatever it multiplies?

Recall Solution 2.2

. A gate at passes half of whatever it multiplies through. It is the "half-open valve" point on the red curve in the figure above.

Exercise 2.3. The candidate's pre-activation is a large negative number, say . What does do to the memory when written? Give to 3 dp.

Recall Solution 2.3

. So this candidate proposes writing a value near : it will push the memory downward. This is exactly why the candidate uses and not — it must be able to decrease memory, not only increase it.

Exercise 2.4. Two-dimensional cell. , , , . Compute elementwise.

Recall Solution 2.4

Elementwise () means work coordinate by coordinate.

  • Entry 1: .
  • Entry 2: . So . Entry 2 wrote nothing () and just decayed the old value a little ().

L3 — Analysis

Goal: reason about behaviour and gradients — the "why", not just the "what".

Exercise 3.1. Along the cell line only, show that , and explain why this single fact is the reason LSTMs beat vanishing gradients.

Recall Solution 3.1

Start from . Treating the gates and candidate as inputs (they depend on , but we follow the direct -highway), differentiate w.r.t. : There is no weight matrix and no factor — just the gate. Chaining steps, If the forget gates stay near , the product stays near , so gradients travel back undiminished — the constant error carousel. Contrast a plain RNN, whose factor is with magnitude typically , so its product exponentially. See Vanishing & Exploding Gradients and Backpropagation Through Time (BPTT).

Exercise 3.2. A forget gate is stuck at every step. By what factor has the gradient along the cell path shrunk after 50 steps? After 200 steps? Is this "vanishing"?

Recall Solution 3.2

The factor is .

  • .
  • . After 50 steps it is already tiny; after 200 it is effectively gone. So near but below still vanishes, just slower than a plain RNN. Genuine long memory needs very close to (this is why we bias high — see 3.3).

Exercise 3.3. LSTMs are often initialised with a large positive forget-gate bias to . If the rest of the pre-activation is , what is at and at ? Why is this a good default?

Recall Solution 3.3

.

  • .
  • . Both are close to , so at the start of training the network remembers by default. This keeps the cell highway open and gradients alive during the crucial early epochs, before the gates have learned anything. Initialising would give — half the memory erased every step — and training stalls.

Exercise 3.4. Set and for all . Show algebraically that , and say why a plain RNN cannot copy a value this way.

Recall Solution 3.4

With : . By induction . Perfect copy. A plain RNN has : to hold a value it would need exactly for the right , and even then the backward Jacobian shrinks it. There is no "identity valve" — memory is always reprocessed. The LSTM's plus-sign gives it one for free.


L4 — Synthesis

Goal: build small gate-programs yourself to achieve a stated behaviour.

Exercise 4.1. You want the cell to completely reset to a fresh candidate at this step (throw away all history, write the new value fully). What do you need, and what does become?

Recall Solution 4.1

Choose (erase old) and (write candidate fully): The cell is overwritten by the candidate — this is deliberately RNN-like behaviour, useful when a genuinely new regime starts (e.g. a maintenance event resets a machine's history).

Exercise 4.2. Design the gate schedule for this aerospace task: a rare high-amplitude vibration spike arrives at and must influence a Remaining Useful Life (RUL) Prediction forecast at . Specify approximate at , at , and at .

Recall Solution 4.2
  • (capture): (write the spike into ), and let encode its magnitude. is task-dependent — nonzero only if a prediction is also needed here.
  • (hold): , — keep the stored spike, write nothing new over it. Set during this stretch: we do not want the stored spike leaking into the output yet, so the read valve stays shut until release. (If intermediate forecasts are needed, may reflect other channels, but the spike's channel stays gated.)
  • (release): so actually reveals the stored info to the forecast head. Gradient descent discovers this routing automatically because the gates are data-dependent learned switches; see Time Series Forecasting.

Exercise 4.3. Build a "leaky integrator" (running average) out of one LSTM cell. If you want , what constant gate values achieve it, and what property does it give the memory?

Recall Solution 4.3

Set and (constant). Then an exponential moving average of the candidates. Memory of an old input decays as : it fades gradually rather than being kept forever (large ) or reset (small ). This is useful for smoothing noisy sensor streams. Note need not equal in a real LSTM — here we chose them to sum to on purpose to get a clean average.


L5 — Mastery

Goal: handle every case, sign, and degenerate input — the corners where intuition breaks.

Exercise 5.1 (unbounded cell, bounded output). The cell has been accumulating and reaches . Compute to 4 dp, then if . Explain why the inside is not redundant with the candidate's .

Recall Solution 5.1

(to 4 dp; exactly ). So . The candidate's bounds what we write in one step; but is a running additive sum and can grow unbounded over many steps (here ). The output's re-squashes the accumulated cell to so downstream layers always see bounded, stable values. Two different jobs — not redundant. See Sigmoid and Tanh Activations.

Exercise 5.2 (negative memory, every sign). , , , , . Compute and . Check the sign of matches the sign of and explain why it must.

Recall Solution 5.2

. , so (4 dp). and : they share a sign because is an odd function () and the output gate is always non-negative, so it never flips sign. A negative memory reads out as a negative hidden value.

Exercise 5.3 (degenerate: all gates zero). Set . Compute and for any inputs. What does the cell "become", and is this ever useful?

Recall Solution 5.3

. . The cell is fully wiped and emits nothing — a memoryless dead unit. It is essentially never a good learned state (it destroys all gradient flow through that cell), which is precisely why we bias positive at init to steer away from this corner. Useful only as a deliberate "silence this channel" behaviour.

Exercise 5.4 (limiting gradient behaviour). Over a horizon of steps with a constant forget gate , the cell-path gradient scales as . Describe all three limiting regimes: , , and . Give the limit of as in each.

Recall Solution 5.4
  • : for all → gradient preserved forever (perfect memory highway). Limit .
  • : but only polynomially-slowly compared to how fast a plain RNN dies; still vanishes eventually. Limit . (E.g. survives ~hundreds of steps.)
  • : for every → gradient killed in one step. Limit (instantly). The whole art is keeping as close to as the task allows for as long as the memory is needed. This is the analysis backbone of Backpropagation Through Time (BPTT) for LSTMs.

Exercise 5.5 (GRU contrast, synthesis+mastery). In an LSTM, write the "copy memory forever" recipe. Then state how a GRU — Gated Recurrent Unit achieves the same copy with its single update gate , and why it needs fewer parameters.

Recall Solution 5.5

LSTM copy: . GRU copy: a GRU uses . Setting gives — same perfect copy. The GRU ties keep-vs-write into one gate (write fraction) with its complement (keep fraction), and has no separate output gate (it exposes directly). Fewer gates → fewer weight matrices → fewer parameters, at the cost of less independent control. Same constant-error-carousel idea, leaner hardware — attractive for embedded avionics.


Connections