3.5.2 · D4Sequence Models

Exercises — Backpropagation through time

2,758 words13 min readBack to topic

Throughout we reuse the standard names from the parent note. Nothing new is assumed; every symbol is re-explained the first time it appears here.


Level 1 — Recognition

Exercise 1.1 (L1)

In the equation , which single weight is shared across every time step, and why does that sharing make BPTT necessary?

Recall Solution

The shared weight is . In fact and are shared across steps too, but is the one that links step to step (it multiplies the previous hidden state). Because the same is used at (recall is the sequence length), a tiny change in it ripples into every hidden state, so its total gradient is a sum of contributions from all time steps: Ordinary layer-by-layer backprop assumes each weight lives in one place; BPTT is exactly the bookkeeping for a weight that lives in many places at once.

Exercise 1.2 (L1)

Match each name to its formula: (a) post-activation gradient, (b) pre-activation gradient, (c) slope.

Recall Solution
  • (a) → (ii): is the gradient at the output side of the .
  • (b) → (iii): is pushed back through the to its input side.
  • (c) → (i): (our shorthand ) is the derivative of written using its own output (where is the pre-activation defined above), since .

Exercise 1.3 (L1)

The gradient that flows one step backward is Name the two factors and say where each comes from.

Recall Solution
  • comes from the nonlinearity — its slope at step . "diag" means we place these slopes on the diagonal of a matrix because acts element-by-element.
  • comes from the linear part, the pre-activation (see the symbol list); differentiating this linear term with respect to gives .

Level 2 — Application

Exercise 2.1 (L2)

1-D RNN, activation, , , , . Inputs , . Compute and (4 decimals). (Here the sequence length is .)

Recall Solution

WHAT/WHY: just run the forward recurrence; each needs the previous plus this step's input. Recall is the pre-activation.

Exercise 2.2 (L2)

Same network as 2.1, with output and loss , targets , . Compute the post-activation gradient .

Recall Solution

At the last step there is no future to receive gradient from, so is just the local loss slope :

Exercise 2.3 (L2)

Continue 2.2. Compute using

Recall Solution

WHY this formula: affects the loss two ways — directly through , and indirectly by feeding . The second term is exactly .

Exercise 2.4 (L2)

Using the results above, compute the pre-activation gradients and then

Recall Solution

Since , only the term survives.


Level 3 — Analysis

Exercise 3.1 (L3)

The gradient carried steps back is proportional to repeated times. For a 1-D RNN with all slopes equal to and , what factor multiplies the gradient after 10 steps? Is this vanishing or exploding?

Recall Solution

Each backward step multiplies by . After 10 steps: The factor is far below 1, so the signal from 10 steps ago is crushed — this is vanishing gradient. See 3.5.03-Vanishing-and-Exploding-Gradients.

Exercise 3.2 (L3)

Now take and slopes (network barely saturated). What multiplies the gradient after 8 steps? Classify it.

Recall Solution

Per-step factor . After 8 steps: The gradient is amplified thousands of times — exploding gradient.

What the figure below shows. The plot traces the same backward recurrence for two different per-step factors on a log scale. The magenta curve uses factor (Exercise 3.1): it dives toward zero — the vanishing regime. The orange curve uses factor (Exercise 3.2): it rockets upward — the exploding regime. The violet dashed line marks the healthy value where gradients neither shrink nor grow. The single message: whether a gradient survives being carried back many steps is decided by whether the per-step factor sits below or above that dashed line.

Figure — Backpropagation through time

Exercise 3.3 (L3)

Explain, using , why deep saturation of (outputs near ) makes the vanishing problem worse, and why this is one motivation for 3.5.04-Long-Short-Term-Memory-LSTM.

Recall Solution

When saturates, , so its slope . Every backward hop then multiplies by a near-zero number, so gradients from far away die even faster than alone would predict. An LSTM sidesteps this with an additive cell state whose gradient path multiplies by values near 1 (the forget gate) instead of a shrinking slope, keeping long-range signal alive.

