Learn · 5
Convolution
Images are NCHW — batch, channels, height, width, the same layout as
torch. The sister binding used NHWC to work around a TF.js limit; the kernels here
have no such limit, so the layout is not inherited.
One convolution
nn.Conv2d(inC, outC, kernel, stride, padding). Shapes follow the
usual formula, and the quickest way to be sure is to print them.
A small classifier
Convolution, activation, pooling, twice — then flatten and a linear head. This is the shape of every first CNN, and it trains here in a couple of hundred milliseconds.
What the GPU actually did
The device counts what it was asked to do. These numbers do not depend on the adapter — they are decided by the code path, which is why the repository's cost test can run where a wall-clock benchmark cannot.
A conv layer is a bank of kernels
Each output channel is one little learnable filter. Here are six 3×3 kernels at initialisation, drawn — random now; training turns each into a detector for some pattern (an edge, a texture, a colour).
What to remember
Conv2dslides a small learnable kernel across the input; the same weights are reused at every position.- Stride and padding set the output size; pooling downsamples;
flattenhands the map to aLinearhead. - A conv layer is a bank of kernels — each learns to respond to one pattern.
- In borch:
nn.Conv2d(inC, outC, kernel, stride, padding).