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

PyTorch · Function

F.conv2d

F.conv2d applies a 2D convolution to image-like input using explicit filters and optional bias. Parameters control kernel movement, padding, dilation, and channel grouping.

How it works

F.conv2d applies a two-dimensional convolution to an input tensor using explicitly supplied filters and an optional bias. Stride, padding, dilation, and groups control sampling, borders, kernel spacing, and channel connectivity, and the result is returned as a tensor.

Syntax

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor

Parameters

NameTypeDescription
inputTensorInput tensor with shape (minibatch, in_channels, iH, iW).
weightTensorFilter tensor with shape (out_channels, in_channels / groups, kH, kW).
biasTensor or NoneOptional bias tensor with shape (out_channels); default is None.
strideint or tupleKernel step size; one number or (sH, sW), default 1.
paddingstr, int, or tupleImplicit padding on both sides; valid, same, one number, or (padH, padW), default 0.
dilationint or tupleSpacing between kernel elements; one number or (dH, dW), default 1.
groupsintNumber of channel groups; default 1. Input and output channels must be divisible by groups.

Returns

Tensor

Key points

  • The function performs a two-dimensional convolution over image-like data with multiple input channels.
  • On supported hardware, the operation can use TensorFloat32 arithmetic.
  • Stride and dilation-style spatial settings can be expressed as one number or separate height and width values.

Common mistakes

  • The padding value "same" can only be used with stride 1.

Example: Apply a padded 2D convolution

import torch
import torch.nn.functional as F

inputs = torch.randn(1, 4, 5, 5)
filters = torch.randn(8, 4, 3, 3)
output = F.conv2d(inputs, filters, padding=1)
print(output.shape)  # torch.Size([1, 8, 5, 5])

Creates a batch of four-channel images and applies eight 3×3 filters with one-pixel padding, preserving the spatial dimensions.

When to use it

Image feature extraction

Apply learned or manually supplied filters to batched image tensors.

Grouped convolution

Restrict filters to channel partitions for efficient or specialized convolutions.

Spatial resizing control

Preserve or adjust spatial dimensions through explicit border handling and kernel movement.

Interview questions

QWhat shapes do input and weight have?

The input is shaped as (minibatch, in_channels, height, width), while the weight tensor is shaped as (out_channels, in_channels / groups, kernel height, kernel width). The operation returns a tensor containing the convolution output.

Follow-up: How does groups change connectivity? It partitions the input and output channels into groups, so each filter group operates only on its corresponding input-channel partition.

QWhat do stride and padding control?

stride controls the movement of the kernel and accepts either one value or a two-dimensional tuple. padding adds implicit border padding and accepts a number, a pair, or the strings valid and same.

Follow-up: What restriction applies to same padding? It requires stride to be 1.

QHow do dilation and groups affect convolution?

dilation specifies the spacing between kernel elements and can be one value or a two-value tuple. groups partitions the convolution, and the input and output channel counts must both be divisible by it.

Follow-up: What happens when groups equals the number of input channels? The operation becomes depthwise-style when the corresponding output-channel divisibility condition also holds.

  • torch.nn.functional.conv1d
  • torch.nn.functional.conv3d
  • torch.nn.Conv2d

Source: torch.nn.functional.conv2d — PyTorch 2.12 documentation. Checked 2026-10-16.