Concepts / Convolutional Neural Networks

Convolutional Neural Networks

A convolutional neural network is best understood as a path from an image to a class prediction.

  • Programming
Interactive lab

Try it: Convolution

How a convolution layer slides a small kernel over an image, multiplying and summing at each position to build a feature map.

How it works

  1. Place the 3×3 kernel over the top-left 3×3 window of the input.
  2. Multiply each input value by the kernel weight on top of it and add the nine products — that sum is one output cell.
  3. Slide the window by the stride and repeat until every position is covered.
  4. Zero padding adds a border so edge pixels are covered too; output size = ⌊(n + 2p − k) / s⌋ + 1.

Default run (11 steps): 5×5 input, 3×3 kernel, stride 1. Output is ⌊(5 + 2×0 − 3) / 1⌋ + 1 = 3 per side. … Done: the kernel visited all 9 positions and produced a 3×3 feature map.

Simplified: One 5×5 single-channel input and one 3×3 kernel. As in deep-learning libraries, the kernel is not flipped (strictly, cross-correlation).

Educational simulation

Loading the simulation…

From Image to Decision

A convolutional neural network is best understood as a path from an image to a class prediction. In the documented dogs-versus-cats task, the network progressively exchanges spatial detail for deeper feature representations. Rather than treating the model as a collection of unrelated lines, trace what happens to the image as it passes through convolution, pooling, and the final classification stages.

ImageinputConv2D3 × 3 kernels, reluMaxPooling2D2 × 2 poolDeeper representationmore channelsBinary outputone sigmoid unitpatterns enterspatial representationrepeat through networkclass decision
What happens as an image passes through alternating convolution and pooling stages and reaches a binary classification output?

Tracing Shape Changes

The model summary provides a shape trace. After each Conv2D and MaxPooling2D stage, inspect two aspects of the representation: spatial size and feature-map depth. The pooling stages help reduce the spatial representation. Meanwhile, the documented filter counts increase through the network, so the number of channels grows as the representation becomes deeper. The result is a deliberate exchange: less spatial detail, but deeper feature representations.

Input imageheight × width × channelsFirst feature mapsspatial layout processedPooled mapsreduced spatialrepresentationDeeper feature mapsmore channelsFurther pooled mapsless spatial detailconvolutionpooling reduces spatial representationdeeper filterspooling continues
How do height, width, and channel depth change as the representation moves through convolution and pooling stages?
Spatial detailbefore poolingReduced spatialdetailafter poolingEarlier channelsearlier representationDeeper channelslater representationpoolingincreasing documented filters
Where is fine-grained spatial information reduced, and where do deeper visual representations accumulate?
Worked example

A Shape-Trace Reading

Suppose the model summary shows several alternating convolution and pooling stages. What should you look for while reading the summary?

  1. 1
    Find the first convolution

    Confirm that the convolution uses the documented 3 × 3 kernels and relu activation.

  2. 2
    Check the following pooling stage

    Look for the spatial representation to be reduced after the MaxPooling2D stage, which uses a 2 × 2 pool size.

  3. 3
    Compare later convolution stages

    Check whether the documented filter counts increase through the network, indicating greater feature-map depth.

  4. 4
    Inspect the later representation

    Confirm that the final dense layers receive the intended flattened representation.

Result

The summary is not merely a list of layers. It is a trace of whether spatial size, feature-map depth, and the transition to the dense layers match the intended architecture.

The Binary Output

The documented task has two classes: dogs and cats. Its final classification configuration therefore uses one output unit with sigmoid activation. This gives the network one final binary decision rather than a separate output unit for each class. The output layer should be interpreted together with the task: the network has been configured to make one decision for a two-class problem.

Finalrepresentationfrom earlier layersOne output unitsigmoidBinary class decisiondogs or catsclassification inputone binary output
How does the final representation become one output for a two-class classification task?

Compilation as a Training Contract

