Unit 4 of 7 · about 5 hours · derive, implement

Recurrent cells, unrolling and backpropagation through time

Unrolling turns a loop into a deep shared-weight network. BPTT is backprop on that unrolled graph, and truncation decides which dependencies the gradient can ever see.

You'll be able to

  • Unroll a vanilla RNN cell and derive its per-step shapes and shared-parameter count.
  • Derive BPTT as chained Jacobian products and state what truncating at keeps and drops.
  • Implement an RNN language model from tensor ops with gradient clipping and train it below uniform perplexity.

Covers

Vanilla recurrent cells and sequence unrollingBackpropagation through time and truncated BPTT
1

Read

In this order, only the parts listed

0 of 3 done

  1. The recurrent cell and its unrollingD2L - convolutional and recurrent architecture route

    Recurrent Neural Networks with Hidden States (the recurrence and parameter sharing) and RNN-Based Character-Level Language Models

  2. Scratch RNN with clipping and decodingD2L - convolutional and recurrent architecture route

    The RNN Model from scratch, One-Hot Encoding, Gradient Clipping with the rule, Training, and Decoding with warm-up prefix

  3. Backpropagation through time and truncationD2L - convolutional and recurrent architecture route

    Analysis of Gradients (full computation with the chained sum, regular truncation at , randomized truncation) and Backprop Through Time in Detail (the , and gradient sums)

2

Focus on

  • reuses the same three matrices every step. Sequence length adds depth, not parameters.
  • BPTT sums per-step losses whose gradients chain products of hidden Jacobians. Regular truncation at drops all paths longer than and biases long-dependency learning.
  • Clip by global norm after unrolling. It bounds the step, not the Jacobian product, so it treats explosion, not vanishing.
  • Detaching the hidden state between batches stops gradients but keeps the values. Truncated BPTT still learns only within-segment dependencies.
Common mistakes · 3
  • Averaging the loss over time and thinking truncation is then unbiased. Truncation drops long paths whatever the reduction.
  • Carrying hidden state across batches and calling it full BPTT. Values carry but gradients do not unless the graph does.
  • Clipping to fix non-learning from vanishing. Clipping only caps large steps, so vanishing needs architecture or objective changes.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write the and equations with shapes for the D2L character vocabulary of 28 tokens, hidden size 32 and batch 4, and derive as the chained sum. Then train the scratch RNN on The Time Machine to a perplexity well below 28, the value of uniform guessing.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1., hidden 32. How many matrices does the unrolled graph hold?Answer

    One shared matrix. Unrolling copies the use, not the parameters, and gradients accumulate into it from all steps.

  2. 2.Truncated BPTT with on a dependency of length 50. What happens?Answer

    The gradient path is cut. The update cannot assign credit across the full span, so the dependency is effectively unlearnable at that .

  3. 3.Gradient norm 12 with . What is the clipped gradient?Answer

    . scales the norm to exactly 1 without changing direction.

  4. 4.Why does D2L one-hot encode instead of using an embedding layer?Answer

    Small char vocab makes one-hot cheap and explicit. It is the same linear map as an embedding row lookup, so shapes and gradients agree.

5

Practice

You're done when

Your scratch RNN trains below uniform perplexity, Deep-ML 62 passes, and you can state which dependencies your can and cannot learn.

Only when you can do this without notes.

Leads to

Notes

Resources · 1 for this unit, 6 more in the section
  • D2L - convolutional and recurrent architecture routeD2L authors · primary

    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 networksSimon J. D. Prince · required

    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 2Stanford University · support

    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 problemsDeep-ML · optional

    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 cellsTrenTorch · support

    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 referencePyTorch · reference

    Read Signature and Shape for the exact floor formula with dilation, the groups divisibility rule with grouped and depthwise examples, and the weight layout . D2L and UDL teach the mechanism; this page is the shape contract to code against.

  • PyTorch sequence packing utilitiesPyTorch · reference

    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.