Exercise 3.4 (L3)

So far we only tracked magnitudes. Suppose (negative) with all slopes . What is the per-step factor, and what happens to the sign of the gradient as it is carried back many steps? Why does this matter?

Recall Solution

Per-step factor . Its magnitude () still vanishes, but the sign alternates: after steps the factor is , which is positive for even and negative for odd . Why it matters: when the total gradient collects contributions of alternating sign, some terms cancel each other. Two sequences with almost identical magnitudes can end up with very different — even near-zero — total gradients purely because of these sign flips. A magnitude-only analysis () would completely miss this cancellation. (For the same alternating sign instead amplifies with flipping direction, making updates oscillate.)


Level 4 — Synthesis

Exercise 4.1 (L4)

Write, in order, the three phases of the BPTT algorithm for a sequence of length , naming exactly what is computed and stored in each — including the bias .

Recall Solution

1. Forward pass — compute and store every and every loss . Why store them? The backward pass needs each twice: once for the slope and once as the incoming state in the weight sum. We trade memory (holding all hidden states) for the ability to compute exact gradients — without them we'd have to recompute the forward pass repeatedly, which is far more expensive. This memory-for-compute trade is exactly why long sequences later force truncation (Exercise 5.1).

2. Backward pass — set (no future term, since has no successor), then for down to apply the recursion below. Why backward and why this shape? Gradient at has two sources — the local loss and everything downstream, which is already summarised in . Working right→left lets us reuse instead of re-deriving the whole future each time. The one-step Jacobian is To send the gradient vector backward through this map we multiply by its transpose (a gradient is pulled back by the transpose of the forward Jacobian — that is what "backprop" means). Transposing gives (the diagonal is its own transpose, and the order of the two factors flips). Hence, written consistently as a pull-back: In the 1-D exercises every matrix is a single number, so transpose does nothing and this reads — exactly what we used in Exercise 2.3.

3. Accumulate gradients — form the pre-activation gradient and sum over all steps, for every shared parameter: The bias gradient is just because enters the pre-activation with a constant multiplier of (), so no extra or factor appears.

Exercise 4.2 (L4)

1-D RNN, , , , , . Inputs , , (so ). Output , loss , targets . Run the full BPTT and report .

Recall Solution

Forward. Local loss slopes (targets are 0): . slopes : . Backward (using ): Pre-activation : Accumulate the weight gradient (with , so the term drops):


Level 5 — Mastery

Exercise 5.1 (L5)

Truncated BPTT. Suppose in Exercise 4.2 we truncate the backward pass to a window of only 1 step (i.e. each ignores contributions from steps beyond : use ). Recompute and compare to the full value . What does the difference tell you about truncation?

Recall Solution

With truncation, , so : Truncated vs full : the truncated gradient is smaller because it drops the long-range terms that push up. Truncation trades some accuracy (missing long dependencies) for cheaper, more stable computation — the standard practical choice for long sequences, and the direct payoff of the memory-vs-compute trade noted in Exercise 4.1.

Exercise 5.2 (L5)

Design reasoning. A network trained with full BPTT on 200-step sequences learns short patterns fine but never captures dependencies longer than ~15 steps, and its far-back gradients are . Which two remedies from the vault directly attack this, and by what mechanism?

Recall Solution

The symptom is vanishing gradients (the per-step product compounds to ).

Exercise 5.3 (L5)

Reveal-drill (cover the right side):

Why is 's gradient a sum over time?
Because the same is used at every step, so it influences every ; total effect = sum of per-step effects.
What turns into ?
Multiplying by the slope (passing back through the nonlinearity).
Per-step backward factor in a 1-D RNN?
.
Product in magnitude over many steps causes?
Vanishing gradients.
What extra effect does a negative add?
Alternating gradient sign, so summed contributions can cancel.
Where does the backward recursion start?
At (no future term).
Gradient of the bias ?
(no extra factor, since ).

See also the parent Backpropagation through time (index 3.5.2) and the general 2.2.03-Backpropagation-Algorithm this specialises.