Interactive lesson · Convolutional Neural Networks

CNN Layer Lab

Change the stride, the filter, and the padding, and watch every number move. This page walks through a CNN one layer at a time: convolution, ReLU, pooling, and the final flatten, dense and softmax layers that turn feature maps into a prediction.

Tiny VGG, layer by layer

Input channels
Kernel size
Conv stride
Padding
Pooling
Pool size / stride
input convolution ReLU max pooling flatten · dense · softmax

Local connections

Each output value looks at a small patch of the input (the receptive field), not the whole image.

Shared weights

One small filter slides over the whole image, so the same pattern is detected anywhere with only K×K×C weights.

A hierarchy of features

Early layers find edges, later layers combine them into textures, parts and whole objects. Pooling shrinks the maps as depth grows.

01 · Convolutional layer

Stride, kernel size and padding

A convolution slides a kernel (filter) across the input. At each stop it multiplies the overlapping values element by element and adds them up. That single sum becomes one cell of the output, the feature map.

  • Kernel size K: how large a patch each output looks at.
  • Stride S: how many cells the kernel jumps between stops. Stride 2 roughly halves the output.
  • Valid padding: no padding. The kernel stays inside the input, so the output shrinks.
  • Same padding: zeros are added around the border so that the output is ⌈N / S⌉. With stride 1 the output equals the input size.
O = ⌊(N + Pbefore + Pafter − K) / S⌋ + 1

Hover over an output cell to move the kernel there. Hatched input cells are never visited by the kernel, which happens when (N + padding − K) is not divisible by the stride.

Padding
Kernel
02 · Filters and activation

What a filter actually detects

The numbers in a kernel decide what it responds to. A vertical-edge kernel gives large positive values where brightness rises from left to right and large negative values where it falls. A blur kernel averages neighbours, so it removes detail.

In a trained CNN nobody writes these numbers by hand. The network learns them with backpropagation, and a layer has many filters: Tiny VGG uses 10 per layer, VGG-16 uses up to 512. Each filter produces its own feature map, and the maps are stacked into the output channels.

ReLU is applied right after the convolution: ReLU(x) = max(0, x). It keeps positive responses and sets negatives to zero. Without a non-linearity, stacked convolutions would collapse into one linear operation.

Edit any kernel value, try the presets, or load your own photo. Orange is positive, blue is negative.

3×3 kernel
Stride
Padding
Input
Convolution
After ReLU
Max pool 2×2, S=2
strong negative0strong positive

03 · Pooling layer

Pooling shrinks the map and keeps the signal

A pooling layer slides a window over each feature map, just like convolution, but it has no weights to learn. It summarises each window with one number.

  • Max pooling keeps the strongest activation. It answers "was this feature present somewhere in this area?"
  • Average pooling keeps the mean. It is smoother and is used in some architectures.
  • Global average pooling averages a whole map to a single value. Modern networks like ResNet use it before the final dense layer.

Pooling works on each channel separately, so the number of channels stays the same. A 2×2 window with stride 2 halves the width and height and removes 75% of the values, which reduces computation and makes the network tolerant to small shifts.

O = ⌊(N + padding − F) / S⌋ + 1  ·  channels unchanged  ·  0 parameters
Type
Padding
04 · The last layers

From feature maps to a prediction

After the convolution and pooling blocks, the network holds a small stack of feature maps. The last layers turn that stack into one probability per class.

Flatten unrolls the stack (H × W × C) into one long vector. It has no weights. Tiny VGG flattens 13 × 13 × 10 into 1,690 numbers.

Dense (fully connected) computes z = W·x + b. Every input connects to every output, so this is often where most of the parameters live: 1,690 × 10 + 10 = 16,910 in Tiny VGG.

Softmax turns the scores z (logits) into probabilities that add up to 1: pi = ezi / Σ ezj. The predicted class is the largest p. During training, cross-entropy loss compares p with the correct label.

Full forward pass: a 4-class line detector

Draw on the 12×12 input (click and drag). The network is hand-built so you can read every weight: 4 filters (3×3, same padding) → ReLU → max pool 2×2 → flatten 144 → dense 4 → softmax.

Input12 × 12 × 1

Conv + ReLU4 filters → 12 × 12 × 4

Max pool 2×26 × 6 × 4

