Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Class

nn.Conv2d

torch.nn.Conv2d applies a learnable 2D cross-correlation to channel-first image-like tensors. Stride, padding, dilation, and groups control spatial sampling and channel connectivity.

How it works

Conv2d learns a bank of two-dimensional filters and applies cross-correlation across spatial dimensions. It accepts batched channel-first tensors, while stride, padding, dilation, and groups control sampling, borders, receptive-field spacing, and channel connectivity; the resulting spatial size follows the documented floor-based formula.

Syntax

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros', device=None, dtype=None)

Parameters

NameTypeDescription
in_channelsintNumber of channels in the input image.
out_channelsintNumber of channels produced by the convolution.
kernel_sizeint or tupleSize of the convolving kernel.
strideint or tupleStride of the convolution; default 1.
paddingint, tuple, or strPadding added to all four sides of the input; default 0.
dilationint or tupleSpacing between kernel elements; default 1.
groupsintControls connections between inputs and outputs; default 1.
biasboolIf true, adds a learnable bias; default true.
padding_modestrPadding mode: zeros, reflect, replicate, or circular; default zeros.
devicetorch.device or str or NoneOptional device on which to create the module.
dtypetorch.dtype or NoneOptional data type for the module parameters.

Returns

A tensor of shape (N, out_channels, H_out, W_out), where spatial dimensions are determined by the documented convolution formula.

Key points

  • Conv2d is a learnable module for extracting two-dimensional spatial features from multiple input channels.
  • Stride sets the filter movement step and can be shared across dimensions or specified separately.
  • Groups partitions channel connections, enabling grouped or depthwise convolution patterns.

Common mistakes

  • The string padding mode same preserves spatial size but cannot be combined with a stride other than one.
  • Integer spatial arguments apply uniformly; use two-element tuples when height and width need different settings.

Example: Basic 2D convolution

import torch
from torch import nn

layer = nn.Conv2d(in_channels=3, out_channels=8, kernel_size=3, padding=1)
x = torch.randn(2, 3, 32, 32)
y = layer(x)
print(y.shape)  # torch.Size([2, 8, 32, 32])

Creates an eight-channel 3×3 convolution for RGB images and applies it to a batch of two 32×32 inputs.

When to use it

Learn spatial visual features from image batches.

Image classification and recognition

Transform feature maps while controlling resolution and channel count.

Encoder-decoder and segmentation models

Reduce or structure channel connectivity with groups.

Grouped or depthwise architectures

Interview questions

QWhat input and parameter shapes does Conv2d use?

Conv2d expects input shaped (N, C_in, H, W) and produces (N, C_out, H_out, W_out). Its learned weight has shape (C_out, C_in / groups, kernel height, kernel width), with an optional bias of length C_out.

Follow-up: How does groups change connectivity? Each output channel connects only to its corresponding input-channel group, and C_in and C_out must both be divisible by groups.

QHow are Conv2d output dimensions determined?

The output height and width are computed independently as floor((input + 2·padding − dilation·(kernel−1) − 1) / stride + 1). Increasing padding generally preserves more border information, while stride reduces the spatial resolution.

Follow-up: What does dilation do? It spaces kernel elements apart, increasing the receptive field without proportionally increasing the number of kernel parameters.

QWhich padding and operation semantics should be remembered?

Conv2d performs cross-correlation rather than mathematically flipped convolution. It supports zero, reflect, replicate, and circular padding modes, and its default padding mode is zeros.

Follow-up: When is padding='same' valid? It keeps output and input spatial shapes equal, but the documented module restriction allows only stride 1.

  • torch.nn.Conv1d
  • torch.nn.Conv3d
  • torch.nn.functional.conv2d
  • torch.nn.BatchNorm2d
  • torch.nn.MaxPool2d

Source: Conv2d — PyTorch 2.14 documentation. Checked 2026-10-15.