Skip to content

grid_sample

Sample an image using a per-example normalized coordinate grid.

Category: spatial · Identity: grid_sample@1

Shape

x[B, C, H, W], grid[B, OH, OW, 2] -> out[B, C, OH, OW]
Port Direction Pattern dtype
x input x[B, C, H, W] compute
grid input grid[B, OH, OW, 2] compute
out output out[B, C, OH, OW] compute

Arguments

Name Type Default Constraints Description
mode str "bilinear" one of bilinear, nearest, bicubic Interpolation kernel.
padding_mode str "zeros" one of zeros, border, reflection How samples outside the input are filled.
align_corners bool False — Whether -1 and 1 refer to corner pixel centers.

Description

A direct torch.nn.functional.grid_sample with an explicit grid.

The grid's last axis is (x,y) in normalized [-1,1] coordinates. Image and grid batches must match. Defaults are bilinear interpolation, zero padding, and align_corners=False. No parameters or random draws; train and eval are identical. Gradients flow to image and coordinates as supported by PyTorch. CUDA backward can be nondeterministic, and PyTorch's sampler does not support double backward.

Examples

Example 1

Sample a 4x4 image on an 8x8 grid.

g = coordinate_grid(x, 8, 8)
grid_sample(x, g)

Input ['B', 3, 4, 4] → output ['B', 3, 8, 8].

Network: [B, 3, 4, 4] -> [B, 3, 8, 8]  dtype=float32
name  operation        input shapes                                           output shapes
n0    coordinate_grid  x=input:x:[B, 3, 4, 4]                                 out=[B, 8, 8, 2]
n1    grid_sample      x=input:x:[B, 3, 4, 4], grid=node:n0/out:[B, 8, 8, 2]  out=[B, 3, 8, 8]

Parameters: 0