✎ Edit content·DAY 044 · POST 4 OF 5 · Code Example

Convolutional Neural Networks

Deep Learning · 11 slides
DAY 044 · POST 4 OF 5
(REMINDER)
DAY 044
Build a CNN in PyTorch
@saurav_dnj_24github.com/SauravDnj · linkedin.com/in/sauravdnj
1 / 11

Theme

Palette

Download

4K — sharpest, slowest
🎬 Video options
Preparing preview…
Live preview · loops the “none” effect
All rendering runs in your browser. No server, no cost, no upload. MP4/WebM = full motion + effects · GIF = lightweight loop · PNG/PDF = static for the Instagram & LinkedIn carousel.

Caption (tap to copy)

📖 Deep dive (full written explanation)

The slides stay clean and scannable. Here's the in-depth explanation behind each one — great for the blog version, show notes, or studying the topic properly.
Slide 1 · Build a CNN in PyTorch

This cover signals a gear change: the previous post traced convolution on paper, this one assembles conv blocks into a real, trainable image classifier. The promise is a complete PyTorch build — load data, define the model, train, evaluate — which is exactly what the code-example angle demands.

Using a small two-conv-block CNN on MNIST is a conscious choice. It is small enough to train in seconds on a laptop and to read in full, yet it exercises every concept from the day: convolution, ReLU, pooling, the feature hierarchy, the flatten, and a dense classifier head. The reader finishes with a pattern they reuse for every CNN afterward.

Slide 2 · 0. Load the data

The data slide front-loads loading and normalization because a CNN cannot train well on raw pixels. ToTensor scales 0-255 pixels into the 0-1 range, and Normalize then centers them around zero, which keeps activations and gradients well behaved. The DataLoader batches and shuffles the data for stochastic training.

Normalization is highlighted deliberately because skipping it is one of the most common silent failures, which post 5 treats as a leading mistake. Doing it correctly here, as the very first step, models the right habit: the transform pipeline is part of the model, not an afterthought, and getting it right is often worth more accuracy than any architecture tweak.

Slide 3 · 1. Define the CNN

This slide defines the network's layers in the constructor, establishing the architecture before the data flows through it. Two Conv2d layers grow the channel count from 1 to 16 to 32 — more filters with depth, matching the feature-hierarchy idea — each with padding 1 so convolution preserves spatial size. A shared MaxPool2d halves the size, and a Linear head classifies.

Separating layer definition from the forward pass mirrors how PyTorch models are structured and makes the shapes explicit. The padding=1 choice connects to post 3's output-size formula: with a 3x3 kernel and padding 1, convolution keeps the size constant, so only the pooling layers change spatial dimensions. That clean separation is what makes the shape arithmetic in the next slides predictable.

Slide 4 · 2. The forward pass

This slide implements the forward pass, the actual sequence of operations on an input batch. Each line is one conv block from post 3: convolution, then ReLU to keep positive responses, then pooling to halve the spatial size. The comments track the size shrinking from 28 to 14 to 7.

The flatten and final Linear are where the feature extractor hands off to the classifier. flatten(1) collapses the 32x7x7 feature volume into a per-image vector while preserving the batch dimension, and the Linear layer maps that vector to 10 class logits. This is the conv-extractor-then-dense-classifier split from post 1, now executed in four readable lines.

Slide 5 · Why 32 * 7 * 7

This explanatory slide addresses the arithmetic most likely to trip up a first CNN: where 32*7*7 comes from. MNIST starts at 28x28. Two 2x2 poolings each halve the spatial size, taking it from 28 to 14 to 7. The final conv layer output 32 channels, so the feature tensor is 32 by 7 by 7, which is 1568 numbers per image.

That 1568 must exactly match the Linear layer's input size, or PyTorch throws a shape error. Walking the size through each pooling step, rather than presenting the number as magic, gives the reader a method they can apply to any architecture. This is precisely the calculation post 5 recommends automating to avoid the most common CNN bug.

Slide 6 · 3. Train the model

This slide implements the training loop, which is identical in structure to the loop from the neural-network and backprop posts — the CNN changes only what model() computes, not how training works. Adam adapts the learning rate per parameter, and CrossEntropyLoss is the standard choice for multi-class classification, combining a softmax and the loss in one call.

The four lines inside the loop are the universal training step: zero the gradients, compute the loss on a batch, backward to fill gradients, and step the optimizer. Printing the loss each epoch lets the reader watch it fall, confirming the network is learning. Seeing that a CNN trains with the exact same loop as any other network is reassuring and correct.

Slide 7 · 4. Evaluate accuracy

This slide evaluates the trained model on held-out test data, the only honest measure of how well it learned. It switches to eval mode, wraps inference in torch.no_grad to skip gradient tracking, takes the argmax of the logits as the predicted class, and counts how many match the true labels.

Two habits are modeled here that post 5 flags as common omissions: calling model.eval() and using no_grad at test time. The expected accuracy around 0.98 gives the reader a concrete target to verify against, turning the abstract build into a result they can reproduce and trust. Evaluating on the separate test set, never the training set, is the discipline that makes the number meaningful.

Slide 8 · The architecture

The network diagram distills the architecture into its channel progression: a single input channel feeds conv1's 16 filters, then conv2's 32 filters, then the 10-way output. It is the bird's-eye view of the layer definitions, showing how representational depth grows as spatial size shrinks.

The diagram intentionally abstracts away the spatial dimensions to highlight the channel story — 1 to 16 to 32 to 10 — which is the part beginners most need to internalize when designing a CNN. More filters at deeper layers gives the network capacity to combine simple patterns into complex ones, the feature hierarchy from post 1 made visible as widening layers.

Slide 9 · What each block does

This slide narrates what each architectural block accomplishes, connecting the code back to the concepts. The first conv block finds simple strokes and halves the image; the second combines strokes into digit parts and halves it again. The flatten turns the resulting feature volume into a vector, and the Linear head scores the 10 classes.

Naming CrossEntropyLoss as what turns those scores into a training signal closes the loop from prediction to learning. This block-by-block reading is what lets a reader adapt the architecture confidently — adding a third conv block, changing channel counts, or swapping the head for a different number of classes — because they understand the role each piece plays rather than copying it blindly.

Slide 10 · The build, in order

The recap orders the build into a reusable template: normalize inputs, repeat conv-ReLU-pool blocks, track the spatial size through each pool, flatten and add a Linear classifier head, and train with Adam and CrossEntropy before evaluating under no_grad. A reader can reproduce the whole workflow from this list.

The emphasis on tracking spatial size through each pool deliberately bakes in the one calculation most likely to go wrong, the same flatten-size issue post 5 leads with. Listing eval with no_grad reinforces the test-time discipline, so the recap quietly carries forward the habits that separate a working pipeline from a buggy one.

Slide 11 · Save this. Follow for Day 45.

The CTA pivots from the working build to the cautionary post. Having built, trained, and evaluated a CNN, the reader is ready to learn how the process goes wrong — shape mismatches, missing normalization, networks too small, broken augmentation, a forgotten eval call, and unaddressed overfitting.

Naming the specific traps in the teaser creates anticipation and signals that the day does not stop at 'it works in a notebook.' Real competence is knowing the failure modes that quietly cap your accuracy, which is exactly the promise of post 5.

🎨 AI image prompt (matches this theme + palette)

Paste into Midjourney, DALL·E, Ideogram, etc. to generate an on-brand image, then upload it on the Edit content page. The prompt updates automatically with the selected theme + palette.