Learn spatial visual features from image batches.
Image classification and recognition
PyTorch · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
in_channels | int | Number of channels in the input image. |
out_channels | int | Number of channels produced by the convolution. |
kernel_size | int or tuple | Size of the convolving kernel. |
stride | int or tuple | Stride of the convolution; default 1. |
padding | int, tuple, or str | Padding added to all four sides of the input; default 0. |
dilation | int or tuple | Spacing between kernel elements; default 1. |
groups | int | Controls connections between inputs and outputs; default 1. |
bias | bool | If true, adds a learnable bias; default true. |
padding_mode | str | Padding mode: zeros, reflect, replicate, or circular; default zeros. |
device | torch.device or str or None | Optional device on which to create the module. |
dtype | torch.dtype or None | Optional data type for the module parameters. |
A tensor of shape (N, out_channels, H_out, W_out), where spatial dimensions are determined by the documented convolution formula.
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.
Image classification and recognition
Encoder-decoder and segmentation models
Grouped or depthwise architectures
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.
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.
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.Conv1dtorch.nn.Conv3dtorch.nn.functional.conv2dtorch.nn.BatchNorm2dtorch.nn.MaxPool2d