Table of Contents
Shortcuts

# torch.nn.functional.affine_grid

torch.nn.functional.affine_grid(theta, size, align_corners=None)

  Generates a 2D or 3D flow field (sampling grid), given a batch of affine matrices `theta`.

  Note

  This function is often used in conjunction with `grid_sample()` to build Spatial Transformer Networks .

  Parameters:

    - theta (Tensor) – input batch of affine matrices with shape (N×2×3 N \times 2 \times 3 N×2×3) for 2D or (N×3×4 N \times 3 \times 4 N×3×4) for 3D

    - size (torch.Size) – the target output image size. (N×C×H×W N \times C \times H \times W N×C×H×W for 2D or N×C×D×H×W N \times C \times D \times H \times W N×C×D×H×W for 3D) Example: torch.Size((32, 3, 24, 24))

    - align_corners (bool, optional) – if `True`, consider `-1` and `1` to refer to the centers of the corner pixels rather than the image corners. Refer to `grid_sample()` for a more complete description. A grid generated by `affine_grid()` should be passed to `grid_sample()` with the same setting for this option. Default: `False`

  Returns:

    output Tensor of size (N×H×W×2 N \times H \times W \times 2 N×H×W×2)

  Return type:

    output (Tensor)

  Warning

  When `align_corners = True`, the grid positions depend on the pixel size relative to the input image size, and so the locations sampled by `grid_sample()` will differ for the same input given at different resolutions (that is, after being upsampled or downsampled). The default behavior up to version 1.2.0 was `align_corners = True`. Since then, the default behavior has been changed to `align_corners = False`, in order to bring it in line with the default for `interpolate()`.

  Warning

  When `align_corners = True`, 2D affine transforms on 1D data and 3D affine transforms on 2D data (that is, when one of the spatial dimensions has unit size) are ill-defined, and not an intended use case. This is not a problem when `align_corners = False`. Up to version 1.2.0, all grid points along a unit dimension were considered arbitrarily to be at `-1`. From version 1.3.0, under `align_corners = True` all grid points along a unit dimension are considered to be at `0` (the center of the input image).

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.