Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

LeNet-5: Architecture and Tensor Flow

Introduction

In our last lesson, we explored the fascinating emergent property of feature hierarchies in CNNs. We saw how stacking simple convolutional layers allows a network to learn representations of increasing complexity, from edges and colors to object parts and entire objects. We also established the crucial role of the receptive field in this process.

Now, it's time to ground these abstract concepts in a concrete, historical example. This lesson moves from the what and why to the how, by dissecting one of the most influential early CNNs: LeNet-5. Proposed by Yann LeCun et al. in 1998, this architecture was a trailblazer for document recognition and laid the groundwork for modern deep learning in computer vision. We will perform a systematic, layer-by-layer trace of the data flow, calculating the shape of the data tensors at each step. This process is akin to tracing signals through a circuit or data through a low-level system, allowing us to build a first-principles understanding of an end-to-end vision network.

Part 1: The Mathematics of Tensor Shapes

Before we start our dissection of LeNet-5, we must have our primary tool ready: the formula for calculating the output dimensions of a convolutional or pooling layer. In the previous lesson, we focused on the receptive field, which looks backward from an output neuron to the input. Now, we will look forward to determine the shape of the output volume.

The key insight is that the spatial dimensions (width and height) and the depth (channels) of the output are calculated differently.

A Comprehensible Explanation of the Dimensions in CNNs

This article from Towards Data Science provides an exceptionally clear breakdown of how to calculate output shapes. It methodically separates the calculation of spatial dimensions from the calculation of depth.

First, read the section "Calculate the width and height of the output array". Focus on understanding Equation 2, which provides the general formula for the output size of a single spatial dimension: o = \bigg\lfloor \frac{i - k + 2p}{s} \bigg\rfloor + 1 where i is the input size, k is the kernel size, p is the padding, and s is the stride. The floor function (\lfloor \cdot \rfloor) is crucial. Next, study "Calculate the depth of the output array". This is the most critical part. Pay close attention to the two rules it establishes, summarized in Equation 8 and Equation 9. These rules articulate a fundamental concept: The depth of a kernel must match the depth of the input volume (d_k = d_{in}). The depth of the output volume is determined by the number of filters (kernels) you apply (d_{out} = n_k).

With this mathematical framework, we are now equipped to trace any CNN architecture.

Part 2: Dissecting LeNet-5

We will now walk through the LeNet-5 architecture layer by layer. For each step, we'll note the input shape, the layer's parameters, and use our formula to calculate the output shape.

The following video provides an excellent visual walkthrough of this exact process. We will use it as our guide.

LeNet LeNet Architecture CNNs

This "Code With Aarohi" video gives a clear, step-by-step explanation of the LeNet-5 architecture and the tensor shape calculations.

Watch the video from the beginning to understand the context and the input data specifications. We'll then proceed layer by layer.

Layer 0: Input

The input to LeNet-5 is a 32x32x1 single-channel (grayscale) image. The original MNIST digits are 28x28, but they were placed in a 32x32 canvas. This padding ensures that features like corners and stroke endpoints, which might be at the edge of the 28x28 box, can fall into the center of a kernel's receptive field in the first layer.


Layer C1: First Convolutional Layer

This diagram illustrates the first convolutional layer, C1. It takes a 32x32 input and applies 6 different 5x5 kernels to produce 6 feature maps, each of size 28x28.
  • Input Shape: 32x32x1
  • Operation: Convolution
  • Parameters:
    • Number of filters (): 6
    • Kernel size (): 5x5
    • Stride (): 1
    • Padding (): 0
  • Calculation:
    • Spatial Dimensions: Using our formula, the output width and height are:
    • Depth: The output depth is equal to the number of filters.
  • Output Shape: 28x28x6

You can verify this by watching the corresponding segment in the video guide.

LeNet LeNet Architecture CNNs

Watch the segment explaining the first convolutional layer. The presenter walks through the parameters and the resulting output shape. Then, see the mathematical derivation which applies the formula we just learned.


Layer S2: First Subsampling (Pooling) Layer

This diagram shows the S2 subsampling layer. It reduces the spatial dimensions of the 6 feature maps by half, from 28x28 to 14x14, using a 2x2 pooling window with a stride of 2.
  • Input Shape: 28x28x6
  • Operation: Subsampling (Average Pooling). Modern architectures typically use Max Pooling.
  • Parameters:
    • Kernel size (): 2x2
    • Stride (): 2
    • Padding (): 0
  • Calculation:
    • Spatial Dimensions:
    • Depth: Pooling operates on each channel independently, so the depth remains unchanged.
  • Output Shape: 14x14x6

The video guide confirms this calculation.

LeNet LeNet Architecture CNNs

Watch the section on the first pooling layer and its corresponding mathematical calculation. Notice how the stride of 2 is the key to halving the spatial dimensions.


Layer C3: Second Convolutional Layer

  • Input Shape: 14x14x6
  • Operation: Convolution
  • Parameters:
    • Number of filters (): 16
    • Kernel size (): 5x5
    • Stride (): 1
    • Padding (): 0
  • Calculation:
    • Spatial Dimensions:
    • Depth: The output depth is the number of new filters.
  • Output Shape: 10x10x16

A Note on Connectivity: In the original 1998 paper, this layer used a complex, sparse connection scheme to reduce parameters and break symmetry. Not all 16 output feature maps were connected to all 6 input feature maps. Modern implementations, optimized for GPUs, typically ignore this and use a standard "fully connected" convolution where each of the 16 filters has a depth of 6 and is convolved across all input channels.