Compilation gives training three distinct instructions. The loss function states how prediction error is evaluated. For this binary task and one-unit sigmoid output, the documented loss is binary crossentropy. The optimizer determines how the model's weights are adjusted; the documented optimizer is RMSprop. Its configured learning rate is 1e-4. The accuracy metric is included so performance can be reported during training. These settings have different jobs: loss evaluates error, the optimizer uses that error to adjust weights, the learning rate controls the configured optimization step size, and accuracy reports performance.

Predictionone sigmoid outputBinary crossentropyevaluates errorRMSpropadjusts weights1e-4learning rateAccuracyreports performanceerror evaluationfeedbackconfigured withperformance report
How do predictions, loss, optimization, learning rate, and accuracy fit together during compilation and training?

Debugging with the Summary

Run the model-building code before trying to memorize every argument. Then inspect the model summary as a debugging checkpoint. It should show the spatial representation shrinking through pooling, the number of channels growing through the documented filter progression, and the dense layers receiving the intended flattened representation. If the result differs from the expected summary, locate the first unexpected shape rather than beginning at the final prediction.

Tensor shapesfind first divergenceParameter countsinspect summaryOne sigmoid unitbinary taskBinary crossentropymatches outputAccuracyreports performancesummary evidencetask alignmenttraining configuration
How can the summary and compilation choices reveal likely architecture or configuration problems?
  • Common mistake

    Starting debugging at the final prediction instead of finding the first unexpected shape.

    What it looks like

    A later tensor shape differs from the expected summary, but earlier layers are not checked.

    Why it fails

    The first divergence identifies the most efficient place to begin debugging.

    Do this instead

    Read the summary from the input forward and inspect the first unexpected shape.

  • Common mistake

    Treating spatial size and feature-map depth as the same property.

    What it looks like

    A learner sees pooling reduce the spatial representation and concludes that every part of the representation is shrinking in the same way.

    Why it fails

    Pooling reduces spatial representation, while documented filter counts increase through the network.

    Do this instead

    Track spatial size and channel depth separately.

  • Common mistake

    Choosing compilation settings without checking the classification task.

    What it looks like

    The final layer, loss, optimizer, and metric are considered unrelated settings.

    Why it fails

    The one-unit sigmoid output and binary crossentropy loss are documented together for the binary task, while RMSprop and accuracy serve different training roles.

    Do this instead

    Check output units and activation, loss, optimizer, learning rate, and metric as one configuration.

Best practice

Use the model summary as evidence, not decoration. A correct-looking final output does not prove that the intermediate architecture is correct. The first unexpected shape, the progression of channels, and the transition into the flattened representation provide a more useful debugging trail.

Practice Trace

MEDIUM

Imagine that a model summary does not match the intended convnet. Describe the order in which you would inspect the architecture and compilation configuration.

Hints
  • Begin with the first unexpected tensor shape.
  • Check whether pooling reduced the spatial representation.
  • Check whether documented filter counts increase through the convolution stages.
  • Confirm the flattened representation reaches the dense layers.
  • Finally, compare the binary output, loss, optimizer, learning rate, and accuracy metric.
  1. 1
    A convnet for the documented binary image task alternates Conv2D layers using 3 × 3 kernels and relu activation with MaxPooling2D stages using a 2 × 2 pool size. Pooling reduces spatial representation, while increasing documented filter counts deepen the feature maps. The network ends with one sigmoid output unit because the task has two classes. During compilation, binary crossentropy evaluates error, RMSprop adjusts weights with a learning rate of 1e-4, and accuracy reports performance. The model summary helps locate problems by revealing the first unexpected shape and checking the transition to the flattened dense representation.

At a glance

  • Alternating Conv2D and MaxPooling2D stages transform an image into a deeper representation for classification.
  • Pooling reduces spatial representation, while documented filter counts increase feature-map depth.
  • A two-class task uses one sigmoid-activated output unit and binary crossentropy in the documented configuration.
  • RMSprop is the optimizer, its configured learning rate is 1e-4, and accuracy reports performance.
  • The model summary is a debugging checkpoint: find the first unexpected shape before investigating later layers.

Related concepts

More concepts starting with C