3.4.12 · D4Convolutional Neural Networks

Exercises — Image classification pipeline

2,340 words11 min readBack to topic

This page tests everything in the parent pipeline note: preprocessing, the forward pass, softmax, and cross-entropy loss. Each problem is graded by cognitive level. Every symbol you meet was defined in the parent — but where a shortcut hides, this page rebuilds it from scratch.

A quick reminder of the three quantities that appear again and again:


Level 1 — Recognition

Exercise 1.1

A grayscale pixel has raw value . The dataset mean is and standard deviation is . Compute the normalized value .

Recall Solution

WHAT we do: plug numbers into the normalization formula from the parent note. WHY: normalization re-centers data on and rescales spread to so no single feature dominates the loss. The value means "this pixel sits standard deviations above the mean" — bright relative to typical pixels.

Exercise 1.2

A network's final layer outputs logits for classes. Without a calculator, state each softmax probability.

Recall Solution

WHAT: apply softmax to three identical logits. WHY: when all logits are equal, exponentiating and dividing must treat every class the same — symmetry. Every class gets . The pie is split into three equal slices.

Exercise 1.3

Match each stage of the pipeline to its job: (a) Preprocessing, (b) Feature extraction, (c) Classification, (d) Postprocessing.

Recall Solution
  • (a) Preprocessing → resize + normalize the raw pixels so all images share one scale.
  • (b) Feature extraction → convolution + pooling learn edges → textures → parts.
  • (c) Classification → fully connected layers + softmax map features to class probabilities.
  • (d) Postprocessing → apply the argmax and a confidence threshold to produce a final label.

Level 2 — Application

Exercise 2.1

Logits for classes are . Compute and exactly, then numerically.

Recall Solution

WHAT: two-class softmax. WHY exponentials and not raw division? is always positive (so probabilities can never go negative) and it amplifies gaps — a logit gap of becomes a probability ratio of . Check: . ✓

Exercise 2.2

The true class is class . Using from Ex 2.1, compute the cross-entropy loss (natural log).

Recall Solution

WHAT: feed the correct-class probability into the surprise meter. WHY only one term? The label is one-hot: , . Every term with vanishes, so only the true class survives. Small loss → the network was confidently correct. Good.

Exercise 2.3

An RGB pixel's red channel is . Using ImageNet stats () on the -scaled pixel, compute the normalized value.

Recall Solution

WHAT: scale to first (divide by ), then normalize — this is the exact recipe Transfer learning with ImageNet weights expects. Matching this distribution is why Batch normalization-trained backbones transfer cleanly.


Level 3 — Analysis

Exercise 3.1

Logits are . Compute the softmax probability of the top class (index 0). Then explain why the largest logit "dominates" the denominator.

Recall Solution

WHAT: four-class softmax, top class. Sum . WHY dominance: because grows exponentially, a logit that is only larger than the next produces a term times bigger. The largest term alone is of the sum, so the denominator is approximately the top term — this is the " confidence" behaviour the parent example described.

Exercise 3.2

Show softmax is shift-invariant: adding a constant to every logit leaves all probabilities unchanged. Verify with and .

Recall Solution

WHAT: prove . The common factor cancels top and bottom. WHY it matters: implementations subtract before exponentiating to avoid overflow (computing would crash) — and this proof shows that trick changes nothing. Check with , : Identical to Ex 2.1. ✓

Exercise 3.3

A dog image gives correct-class probability ; a mislabelled-looking image gives . Compute both losses and quantify how much more the wrong prediction is penalized.

Recall Solution

Ratio . WHY the huge gap: shoots to as . Being confidently wrong is punished far more than being confidently right is rewarded — this asymmetry is what pushes gradient descent (via Backpropagation) away from disastrous mistakes first.


Level 4 — Synthesis

Exercise 4.1

A batch of images each of shape passes through conv1 ( kernel, stride , output channels) producing a map per image. Using output width with input , kernel , stride , and padding , confirm the output spatial size is .

Recall Solution

WHAT: apply the convolution output-size formula with padding on each side, so effective input . WHY padding: without the map would shrink below and lose border information; padding preserves the intended used by ResNet-50. Full batch output tensor: .

Exercise 4.2

The same -dimensional feature vector feeds a final layer with . For single-label classification we apply softmax; for Multi-label classification we apply an independent sigmoid to each logit. For logit , compute both the softmax-style ratio partner () and the sigmoid, and explain the conceptual difference.

Recall Solution

Sigmoid: . Softmax for (Ex 2.1) too — numerically equal here because two-class softmax is a sigmoid of the logit difference. Conceptual difference: softmax forces the probabilities to compete (they must sum to — "exactly one class"). Sigmoid treats each class independently (each answer is its own yes/no — "cat AND outdoors AND sunny" can all be true). WHY it matters: choose softmax + cross-entropy for mutually exclusive labels; choose sigmoids + per-class loss for multi-label tasks like Object detection attributes.

Exercise 4.3

Data augmentation horizontally flips an image with probability and does nothing otherwise. If a training set has images and we draw one augmentation per image per epoch, what is the expected number of flipped images seen in one epoch, and why does this help generalization?

Recall Solution

Expected flips . WHY it helps: a horizontally flipped dog is still a dog. Showing both orientations teaches the network that class identity is invariant to left-right mirroring, enlarging the effective dataset without collecting new photos — reducing overfitting exactly as pooling's spatial invariance does inside the network.


Level 5 — Mastery

Exercise 5.1

Derive the gradient of the cross-entropy loss with respect to a logit for the softmax + cross-entropy pair. Show that , then evaluate it for with true class .

Recall Solution

WHAT: compute how loss changes as we nudge one logit — the signal Backpropagation sends backward. Setup: , with . Step 1 — softmax derivative. For the same index: . For a different index : . WHY these two cases: raising makes class 's slice grow but steals probability from every other slice, hence the negative cross-terms. Step 2 — chain rule. Step 3 — collapse the label sum. Since (one-hot), , so . Beautifully simple: the gradient is just "predicted minus true". Evaluate for , true class : ; and . Sign check: should increase (negative gradient → gradient descent moves up), should decrease. Exactly what we want.

Exercise 5.2

A confidence threshold rejects any prediction whose top probability is below as "uncertain". Given logits , decide whether the network abstains.

Recall Solution

Since , the pipeline outputs "uncertain" and abstains. WHY thresholding exists: on out-of-distribution or ambiguous images the logits are close together, softmax spreads probability thin, and a low peak is an honest signal that no class is trustworthy.

Exercise 5.3 (capstone)

End-to-end: a dog image is normalized, produces logits (class 0 = dog is true). Compute (a) , (b) the loss , (c) the output-layer gradient , and (d) state the final decision at threshold .

Recall Solution

(a) From Ex 3.1: . (b) — low loss, confidently correct. (c) — a small negative nudge, because the network is already nearly right; little learning left on this example. (d) output "dog" with confidence. This is the whole pipeline in four numbers: normalize → softmax → loss → gradient → decision.


Recall Self-test recap

Softmax over equal logits ::: each probability is Loss when correct-class probability is (natural log) ::: The softmax+cross-entropy logit gradient ::: ("predicted minus true") Why subtract before softmax ::: shift-invariance means it changes nothing but prevents overflow Softmax vs sigmoid head choice ::: softmax for mutually exclusive classes (sum to 1); sigmoids for independent multi-label