You can watch the video explanation for this layer.

LeNet LeNet Architecture CNNs

Watch the segment on the second convolutional layer.


Layer S4: Second Subsampling (Pooling) Layer

  • Input Shape: 10x10x16
  • Operation: Subsampling (Average Pooling)
  • Parameters:
    • Kernel size (): 2x2
    • Stride (): 2
    • Padding (): 0
  • Calculation:
    • Spatial Dimensions:
    • Depth: Unchanged.
  • Output Shape: 5x5x16

This is the final output of the convolutional "backbone" of LeNet-5. At this point, the network has transformed the 32x32 image into 16 abstract feature maps, each of size 5x5.

LeNet LeNet Architecture CNNs

Watch the brief explanation of the second pooling layer.


Layers C5, F6, and Output: The Classifier Head

After feature extraction, the network must perform classification. This is handled by a series of layers that transition from 3D tensors to a final 1D vector of class probabilities.

  1. Layer C5 (Convolution as a Fully Connected Layer):

    • Input Shape: 5x5x16
    • Operation: Convolution
    • Parameters: 120 filters, 5x5 kernel, stride 1, padding 0.
    • Output Shape: Let's calculate: . The output is 1x1x120.
    • Interpretation: A convolution with a kernel size equal to the input size results in a 1x1 output for each filter. This is functionally equivalent to flattening the 5x5x16 input into a vector of length 400 () and applying a fully connected layer with 120 output neurons.
  2. Flattening: The 1x1x120 tensor is flattened into a vector of 120 elements.

  3. Layer F6 (Fully Connected Layer):

    • Input: 120 neurons
    • Operation: Fully Connected (matrix multiplication + bias)
    • Output: 84 neurons. This number was chosen for a historical reason related to the bitmap representation of characters on a terminal.
  4. Output Layer (Fully Connected Layer):

    • Input: 84 neurons
    • Operation: Fully Connected
    • Output: 10 neurons, one for each digit class (0-9).
    • Activation: A softmax function is applied to these 10 outputs to convert them into a probability distribution.

The video guide concludes with an explanation of these final layers.

LeNet LeNet Architecture CNNs

Watch the final segment on the fully connected layers and the output. This explains the transition from the 3D feature maps to the 1D classification vector.

The GeeksForGeeks article LINK also provides a concise table summarizing the entire architecture, which is a great reference.

Part 3: Parameter Efficiency

Your systems background will appreciate the importance of efficiency. One of the primary motivations for convolution is parameter sharing, which dramatically reduces the number of trainable weights compared to using fully connected layers on images. Let's quantify this.

Socratic Question:

  1. Consider layer C3, which takes a 14x14x6 input and produces a 10x10x16 output using 5x5 kernels. Assuming a modern implementation (each output channel connects to all input channels), calculate the total number of trainable parameters (weights + biases) for this layer.
  2. Now, imagine you tried to achieve a similar mapping using only a fully connected layer. It would take an input vector of size and produce an output vector of size . How many trainable parameters would this FC layer require?
  3. What does the enormous difference in these two numbers tell you about the fundamental operating principle of convolutional layers?
Answer 1. **Convolutional Layer (C3) Parameters:** * Each of the 16 filters has a shape of $5 \times 5 \times 6$ (kernel width x kernel height x input depth). * Number of weights per filter: $5 \times 5 \times 6 = 150$. * Each filter also has 1 bias term. So, $150 + 1 = 151$ parameters per filter. * Total parameters for 16 filters: $151 \times 16 = \mathbf{2,416}$.
  1. Fully Connected Layer Parameters:

    • Input neurons: .
    • Output neurons: .
    • Number of weights: .
    • Number of biases: .
    • Total parameters: .
  2. Conclusion: The convolutional layer requires almost 800 times fewer parameters to perform a similar input-output mapping. This staggering efficiency comes from two principles:

    • Parameter Sharing: The same filter is reused (slid across) all spatial locations of the input. An FC layer, in contrast, learns a unique weight for every single input-output neuron connection.
    • Locality: A convolutional layer assumes that features are local. An output neuron is only affected by its local receptive field, not by pixels on the other side of the image. An FC layer breaks this assumption, connecting every output to every input.

Conclusion

By tracing LeNet-5, we have solidified our understanding of how the core components of a CNN—convolution, padding, stride, and pooling—work together in a complete system. We have seen firsthand how an architecture progressively reduces spatial dimensions while increasing feature depth, transforming a grid of pixels into a set of abstract, class-relevant features.

Key Takeaways:

  • Architectural Pattern: LeNet-5 established the classic CNN pattern of stacking convolutional and pooling layers to extract features, followed by fully connected layers for classification.
  • Tensor Shape Calculation: The shape of tensors changes predictably at each layer, governed by simple mathematical formulas relating input size, kernel size, stride, and padding.
  • Dimensionality Trade-off: CNNs typically decrease height and width while increasing the number of channels (depth). This transforms low-level spatial information into high-level feature information.
  • Parameter Efficiency: The use of parameter sharing in convolutional layers offers a monumental reduction in model size compared to using fully connected layers on raw pixels.

Preview of the Next Lesson:

While LeNet-5 was revolutionary, as networks grew deeper, a new problem emerged: the degradation problem, where adding more layers actually hurt performance. In our next lesson, we will investigate why this happens and explore the simple yet profound architectural innovation that solved it: residual connections, as introduced in the groundbreaking ResNet architecture.

Can't find a good explanation? Sign up and we'll make it for you

Sign up