Skip to main content

Convolution Operator and Layer Explained in Deep Learning

 

What is a Convolution Layer in Deep Learning?

convolution layer is a building block of Convolutional Neural Networks (CNNs). It's mostly used to process image data.

Instead of connecting every pixel of the input to every neuron (as in a fully connected layer), a convolution layer slides a small filter (kernel) across the image and extracts features like edges, textures, or patterns.

Key Terms

  • Input: The image or feature map (e.g., 6x6 pixels).
  • Kernel(Filter): A small matrix (e.g., 3x3 or 5x5) that moves across the image.
  • Stride: How many steps the filter moves at a time.
  • Padding: Adding extra pixels around the image to control the output size.
  • Feature Map: The result of the convolution operation.

How Convolution Works

Let’s walk through an example with no padding and stride = 1.

1. Input: 6x6 Matrix

Input:
[ [9, 4, 1, 6, 5],
  [1, 1, 1, 0, 2],
  [1, 2, 1, 1, 3],
  [2, 1, 0, 3, 0],
  [1, 4, 2, 5, 6] ]

2. Kernel: 3x3

Kernel:
[ [1, 2, 0],
  [0, 1, 4],
  [1, 0, 1] ]


3. Convolution Operation (3x3)

At each position:

  • Multiply overlapping numbers
  • Sum the result 

Example at top-left corner (first 3x3 area):

Input Patch:
[ [9, 1, 1],
  [1, 1, 1],
  [1, 2, 1] ]

Calculation:
9*1 + 1*2 + 1*0 +
1*0 + 1*1 + 1*4 +
1*1 + 2*0 + 1*1
= 9 + 2 + 0 + 0 + 1 + 4 + 1 + 0 + 1 = 24
**Slide this filter across the whole input to produce the output feature map.


Output Size Formula



Example: 5x5 Kernel

Same 6x6 input, now with 5x5 kernel and no padding:

Only 2x2 positions to apply the kernel.


Why Use Convolution Instead of Fully Connected Layers?

1. Parameter Efficiency

Let’s say you have a 32x32 RGB image (i.e., 32x32x3 = 3072 inputs).

  • Fully Connected Layer (FC):
    Every pixel connects to every neuron.

       With 100 neurons: Parameters=3072x100=307,200

  • Convolution Layer (3x3, 3 input channels, 32 filters):

Parameters per filter=3x3x3=27 Total=27x32=864

  ** Way fewer parameters (307,200 → 864)!

2. Spatial Hierarchy

Convolution keeps spatial info (e.g., nearby pixels are related), while fully connected layers flatten everything.

How to Calculate Parameters in Convolution Layer

For each filter:

equation

Then multiply by number of filters:

equation

Example:

  • Input Channels = 3 (RGB image)
  • Kernel Size = 5x5
  • Number of Filters = 64

equation

How to Calculate MACs

Each kernel application =

equation

Multiply by:

  • Number of output positions (H x W)
  • Number of filters

Example:

  • Input: 32x32x3
  • Kernel: 3x3
  • Filters: 16
  • Output: 30x30 (no padding)

equation

References

  1. Fuhg, J.N., Karmarkar, A., Kadeethum, T.  et al.  Deep convolutional Ritz method: parametric PDE surrogates without labeled data.  Appl. Math. Mech.-Engl. Ed.   44 , 1151–1174 (2023). https://doi.org/10.1007/s10483-023-2992-6.


Comments

Popular

Understanding SentencePiece: A Language-Independent Tokenizer for AI Engineers

In the realm of Natural Language Processing (NLP), tokenization plays a pivotal role in preparing text data for machine learning models. Traditional tokenization methods often rely on language-specific rules and pre-tokenized inputs, which can be limiting when dealing with diverse languages and scripts. Enter SentencePiece—a language-independent tokenizer and detokenizer designed to address these challenges and streamline the preprocessing pipeline for neural text processing systems. What is SentencePiece? SentencePiece is an open-source tokenizer and detokenizer developed by Google, tailored for neural-based text processing tasks such as Neural Machine Translation (NMT). Unlike conventional tokenizers that depend on whitespace and language-specific rules, SentencePiece treats the input text as a raw byte sequence, enabling it to process languages without explicit word boundaries, such as Japanese, Chinese, and Korean. This approach allows SentencePiece to train subword models di...

Mastering the Byte Pair Encoding (BPE) Tokenizer for NLP and LLMs

Byte Pair Encoding (BPE) is one of the most important and widely adopted subword tokenization algorithms in modern Natural Language Processing (NLP), especially in training Large Language Models (LLMs) like GPT. This guide provides a deep technical dive into how BPE works, compares it with other tokenizers like WordPiece and SentencePiece, and explains its practical implementation with Python code. This article is optimized for AI engineers building real-world models and systems. 1. What is Byte Pair Encoding? BPE was originally introduced as a data compression algorithm by Gage in 1994. It replaces the most frequent pair of bytes in a sequence with a single, unused byte. In 2015, Sennrich et al. adapted BPE for NLP to address the out-of-vocabulary (OOV) problem in neural machine translation. Instead of working with full words, BPE decomposes them into subword units that can be recombined to represent rare or unseen words. 2. Why Tokenization Matters in LLMs Tokenization is th...

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It? LangChain  is an open-source framework that simplifies the use of  Large Language Models (LLMs)  like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software. Why Use LangChain for LLM Projects? Chainable Components : Easily build pipelines combining prompts, LLMs, tools, and memory. Multi-Model Support : Work with Gemini, OpenAI, Anthropic, Hugging Face, etc. Built-in Templates : Manage prompts more effectively. Supports Multi-Turn Chat : Manage complex interactions with memory and roles. Tool and API Integration : Let the model interact with external APIs or functions. Let's Walk Through the Code: Gemini + LangChain I will break the code into  4 main parts , each showcasing different features of LangChain and Gemini API. Part 1: Basic Gemini API Call Using LangChain import os from dotenv import load_dotenv load_dot...