NeuralDocs

Types of Neural Networks / Convolutional Neural Networks

Convolutional Neural Networks

Intermediate

CNNs exploit spatial structure using shared filters, making them the backbone of computer vision.

Core building blocks

Wout=⌊Win+2P−D(K−1)−1S⌋+1\boldsymbol{W_{\mathrm{out}}=\left\lfloor\frac{W_{\mathrm{in}}+2P-D(K-1)-1}{S}\right\rfloor+1}

Wᵢₙ is input width, K is kernel size, P is symmetric padding per side, S is stride, and D is dilation. For D = 1 this reduces to ⌊(Wᵢₙ − K + 2P)/S⌋ + 1. Apply the same formula separately to height.

  • Convolution — a small kernel slides over the input producing feature maps.
  • Stride & padding — control output size.
  • Pooling — max/average pooling downsamples feature maps.
  • Fully connected head — maps features to predictions.

Working of a CNN

A CNN transforms an image through a repeating pipeline of stages. Each stage refines the one before it: raw pixels become edges, edges become textures, textures become parts, and parts become whole objects.

1. Input — the image enters as a matrix of pixel values, for example 224 × 224 × 3 for a colour photo (height, width, colour channels).

2. Convolution — learnable filters slide over the input. Each filter is tuned to one pattern, such as a diagonal edge or a corner, and produces a feature map recording where that pattern occurs. Stride and padding control each map's size.

3. Activation — ReLU keeps only positive filter responses, letting the network model non-linear patterns.

4. Pooling — max pooling keeps the strongest response in each small window. This shrinks the map and makes detection robust to small shifts of the object.

5. Classification — after the final block the maps are flattened into one vector and passed through fully connected layers; a softmax turns the output scores into class probabilities.

zij(l)=b(l)+∑c=1C∑u=1K∑v=1KWcuv(l) aiS+u−P,  jS+v−P,  c(l−1)aij(l)=max⁡ ⁣(0,  zij(l))\boldsymbol{\begin{aligned}\mathbf{z}^{(l)}_{ij}&=b^{(l)}+\sum_{c=1}^{C}\sum_{u=1}^{K}\sum_{v=1}^{K}\mathbf{W}^{(l)}_{cuv}\,\mathbf{a}^{(l-1)}_{iS+u-P,\;jS+v-P,\;c}\\\mathbf{a}^{(l)}_{ij}&=\max\!\left(0,\;\mathbf{z}^{(l)}_{ij}\right)\end{aligned}}

ᐅ l indexes layers: a⁽ˡ⁻¹⁾ is the previous layer's activation and a⁽ˡ⁾ the new one. C is the number of input channels and K the kernel size. W⁽ˡ⁾ is the learnable kernel, b⁽ˡ⁾ the bias, S the stride and P the padding. The first line shows one element (i, j) of the feature map; the second is the ReLU that follows. Max pooling then replaces each S×S window of a⁽ˡ⁾ by its maximum.

Convolution: a sliding filter

This single-channel example shows how a small filter turns local pixel patterns into a feature map. The highlighted top-left input window gives the first output value; the same weights are reused at every position.

Input · 4 × 4

slide filter

Filter · 2 × 2

multiply + sum

Feature map · 3 × 3

The amber window produces the amber output cell. Move the same filter one column at a time, then one row down. Each output is the sum of element-wise products. Stride 1 · no padding · bias 0. As in typical CNN libraries, the filter is not flipped (cross-correlation).
1⋅1+2⋅0+5⋅0+6⋅1=7\boldsymbol{1\cdot1+2\cdot0+5\cdot0+6\cdot1=7}

The next window uses 2, 3, 6, and 7, giving 2 + 7 = 9. A 4 × 4 input with a 2 × 2 filter, stride 1 and no padding produces a 3 × 3 output.

Pooling: keep the strongest response

Max pooling operates independently on each feature map. Here, four non-overlapping windows reduce a 4 × 4 map to 2 × 2, with no learnable weights.

Feature map · 4 × 4

maximum per window

Pooled map · 2 × 2

Each shaded 2 × 2 region contributes its largest value to the matching output cell: 6, 8, 14, and 16. Window size 2 × 2 · stride 2 · no padding. Pooling reduces spatial size and leaves the number of channels unchanged.
max⁡{1,2,5,6}=6\boldsymbol{\max\{1,2,5,6\}=6}

Average pooling would instead use the mean: the top-left window would produce (1 + 2 + 5 + 6) / 4 = 3.5.

From image to classification

Follow a handwritten digit through a simple CNN. Filters extract local patterns, pooling compresses the maps, and the classification head combines all features into a prediction.

  1. 01Input image

    28 × 28 × 1

    Grayscale digit

  2. 02Convolution + ReLU

    28 × 28 × 8

    8 filters · 3 × 3 · padding 1

  3. 03Max pooling

    14 × 14 × 8

    2 × 2 windows · stride 2

  4. 04Flatten

    1,568 features

    14 × 14 × 8 → one vector

  5. 05Classification head
    791%
    1: 4%Others: 5%

    10 class probabilities

    Linear layer → softmax

  6. 06Prediction

    Digit 7

    Highest probability

One illustrative digit classifier; dimensions are height × width × channels. Convolution uses stride 1. The head scores digits 0–9; softmax converts scores to probabilities. Probabilities shown are illustrative, not a model run.

Key properties

  • Parameter sharing
  • Local connectivity
  • Translation equivariance
  • Hierarchical features: edges → textures → parts → objects

Landmark architectures

ModelYearContribution
LeNet-51998First practical CNN (digits)
AlexNet2012ReLU, dropout, GPU training
VGG2014Deep stacks of 3×3 convs
ResNet2015Residual / skip connections
EfficientNet2019Compound scaling

Example

nn.Sequential(
    nn.Conv2d(3, 32, kernel_size=3, padding=1), nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.ReLU(),
    nn.AdaptiveAvgPool2d(1), nn.Flatten(),
    nn.Linear(64, 10)
)

Interview questions & answers

  1. Q1. Why are convolutional layers useful for images?

    Answer: They use local receptive fields and reuse the same filter weights across spatial positions. This reduces parameter count compared with dense connections and helps detect patterns wherever they occur. Deeper layers combine local responses into larger-scale features.

  2. Q2. How do stride and padding affect convolution output size?

    Answer: Stride controls how far the filter moves per step; padding adds a border around the input. With dilation 1, a 32-by-32 input, a 3-by-3 kernel, padding 1 and stride 2 produce a 16-by-16 spatial output. Output channels equal the number of filters.

  3. Q3. Is a CNN automatically translation invariant?

    Answer: Not exactly. Shared convolution filters are translation equivariant under suitable boundary conditions: shifting the input shifts the feature map. Striding and boundaries can disrupt this property. Pooling or global aggregation may improve shift robustness, but do not guarantee perfect invariance.