Bird
Raised Fist0
PyTorchml~5 mins

nn.Conv2d layers in PyTorch

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
A Conv2d layer helps a computer see patterns in images by sliding small filters over the picture to find edges, shapes, or colors.
When you want a computer to recognize objects in photos.
When building apps that detect faces or handwriting.
When analyzing medical images like X-rays.
When creating filters for image effects.
When you want to reduce image size but keep important details.
Syntax
PyTorch
nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros')
in_channels is the number of input image channels (e.g., 3 for color images).
out_channels is how many filters you want to apply to the image.
Examples
Creates a Conv2d layer that takes a color image (3 channels) and applies 16 filters of size 3x3.
PyTorch
conv = nn.Conv2d(3, 16, 3)
Creates a Conv2d layer for grayscale images (1 channel) with 32 filters of size 5x5, moving 2 pixels at a time, and adds 2 pixels padding.
PyTorch
conv = nn.Conv2d(1, 32, 5, stride=2, padding=2)
Creates a Conv2d layer with 10 input channels and 20 filters of size 3x5, without adding bias.
PyTorch
conv = nn.Conv2d(10, 20, (3, 5), bias=False)
Sample Model
This code creates a Conv2d layer that takes 1-channel images and outputs 2 channels using 3x3 filters. It applies this layer to a simple 5x5 image with values from 0 to 24. It prints the output shape and values, plus the shapes of the weights and bias.
PyTorch
import torch
import torch.nn as nn

# Create a Conv2d layer
conv = nn.Conv2d(in_channels=1, out_channels=2, kernel_size=3, stride=1, padding=1)

# Create a dummy grayscale image batch: batch size 1, 1 channel, 5x5 pixels
input_tensor = torch.arange(25, dtype=torch.float32).reshape(1, 1, 5, 5)

# Apply the Conv2d layer
output = conv(input_tensor)

# Print output shape and values
print('Output shape:', output.shape)
print('Output tensor:', output)

# Print layer weights shape
print('Weights shape:', conv.weight.shape)
print('Bias shape:', conv.bias.shape)
OutputSuccess
Important Notes
Padding adds pixels around the image edges to keep size after convolution.
Stride controls how far the filter moves each step; bigger stride means smaller output.
Kernel size is the filter size; common sizes are 3x3 or 5x5.
Summary
nn.Conv2d slides filters over images to find patterns.
You set input channels, output channels, and filter size when creating it.
It helps computers understand images by focusing on small parts at a time.

Practice

(1/5)
1. What does the nn.Conv2d layer in PyTorch primarily do?
easy
A. It increases the image size by adding pixels.
B. It slides filters over images to find patterns.
C. It converts images to grayscale.
D. It sorts images by color intensity.

Solution

  1. Step 1: Understand the role of convolution layers

    Convolution layers slide small filters over input images to detect features like edges or textures.
  2. Step 2: Match the function to the options

    Only It slides filters over images to find patterns. correctly describes this sliding filter action, while others describe unrelated image operations.
  3. Final Answer:

    It slides filters over images to find patterns. -> Option B
  4. Quick Check:

    Convolution = sliding filters [OK]
Hint: Conv2d = sliding filters over images to find features [OK]
Common Mistakes:
  • Thinking Conv2d changes image size by adding pixels
  • Confusing Conv2d with image color adjustments
  • Assuming Conv2d sorts or rearranges pixels
2. Which of the following is the correct way to create a Conv2d layer with 3 input channels, 16 output channels, and a 3x3 kernel in PyTorch?
easy
A. nn.Conv2d(3, 16, kernel_size=3)
B. nn.Conv2d(16, 3, kernel_size=3)
C. nn.Conv2d(3, 16, kernel=3)
D. nn.Conv2d(input=3, output=16, size=3)