Flatten6 × 6 × 4 = 144 values in one vector

map 1 (│)map 2 (─)map 3 (╲)map 4 (╱)

Dense + softmax144 → 4 logits → probabilities · 580 parameters

Dense weights for 144 weights reshaped back to 4 maps of 6 × 6. Click a class to switch.

Positive weights reward activity in the class's own map, negative weights penalise the others. A trained network learns weights that vary across positions, but the idea is the same: each output neuron is a template over the flattened features.

05 · Dense layer up close

A dense neuron is a weighted vote

Think of each output neuron as a judge listening to every input.

  • Inputs x are the evidence: the flattened feature values.
  • Weights w say how much the judge trusts each piece of evidence. A large positive weight means "this input supports my answer", a negative weight means "this input argues against it", and a weight near 0 means "ignore it".
  • Bias b is the judge's starting opinion before seeing any evidence.
  • Activation f turns the score into the final output: ReLU for hidden layers, softmax for the last layer.
one neuron: z = w1x1 + w2x2 + … + wnxn + b,  output a = f(z)
whole layer: z = W · x + b

Method to understand it: pick one neuron, change one weight, and watch its score move. Then look at the matrix view. Each row of W is one neuron, so a whole dense layer is just many of these weighted votes computed at once.

Click an output neuron in the diagram to select it. Orange lines are positive weights, blue lines negative; thicker means larger.

Activation
Input values x (from the flatten layer)
Matrix view: the whole layer at once
06 · Counting parameters

How many numbers does the network learn?

A parameter is a number the network adjusts during training: every weight and every bias. Counting them tells you the model's size and memory cost.

One rule for every trainable layer:
parameters = (inputs one neuron sees + 1 bias) × number of neurons

Convolution

One filter sees a K × K patch through all Cin input channels. The number of filters is the number of neurons.

(K × K × Cin + 1) × filters

Dense

One neuron sees every input. There is one neuron per output.

(inputs + 1) × outputs

ReLU, pooling, flatten, softmax

They apply a fixed rule (max, average, reshape, exponentials). Nothing is learned.

0 parameters

Convolution calculator

Dense calculator

Worked examples

LayerCalculationParameters
Tiny VGG conv_1_1(3×3×3 + 1) × 10280
Tiny VGG conv_1_2(3×3×10 + 1) × 10910
Tiny VGG dense(13×13×10 + 1) × 1016,910
VGG-16 first dense(7×7×512 + 1) × 4096102,764,544

Common mistakes

  • Forgetting the bias. Every filter and every dense neuron has exactly one.
  • Forgetting Cin. A 3×3 filter on an RGB image has 3×3×3 = 27 weights, not 9.
  • Using the image size for conv layers. Conv parameters do not depend on the image size or the stride, because the same filter is reused everywhere.
  • Ignoring the dense layer. After flatten, the input count is H × W × C, so the first dense layer often holds most of the parameters.
07 · Putting it together

Shape and parameter calculator

Change the hyperparameters of Tiny VGG and see how every layer's output shape and parameter count responds.

Conv params = (K × K × Cin + 1) × filters
Dense params = (inputs + 1) × outputs
ReLU, pooling, flatten, softmax = 0

Notice how "same" padding keeps maps larger, so the flatten vector and the dense layer grow. That is one reason modern networks replace flatten with global average pooling.

Padding
LayerTypeOutput shapeParameters
Total trainable parameters
08 · Check yourself

Six quick questions

A 32×32 image, a 5×5 kernel, stride 1, valid padding. Output size?

28×28. (32 − 5) / 1 + 1 = 28.

Which padding makes a 3×3, stride-1 convolution keep the input size?

Same padding, P = 1 on every side. In general P = (K − 1) / 2 for odd K.

A 13×13 map through 2×2 max pooling with stride 2?

6×6 with valid padding (the last row and column are dropped), 7×7 with same padding.

How many parameters in a 3×3 conv with 3 input and 16 output channels?

448. (3 × 3 × 3 + 1) × 16. Each filter spans all input channels and has one bias.

How many parameters does a max pooling layer learn?

Zero. It applies a fixed max or average and only reduces size.

Softmax outputs 0.7, 0.2, 0.1. Which class is predicted and what do they sum to?

The first class (argmax), and softmax outputs always sum to 1.