7 routes42 sections324 units

Unit 4.1 · about 5 h

193 / 324

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.

  • derive
  • implement

You can now

  • Derive output shapes with padding, stride, dilation and groups, including the effective kernel size \(d(k-1)+1\).
  • State the weight layout \((C_{\text{out}}, C_{\text{in}}/G, kH, kW)\) 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 shapes
  • Padding, stride, dilation and groups

Study, in this order

5 steps

  1. Cross-correlation, kernels and the feature map
    D2L - 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 shapes
    D2L - 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 \((3, 5)\) with padding \((0, 1)\) and stride \((3, 4)\)

  3. Channels and \(1 \times 1\) convolutions
    D2L - convolutional and recurrent architecture route

    Section 7.4: multiple input channels, multiple output channels, and \(1 \times 1\) convolutions with the reshape-plus-matmul check

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

  4. Stride, dilation and channels
    Understanding 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 \(Ci\) by \(Co\) by \(K\)

  5. The exact shape and groups contract
    PyTorch 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

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 \(\left\lfloor (n + 2p - d(k-1) - 1)/s + 1 \right\rfloor\). With odd \(k\) and matching same padding each output centers on its input pixel.
  • Groups split channels into \(G\) independent convolutions. Parameters and multiply-adds fall by \(G\), while output shape is unchanged. Groups equal to \(C_{\text{in}}\) is depthwise convolution.
  • A \(1 \times 1\) convolution is a per-pixel linear map across channels. Stacked \(1 \times 1\) layers need a nonlinearity between them or they fold into one linear map.

Reproduce closed-book

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

Traps

  • Counting padding once in the formula. D2L and PyTorch \(p\) means padding on both sides, so the total added rows are \(2p\).
  • Assuming groups change the output shape. Groups change only the weight layout and the cost, not \(H_{\text{out}}\) or \(W_{\text{out}}\).
  • Reading convolution as flipped correlation and hand-flipping a learned kernel before reuse. Export the kernel as is.

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.

Self-test

  1. \(3 \times 3\) input, \(2 \times 2\) kernel, no padding, stride 1. What is the output shape and why?

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

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

  4. Two stacked \(1 \times 1\) convolutions with no nonlinearity between them. What is the problem?

Completion criterion

Stop when

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

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

Leads to

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

Also in this section

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