3.4.6 · D3Convolutional Neural Networks

Worked examples — LeNet and AlexNet

2,572 words12 min readBack to topic

This page is a workout. The LeNet and AlexNet parent note gave you the two architectures and one master formula. Here we use that formula until it can no longer surprise you. We will hit every kind of case a convolution/pooling problem can throw at you — clean divisions, floor-truncation, padding tricks, degenerate outputs, parameter counting, and a couple of exam-style traps.

Before any numbers, let us re-earn the single tool that runs this whole page.

Let us say what each piece means, because we will lean on all of them.

  • = input side length — how wide the picture is, in pixels.
  • = kernel side length — the width of the little sliding stamp.
  • = padding — a border of zeros we optionally wrap around the picture so the stamp can reach the edges.
  • = stride — how far the stamp hops between stamps. means "touch every pixel"; means "skip 3, land on the 4th".
  • = the floor, meaning "throw away the fractional part, round down". .

The scenario matrix

Every convolution/pooling question you will ever meet falls into one of these cells. The six examples that follow are each tagged with the cell(s) they cover, so you can see the whole space is filled.

Cell Case class What makes it tricky Covered by
A Clean divide ( divisible by ) Nothing — warm-up Ex 1
B Floor actually bites (remainder ) Must truncate down Ex 2
C Padding used to preserve size "same" convolution Ex 3
D Degenerate output Conv acts like fully-connected Ex 4
E Zero-stride-1 pooling chain Repeated shrinking Ex 4
F The "224 vs 227" trap Hidden padding Ex 5
G Parameter counting (weights + biases) Channels multiply in Ex 6
H Activation edge case (ReLU/tanh sign) Sign of input decides gradient Ex 7
I Real-world word problem Translate English → Ex 8

Example 1 — Cell A: the clean divide (LeNet C1)

Forecast: guess the number before reading on. (Hint: the stamp is 5 wide, so it loses a 4-pixel margin.)

  1. Identify the four numbers. , , , . Why this step? The formula is useless until every symbol has a value; naming them prevents mixing up kernel and stride.
  2. Plug into the ruler. Why this step? measures how far the stamp's left edge can travel before its right edge falls off; dividing by counts the hops.
  3. Attach the depth. 6 filters ⇒ output is . Why this step? Each filter produces one 2-D map; depth equals the filter count, never the input channels.

Verify: count fence posts directly. Positions the stamp's left edge can occupy: — that is slots. Matches. This is Cell A because divides exactly; floor changed nothing.


Example 2 — Cell B: when the floor actually bites (AlexNet Conv1, naive)

Forecast: will divide evenly by 4? Guess the leftover.

  1. Numbers: , , , .
  2. Numerator first. . Why this step? Isolating the numerator lets us see the remainder before dividing.
  3. Divide and floor. Why this step? ; that stray pixel is a strip too thin for another hop, so it is discarded — this is exactly the case where floor matters.
  4. Add one. . Output .

Verify: , but , so 53 hops is the most that fit. The remainder pixel is lost. This is Cell B: had we used the answer would be , but the coarse stride throws away information — which is precisely why the parent note warns the "224" number is a simplification. (Ex 5 fixes it.)


Example 3 — Cell C: padding to preserve the size (AlexNet Conv2)

Forecast: the parent note claims Conv2 keeps . Can padding really cancel the kernel's shrinkage?

  1. Numbers: , , , .
  2. Why specifically? A stamp loses pixels of margin total (2 on each side). Adding zeros to each side donates back exactly those 4 pixels. Why this step? This is the "same-padding rule": choose for stride 1 to keep size fixed.
  3. Plug in.

Verify: in, out — confirmed. Look at the figure: the violet zero-border lets the stamp centre reach the corner pixel, which without padding it never could. This is Cell C. Contrast with VGGNet, which uses this trick on every layer so only pooling ever changes the size.


Example 4 — Cells D & E: shrinking to a single pixel (LeNet C3 → S4 → C5)

