Convolutional & Recurrent Architectures
Convolution and recurrence supply complementary inductive biases. Owning their shapes, costs, state and failure modes makes architecture choice explainable in interviews and experiments.
Section 4 of 8 · 7 units · about 33 hours
By the end you can Derive shape, receptive field and cost for a stated CNN, implement one convolution and one recurrent or gated cell, then prove padding or future input cannot alter a valid output.
Units
- Convolution shapes: channels, padding, stride, dilation and groups5h
- Pooling, transposed convolution, receptive fields and cost4h
- Equivariance, residual CNN blocks and resolution cost5h
- Recurrent cells, unrolling and backpropagation through time5h
- Recurrent gradients, GRU and LSTM gates5h
- Hidden state, encoder-decoder models and teacher forcing4h
- Decoding, variable-length batches and leak tests5h
Build it
Mini project · about 10 to 14 hours
Convolution, gated recurrence and leak tests
Implement a convolution and a GRU or LSTM cell from tensor operations and check them against PyTorch. Derive a small CNN's shapes, receptive field and cost before measuring them, and prove with interventions that padding and future inputs cannot change valid outputs.
- 1Pass the CS231n Assignment 2 checks for the naive convolution and max-pooling passes in
ConvolutionalNetworks.ipynband for the vanilla RNN step and sequence passes inRNN_Captioning_pytorch.ipynb. - 2Write
conv2dwith padding, stride, dilation and groups, usingunfoldor loops, and matchtorch.nn.functional.conv2dinfloat64over a grid of settings that includes groups equal to channels. - 3For a stated small CNN, derive every output shape, the last receptive field, the parameter count and forward cost by hand. Check shapes and parameters in code, and check the receptive field by input gradients under a linearized setup that keeps every path alive.
- 4Implement a GRU or LSTM cell in PyTorch's gate convention, load weights from
torch.nn.GRUCellorLSTMCell, and match outputs and gradients over a 20-step unroll infloat64. - 5Batch variable-length sequences with padding plus masks or packing, and test that changing padded values leaves every valid output and the final valid hidden state unchanged.
- 6For a unidirectional model, test that changing the input at step leaves outputs before unchanged. Run the same test on a bidirectional model and show that it fails, which proves the test can fail.
- 7Plot gradient norms through time for a vanilla RNN and your gated cell on a long sequence, and relate them to measured gate values.
What you hand in
- Convolution and gated-cell modules with
float64differential tests against PyTorch. - The derivation sheet for the stated CNN, with code checks for shapes, parameters and receptive field.
- The leak tests: padding invariance, future-input invariance and the bidirectional counter-case.
How to check it
- CS231n Assignment 2: `ConvolutionalNetworks.ipynb` and `RNN_Captioning_pytorch.ipynb` checks
Run the notebook cells locally; each prints a
rel_erroragainst stored values or numerical gradients. Gradescope is for enrolled students only, so these printed checks are the gate. - Self-grade with the rubric below
How it's graded
Convolution
Pass: Matches F.conv2d in float64 for the tested padding, stride, dilation and group settings.
Great: Gradients also match, and a property test samples random valid settings.
Derivation
Pass: Hand-derived shapes, parameters and multiply-adds match the code.
Great: The gradient-based receptive field matches the derivation, including a dilated layer.
Gated cell
Pass: Outputs match the PyTorch cell with loaded weights.
Great: Gradients through the 20-step unroll match too, and you show that a D2L-convention GRU does not match PyTorch even with the same weights, because the reset gate acts before rather than after the hidden-state matmul.
Leak tests
Pass: Padding and future-input interventions leave valid outputs unchanged within a stated tolerance.
Great: The bidirectional counter-case fails the future-input test, and the padding test covers an all-padding row under a stated convention.
Gradients through time
Pass: Gradient norms by time step are plotted for both cells.
Great: You tie the decay to measured gate values or to norms of the recurrent Jacobian.
Resources
- D2L - convolutional and recurrent architecture routeD2L authors
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.
- Understanding Deep Learning - convolution and residual networksSimon J. D. Prince
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
Use only the neural network, normalization, dropout, convolution and PyTorch components that match this section. Do not complete the course or unrelated vision tasks.
- TrenTorch - convolution cost and recurrent cellsTrenTorch
Solve FLOPs Estimation, GRU Forward and LSTM Forward after the D2L scratch models. Each reference cell matches torch.nn.GRUCell or LSTMCell.
- Deep-ML candidate - convolution and recurrence problemsDeep-ML
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.
- PyTorch nn.Conv2d referencePyTorch
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
Read
pack_padded_sequenceforlengths,enforce_sortedand 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.