7 routes42 sections324 units

Unit 4.5 · about 5 h

197 / 324

Recurrent gradients, GRU and LSTM gates

Gates are the fix for the gradient pathology of the previous unit. Each gate is a differentiable decision to keep, overwrite or reveal memory.

  • derive
  • implement

You can now

  • Derive why repeated hidden Jacobians vanish or explode from their eigenvalue powers.
  • Write the GRU and LSTM updates from memory and explain what each gate preserves or erases.
  • Implement both cells from tensor ops and match framework cells on values and gradients.

Covers

  • Vanishing and exploding recurrent gradients
  • GRU and LSTM gates

Study, in this order

3 steps

  1. Jacobian powers and gradient pathology
    D2L - convolutional and recurrent architecture route

    Backprop Through Time in Detail: the linear-recurrence gradient derivation ending in hidden-Jacobian powers. Exercises question 1: symmetric-matrix powers and what they mean for RNN gradients.

  2. GRU gates and scratch implementation
    D2L - convolutional and recurrent architecture route

    Reset Gate and Update Gate, Candidate Hidden State, Hidden State, and Implementation from Scratch

  3. LSTM gates and scratch implementation
    D2L - convolutional and recurrent architecture route

    Input, Forget and Output Gates, Memory Cell Internal State with \(C_t = F_t C_{t-1} + I_t \tilde{C}_t\), Hidden State, and Implementation from Scratch

    Depth · Skip the JAX lax.scan branches unless you train in JAX. The equations are framework-free.

Focus on

  • \(dL/dh_t\) fans out through powers of \(W_{hh}\) transposed. Eigenvalues below 1 decay geometrically and above 1 grow geometrically, and tanh or sigmoid derivatives shrink most steps further.
  • GRU: \(R\) gates how much past state enters the candidate, and \(Z\) interpolates old state with candidate. LSTM splits the same job across \(F\) for erase, \(I\) for write and \(O\) for reveal, with a separate cell \(C_t\).
  • With \(F=1\) and \(I=0\) the cell holds its value exactly and gradients flow unchanged. That constant-error path is the whole point.
  • PyTorch and D2L agree on the update gate: \(z\) weights the old state. They differ on the reset gate: D2L multiplies \(R\) into the state before \(W_{hh}\), while torch.nn.GRU applies \(r\) after the recurrent matmul and its bias, \(r \cdot (W_{hn} h + b_{hn})\). Implement the PyTorch form when you match torch.nn.GRU.

Reproduce closed-book

Closed-book, write all GRU and LSTM equations with gate nonlinearities. Then implement both cells from tensor ops and match torch.nn.GRU and torch.nn.LSTM outputs on random input to float32 tolerance, using the PyTorch reset-gate form for the GRU.

Traps

  • Porting gate formulas between sources without checking conventions. Some texts write \(h_t = (1 - z) h_{t-1} + z \tilde{h}\), the opposite of D2L and PyTorch, and the reset placement differs between D2L and torch.nn.GRU.
  • Assuming LSTM always beats GRU. Cost and data decide, which is why the LSTM page asks for the cost comparison.
  • Treating saturated gates as a bug to tune away. Gates at 0 or 1 stop gate learning while protecting memory, so that trade is expected.

Practice

TrenTorch: GRU Forward

Implement gru_cell_forward with torch.nn.GRUCell's [reset, update, new] packing; one test is a GRUCell oracle

The reset gate multiplies only the hidden-side candidate term, the PyTorch form. Then solve LSTM Forward (seq-rnn-lstm-cell-forward) in the same track, with the [input, forget, cell, output] packing.

D2L GRU Exercises 1 and 4: gate values and single-gate ablation

Exercises: question 1 (best reset and update values to use only the input at \(t'\) for the output at \(t\)) and question 4 (a GRU with only a reset gate or only an update gate)

D2L LSTM Exercise 3: gated-cell cost comparison

Exercises: question 3 (computational cost of GRU, LSTM and plain RNN at fixed hidden size, training and inference)

Self-test

  1. A linear recurrence has \(W_{hh}\) with spectral radius 1.5. What gradient scale results over 100 steps?

  2. GRU with \(Z=1\) everywhere under the D2L convention. What behavior results?

  3. LSTM: which gate settings copy \(C_{t-1}\) unchanged?

  4. A GRU with a reset gate but no update gate. What breaks?

Completion criterion

Stop when

Your cells match framework outputs and gradients, and the ablations show which gate carries long-span memory.

Mark passed, then open 4.6 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • D2L - convolutional and recurrent architecture route
    D2L authorsprimarycourse
    Use why convolution, convolution layer, padding and strides, pooling, channels, batch normalization and ResNet, then RNN, BPTT, scratch RNN, GRU, LSTM, encoder-decoder and seq2seq. Work through the scratch mechanisms before concise APIs.

Also in this section

  • Understanding Deep Learning - convolution and residual networks
    Simon J. D. Princerequiredtheory
    Use Chapters 10 and 11 to sharpen convolutional invariance, receptive-field reasoning, residual paths and normalization. D2L remains the recurrent implementation route.
  • Stanford CS231n - Assignments 1 and 2
    Stanford Universitysupportpractice
    Use only the neural network, normalization, dropout, convolution and PyTorch components that match this section. Do not complete the course or unrelated vision tasks.
  • Deep-ML candidate - convolution and recurrence problems
    Deep-MLoptionalpractice
    After the platform admission check, use at most one of 41 Conv2D, 62 RNN with BPTT, 287 GRU or 59 LSTM as a changed-prompt drill.
  • TrenTorch - convolution cost and recurrent cells
    TrenTorchsupportpractice
    Solve FLOPs Estimation, GRU Forward and LSTM Forward after the D2L scratch models. Each reference cell matches torch.nn.GRUCell or LSTMCell.
  • PyTorch nn.Conv2d reference
    PyTorchreferencereference
    Read Signature and Shape for the exact floor formula with dilation, the groups divisibility rule with grouped and depthwise examples, and the weight layout \((C_{\text{out}}, C_{\text{in}}/\text{groups}, kH, kW)\). D2L and UDL teach the mechanism; this page is the shape contract to code against.
  • PyTorch sequence packing utilities
    PyTorchreferencereference
    Read pack_padded_sequence for lengths, enforce_sorted and batch-first at recognition depth: what packing skips and what contract it demands. Do not reimplement the packed format; pair it with the masked-loss implementation in D2L seq2seq.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels