# grid_sample¶

This operation samples input X by using bilinear interpolation or nearest interpolation based on flow field grid, which is usually generated by `affine_grid` . The grid of shape [N, H, W, 2] is the concatenation of (x, y) coordinates with shape [N, H, W] each, where x is indexing the 4th dimension (in width dimension) of input data x and y is indexing the 3rd dimension (in height dimension), finally results is the bilinear interpolation or nearest value of 4 nearest corner points. The output tensor shape will be [N, C, H, W].

Step 1:

Get (x, y) grid coordinates and scale to [0, H-1/W-1].

```grid_x = 0.5 * (grid[:, :, :, 0] + 1) * (W - 1)
grid_y = 0.5 * (grid[:, :, :, 1] + 1) * (H - 1)
```

Step 2:

Indices input data X with grid (x, y) in each [H, W] area, and bilinear interpolate point value by 4 nearest points or nearest interpolate point value by nearest point.

```wn ------- y_n ------- en
|           |           |
|          d_n          |
|           |           |
x_w --d_w-- grid--d_e-- x_e
|           |           |
|          d_s          |
|           |           |
ws ------- y_s ------- wn

For bilinear interpolation:
x_w = floor(x)              // west side x coord
x_e = x_w + 1               // east side x coord
y_n = floor(y)              // north side y coord
y_s = y_s + 1               // south side y coord
d_w = grid_x - x_w          // distance to west side
d_e = x_e - grid_x          // distance to east side
d_n = grid_y - y_n          // distance to north side
d_s = y_s - grid_y          // distance to south side
wn = X[:, :, y_n, x_w]      // north-west point value
en = X[:, :, y_n, x_e]      // north-east point value
ws = X[:, :, y_s, x_w]      // south-east point value
es = X[:, :, y_s, x_w]      // north-east point value

output = wn * d_e * d_s + en * d_w * d_s
+ ws * d_e * d_n + es * d_w * d_n
```
Parameters
• x (Tensor) – The input tensor, which is a 4-d tensor with shape [N, C, H, W], N is the batch size, C is the channel number, H and W is the feature height and width. The data type is float32 or float64.

• grid (Tensor) – Input grid tensor of shape [N, grid_H, grid_W, 2]. The data type is float32 or float64.

• mode (str, optional) – The interpolation method which can be ‘bilinear’ or ‘nearest’. Default: ‘bilinear’.

• padding_mode (str, optional) – is out of input images. It can be ‘zeros’, ‘reflection’ and ‘border’. Default: zeros.

• align_corners (bool, optional) – If align_corners is true, it will projects -1 and 1 to the centers of the corner pixels. Otherwise, it will projects -1 and 1 to the image edges.

• name (str, optional) – For detailed information, please refer to Name. Usually name is no need to set and None by default.

Returns

Tensor, The shape of output is [N, C, grid_H, grid_W] in which grid_H is the height of grid and grid_W is the width of grid. The data type is same as input tensor.

Examples

```import paddle
import numpy as np

# shape=[1, 1, 3, 3]
x = np.array([[[[-0.6,  0.8, -0.5],
[-0.5,  0.2,  1.2],
[ 1.4,  0.3, -0.2]]]]).astype("float64")

# grid shape = [1, 3, 4, 2]
grid = np.array(
[[[[ 0.2,  0.3],
[-0.4, -0.3],
[-0.9,  0.3],
[-0.9, -0.6]],
[[ 0.4,  0.1],
[ 0.9, -0.8],
[ 0.4,  0.5],
[ 0.5, -0.2]],
[[ 0.1, -0.8],
[-0.3, -1. ],
[ 0.7,  0.4],
[ 0.2,  0.8]]]]).astype("float64")