Forecast: predict all three output sizes before computing.

  1. C3 convolution. : Why this step? Same ruler as always — a conv layer never gets special treatment.
  2. S4 pooling. Pooling uses the same formula with , (Cell E, repeated shrink): Why this step? A window hopping by 2 tiles the image into non-overlapping blocks — it halves each side. See Pooling Layers.
  3. C5 convolution. : Why this step? When the stamp fits in exactly one spot — the degenerate Cell D. The convolution now touches every input pixel at once, so it behaves identically to a fully-connected layer. That is why the parent note writes "(effectively FC)".

Verify: Chain . Each transition satisfies its formula, and the final is the smallest legal output ( always, since here). Note would mean the kernel is bigger than the input — an illegal layer; the design avoids it exactly.


Example 5 — Cell F: the "224 vs 227" trap

Forecast: where could one extra pixel of output come from?

  1. State the discrepancy. Formula on : output (Ex 2). Paper: . Off by one.
  2. Hypothesise hidden padding. Suppose the true effective input is , i.e. an implicit that makes . Why this step? One extra output pixel needs the numerator to grow by (since each hop of stride 4 adds one output). , and ; combined with the border this pushes past the next multiple of 4.
  3. Recompute with . Why this step? exactly — no floor loss this time — so we cleanly reach .

Verify: and , so hops, . Cell F resolved: the "" is the crop size before an implicit 1.5-pixel-per-side pad rounds the effective field to . Moral: always ask what the effective, post-padding input is.


Example 6 — Cell G: counting parameters (weights + biases)

Forecast: guess the order of magnitude — thousands? hundreds of thousands?

  1. Weights of one filter. A single filter spans all input channels, so its weight count is Why this step? A filter is a 3-D block: it sees every input channel at each of its positions. Forgetting the is the classic error.
  2. All filters. filters ⇒ weights. Why this step? Each output channel has its own independent filter.
  3. Biases. One bias per output channel: .

Verify: dimensional sanity — weights scale as , which is indeed ; biases equal . Cell G complete. This is why the FC layers (see Ex, and Dropout) dominate AlexNet's ~60M total — a single layer alone has M weights.


Example 7 — Cell H: the activation sign edge case

Forecast: where does each function stop learning?

  1. tanh gradient rule: .
    • : , gradient — nearly dead (saturated).
    • : gradient — maximal.
    • : , gradient — dead. Why this step? Large pushes flat, so its slope — the vanishing gradient the parent note warns about.
  2. ReLU gradient rule: if , else .
    • : gradient (dead, but only on the negative side).
    • : gradient (boundary, conventionally 0).
    • : gradient — full-strength, no matter how large grows. Why this step? ReLU never saturates on the positive side; this is the whole speed advantage.

Verify: the figure overlays both slopes. Notice tanh dies at both extremes while ReLU stays alive for all — this is Cell H, the sign-dependent case. The negative-side death of ReLU is the "dying ReLU" trade-off; see Activation Functions and Backpropagation for how these gradients chain.


Example 8 — Cell I: a real-world word problem

Forecast: roughly, to shrink 256 → 64 you divide by 4, so stride ≈ 4. Let us pin it exactly.

  1. Translate English to symbols. , , , target . Unknown: . Why this step? Word problems fail when you skip this translation; write the four slots and mark the blank.
  2. Rearrange the ruler (ignore floor for a first estimate): Why this step? Solving for tells us which stride would land on 64 if the divide were clean.
  3. Test the nearest integer . Why this step? undershoots to 63 because floor eats the remainder — we do not get 64.
  4. Fix it with padding. We need one more output pixel, i.e. numerator up to . Add (border of 2 each side): numerator , . ✓ Why this step? Padding is the honest lever when stride alone can't hit the target — this mirrors the 227-trick from Ex 5.

Verify: with : . Target met. Cell I closed: a plain-English requirement became four numbers and one padding tweak.


Recall Self-test (reveal after guessing)

LeNet C1 output side for , , stride 1, pad 0 ::: AlexNet Conv1 naive output on literal input ::: AlexNet Conv1 output on effective input ::: LeNet C5 spatial output when ::: Parameter count of AlexNet Conv2 (, ) ::: Stride+padding to turn with ::: stride , padding tanh gradient magnitude at :::


See also: Convolutional Layers · Pooling Layers · Batch Normalization · ResNet · ImageNet Dataset · Image Classification · Transfer Learning · Object Detection · 3.4.06 LeNet and AlexNet (Hinglish)