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

PyTorch · Function

F.interpolate

Resizes 3-D, 4-D, or 5-D tensors by upsampling or downsampling their spatial dimensions. You choose the output size or scale factor and an interpolation mode such as nearest, bilinear, bicubic, trilinear, area, or nearest-exact.

How it works

The function resizes a tensor by computing an output spatial shape from either an explicit size or scale multiplier, then applying the selected interpolation algorithm. It accepts temporal, spatial, and volumetric layouts with batch and channel dimensions followed by one to three spatial dimensions.

Syntax

torch.nn.functional.interpolate(input, size=None, scale_factor=None, mode='nearest', align_corners=None, recompute_scale_factor=None, antialias=False)

Parameters

NameTypeDescription
inputTensorInput tensor to resize.
sizeint or tuple[int, ...], optionalOutput spatial size, supplied as an integer or a tuple with one to three spatial dimensions.
scale_factorfloat or tuple[float, ...], optionalMultiplier applied to each spatial dimension; a tuple must match the number of spatial dimensions.
modestr, optionalInterpolation algorithm: nearest, linear, bilinear, bicubic, trilinear, area, or nearest-exact.
align_cornersbool, optionalWhether to align corner pixels for supported linear-family modes.
recompute_scale_factorbool, optionalWhether to recompute the interpolation scale from the output size.
antialiasbool, optionalWhether to apply anti-aliasing.

Returns

A tensor resized along its spatial dimensions according to size or scale_factor and the selected mode.

Key points

  • Use the function for both enlarging and reducing tensors.
  • Choose an explicit spatial output size or a multiplicative resize factor.
  • The function supports one-dimensional temporal, two-dimensional spatial, and three-dimensional volumetric data.
  • Enable recomputation only when supplying scale_factor; it derives the interpolation scale from the resulting output size.
  • Set antialias to request anti-alias filtering where the selected interpolation mode supports it.

Common mistakes

  • A tuple scale factor must contain one value for every spatial dimension.

Example: Upsample an image tensor

import torch
import torch.nn.functional as F

x = torch.randn(1, 3, 8, 8)
y = F.interpolate(x, size=(16, 16), mode="bilinear", align_corners=False)
print(y.shape)  # torch.Size([1, 3, 16, 16])

This runnable example upsamples a 4-D image tensor to 16×16 using bilinear interpolation.

When to use it

Resize batched images to a fixed height and width.

Image preprocessing

Upsample or downsample feature maps between network stages.

Feature pyramids

Resize temporal sequences or 3-D spatial volumes with rank-compatible modes.

Video and volumetric data

Interview questions

QWhat does F.interpolate do, and what input shapes does it support?

It resizes a tensor using either an explicit output size or a scale factor. The supported input ranks are 3-D, 4-D, and 5-D, interpreted as batch, channels, and one to three spatial dimensions.

Follow-up: How would you resize a 2-D image batch? Use a 4-D tensor shaped N×C×H×W and select a compatible mode such as bilinear or nearest.

QHow do size and scale_factor differ?

The size argument directly specifies the output spatial dimensions, while scale_factor multiplies the input spatial dimensions. Only one of size or scale_factor should determine the target shape.

Follow-up: What constraint applies to tuple scale_factor values? Their length must equal input.dim() minus 2, the number of spatial dimensions.

QWhen is align_corners valid, and what does it affect?

align_corners controls geometric alignment for linear-family interpolation and is only valid with linear, bilinear, bicubic, or trilinear modes. When enabled, the corner pixels of input and output are aligned; when disabled, scaling uses a different center-based geometry.

Follow-up: Which modes can use antialiasing according to the page? Bilinear and bicubic modes support it.

  • torch.nn.functional

Source: torch.nn.functional.interpolate. Checked 2026-10-16.