fn

interpolate

Tensor
interpolate(x: Tensor, size: int | tuple[int, ...] | None = None, scale_factor: float | tuple[float, ...] | None = None, mode: str = 'nearest', align_corners: bool | None = None, recompute_scale_factor: bool | None = None)
source

Resample an N-D tensor to a target spatial size or scale factor.

Supports the standard family of image / volume resampling kernels. The leading two axes (N batch and C channels) are preserved; only the trailing spatial axes are resampled.

Parameters

xTensor
Input of shape (N, C, *spatial) where len(spatial) is 1 (linear), 2 (bilinear / bicubic / area / nearest-2d) or 3 (trilinear / nearest-3d).
sizeint or tuple of int= None
Target spatial size. Mutually exclusive with scale_factor.
scale_factorfloat or tuple of float= None
Multiplicative factor applied to each spatial axis. Output size is floor(in_size * scale_factor).
modestr= 'nearest'
One of "nearest", "linear", "bilinear", "bicubic", "trilinear", "area", "nearest-exact". Default "nearest".
align_cornersbool= None
Only meaningful for "linear" / "bilinear" / "bicubic" / "trilinear". If True, the corner pixels of the input and output are exactly aligned, and the values at those corners are preserved. If False (default), pixels are treated as 1×1 squares and the sampling grid is centred — this matches the convention used by most modern vision frameworks.
recompute_scale_factorbool= None
If True, recompute scale_factor from the resolved size to avoid floating-point drift when chaining resamples.

Returns

Tensor

Resampled tensor of shape (N, C, *out_spatial).

Notes

The two align_corners conventions produce different sampling grids — xsrc=(xdst+0.5)(Sin/Sout)0.5x_{\mathrm{src}} = (x_{\mathrm{dst}} + 0.5) \cdot (S_{\mathrm{in}} / S_{\mathrm{out}}) - 0.5 for the default, vs xsrc=xdst((Sin1)/(Sout1))x_{\mathrm{src}} = x_{\mathrm{dst}} \cdot ((S_{\mathrm{in}} - 1) / (S_{\mathrm{out}} - 1)) for align_corners=True. The choice matters when fine alignment with other ops (warping, geometric losses) is required.

Examples

>>> import lucid
>>> from lucid.nn.functional import interpolate
>>> x = lucid.randn(1, 3, 32, 32)
>>> y = interpolate(x, scale_factor=2.0, mode="bilinear", align_corners=False)
>>> y.shape
(1, 3, 64, 64)