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

Convolution shapes: channels, padding, stride, dilation and groups

Every CNN bug report starts with a shape mismatch. Channels, padding and groups decide tensor layout before any learning happens.

You'll be able to

  • Derive output shapes with padding, stride, dilation and groups, including the effective kernel size .
  • State the weight layout and count parameters for plain, grouped and depthwise cases.
  • Implement cross-correlation from loops and match a framework Conv2d on shapes and values.

Covers

Convolution kernels, channels and output shapesPadding, stride, dilation and groups
1

Read

In this order, only the parts listed

0 of 5 done

  1. Cross-correlation, kernels and the feature mapD2L - convolutional and recurrent architecture route

    Section 7.2: The Cross-Correlation Operation (corr2d from scratch), Convolutional Layers (the Conv2D class), Object Edge Detection in Images, Learning a Kernel, and Cross-Correlation and Convolution

  2. Padding and stride shapesD2L - convolutional and recurrent architecture route

    Sections 7.3.1 and 7.3.2: the padding formula (7.3.1), the stride formula (7.3.2), and the comp_conv2d shape demos including kernel with padding and stride

  3. Channels and convolutionsD2L - convolutional and recurrent architecture route

    Section 7.4: multiple input channels, multiple output channels, and convolutions with the reshape-plus-matmul check

    Skip the ResNeXt preview at the end. Grouped cost returns through the PyTorch reference below.

  4. Stride, dilation and channelsUnderstanding Deep Learning - convolution and residual networks

    Chapter 10, Sections 10.2.3 to 10.2.5 (pp. 164 to 165): stride, kernel size, dilation rate as zeros-plus-one, and channel counts by by

  5. The exact shape and groups contractPyTorch nn.Conv2d reference

    Signature and Shape: the floor formula with dilation, the groups divisibility rule with the groups=2 and depthwise examples, and the Variables weight shape

2

Focus on

  • Deep-learning convolution is cross-correlation with no kernel flip. A learned kernel absorbs the flip, so the distinction changes nothing in a trained layer.
  • Output size per axis is . With odd and matching same padding each output centers on its input pixel.
  • Groups split channels into independent convolutions. Parameters and multiply-adds fall by , while output shape is unchanged. Groups equal to is depthwise convolution.
  • A convolution is a per-pixel linear map across channels. Stacked layers need a nonlinearity between them or they fold into one linear map.
Common mistakes · 3
  • Counting padding once in the formula. D2L and PyTorch means padding on both sides, so the total added rows are .
  • Assuming groups change the output shape. Groups change only the weight layout and the cost, not or .
  • Reading convolution as flipped correlation and hand-flipping a learned kernel before reuse. Export the kernel as is.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive that Conv2d(3, 64, kernel_size=7, stride=2, padding=3) on gives with 9,472 parameters. Then implement corr2d from loops and match torch.nn.Conv2d values on a input.
4

Quiz yourself

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

0 got · 0 missed

  1. 1. input, kernel, no padding, stride 1. What is the output shape and why?Answer

    . The 2-wide window fits times per axis; without padding a -wide kernel loses positions per axis.

  2. 2.Conv2d(32, 32, kernel_size=3, groups=8). What is the weight shape and the parameter count with bias?Answer

    : weights plus 32 biases, or 1,184. The dense version needs 9,216 weights plus 32 biases.

  3. 3.Kernel size 3 with dilation 2. What is the effective kernel size and what does it buy?Answer

    5, from . A kernel with dilation 2 sees a window with 9 weights per channel pair, at the multiply-add cost of an ordinary kernel.

  4. 4.Two stacked convolutions with no nonlinearity between them. What is the problem?Answer

    They compose into a single linear map. Keep a layer only with a nonlinearity or normalization separating it from the next linear map.

5

Practice

  • CS231n Assignment 2 Q3: convolution layers in ConvolutionalNetworks.ipynb

    Q3: Convolutional Neural Networks, ConvolutionalNetworks.ipynb in the starter code (https://cs231n.github.io/assignments/2026/assignment2.zip): Convolution: Naive Forward Pass and Naive Backward Pass. Each cell compares your output with expected values or numerical gradients through rel_error.

    Gradescope is for enrolled students; the notebook checks are the gate. Expect relative errors around 1e-8 or smaller.

You're done when

Your loop corr2d matches Conv2d values, and you derive grouped and dilated shapes without notes.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 4 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.

  • 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.

  • 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.

Also in this section

  • 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 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.