Solution

  1. Step 1: Recall Conv2d constructor parameters

    The correct order is nn.Conv2d(in_channels, out_channels, kernel_size).
  2. Step 2: Check each option

    nn.Conv2d(3, 16, kernel_size=3) matches the correct parameter order and uses the correct keyword for kernel size. The other options have wrong parameter order or incorrect keywords.
  3. Final Answer:

    nn.Conv2d(3, 16, kernel_size=3) -> Option A
  4. Quick Check:

    Conv2d(in, out, kernel_size) = A [OK]
Hint: Remember Conv2d(in_channels, out_channels, kernel_size) [OK]
Common Mistakes:
  • Swapping input and output channels
  • Using wrong parameter names like 'kernel' instead of 'kernel_size'
  • Passing parameters as keywords not supported by Conv2d
3. What will be the output shape of the following PyTorch Conv2d layer when applied to an input tensor of shape (1, 3, 32, 32)?
conv = nn.Conv2d(3, 6, kernel_size=5)
output = conv(torch.randn(1, 3, 32, 32))
print(output.shape)
medium
A. torch.Size([1, 3, 28, 28])
B. torch.Size([1, 6, 32, 32])
C. torch.Size([6, 3, 28, 28])
D. torch.Size([1, 6, 28, 28])

Solution

  1. Step 1: Calculate output spatial size

    Output size = (Input size - Kernel size + 1) = (32 - 5 + 1) = 28 for both height and width.
  2. Step 2: Determine output channels and batch size

    Output channels = 6, batch size = 1, so output shape is (1, 6, 28, 28).
  3. Final Answer:

    torch.Size([1, 6, 28, 28]) -> Option D
  4. Quick Check:

    Output shape = (batch, out_channels, 28, 28) [OK]
Hint: Output size = input - kernel + 1 if stride=1, padding=0 [OK]
Common Mistakes:
  • Assuming output size equals input size without padding
  • Mixing up input and output channels in shape
  • Forgetting batch size dimension
4. Identify the error in this Conv2d layer definition:
conv = nn.Conv2d(3, 16, kernel_size=3, stride=2, padding=3)
output = conv(torch.randn(1, 3, 28, 28))
print(output.shape)
medium
A. Stride cannot be 2 in Conv2d.
B. Input tensor shape is incorrect for 3 input channels.
C. Padding is too large causing output size to increase unexpectedly.
D. Kernel size must be an odd number.

Solution

  1. Step 1: Calculate output size with given parameters

    Output size formula: floor((Input + 2*padding - kernel_size)/stride) + 1 = floor((28 + 6 - 3)/2) + 1 = floor(31/2) + 1 = 15 + 1 = 16.
  2. Step 2: Understand padding effect

    Padding=3 is large for kernel=3, causing output spatial size to increase unexpectedly, which is unusual and may cause unexpected behavior.
  3. Final Answer:

    Padding is too large causing output size to increase unexpectedly. -> Option C
  4. Quick Check:

    Large padding inflates output size [OK]
Hint: Check padding size relative to kernel size for output shape [OK]
Common Mistakes:
  • Thinking stride=2 is invalid
  • Assuming input shape is wrong for 3 channels
  • Believing kernel size must be odd always
5. You want to design a Conv2d layer that keeps the input image size (28x28) unchanged after convolution with a 5x5 kernel and stride 1. Which padding value should you use?
hard
A. Padding = 2
B. Padding = 1
C. Padding = 0
D. Padding = 3

Solution

  1. Step 1: Use output size formula for Conv2d

    Output size = floor((Input + 2*padding - kernel_size)/stride) + 1. We want output = input = 28, stride=1, kernel=5.
  2. Step 2: Solve for padding

    28 = (28 + 2*padding - 5) + 1 -> 28 = 24 + 2*padding -> 2*padding = 4 -> padding = 2.
  3. Final Answer:

    Padding = 2 -> Option A
  4. Quick Check:

    Padding 2 keeps size with 5x5 kernel [OK]
Hint: Padding = (kernel_size - 1) / 2 for same size [OK]
Common Mistakes:
  • Using zero padding and expecting same size
  • Choosing padding less than 2 for 5x5 kernel
  • Confusing stride effect with padding