Image feature extraction
Apply learned or manually supplied filters to batched image tensors.
PyTorch · Function
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.
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.
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor| Name | Type | Description |
|---|---|---|
input | Tensor | Input tensor with shape (minibatch, in_channels, iH, iW). |
weight | Tensor | Filter tensor with shape (out_channels, in_channels / groups, kH, kW). |
bias | Tensor or None | Optional bias tensor with shape (out_channels); default is None. |
stride | int or tuple | Kernel step size; one number or (sH, sW), default 1. |
padding | str, int, or tuple | Implicit padding on both sides; valid, same, one number, or (padH, padW), default 0. |
dilation | int or tuple | Spacing between kernel elements; one number or (dH, dW), default 1. |
groups | int | Number of channel groups; default 1. Input and output channels must be divisible by groups. |
Tensor
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.
Apply learned or manually supplied filters to batched image tensors.
Restrict filters to channel partitions for efficient or specialized convolutions.
Preserve or adjust spatial dimensions through explicit border handling and kernel movement.
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.
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.
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.conv1dtorch.nn.functional.conv3dtorch.nn.Conv2d