code: def affine(
      tensor: torch.Tensor,
      matrix: torch.Tensor,
      mode: str = "bilinear",
      padding_mode: str = "zeros",
      align_corners: bool = True,
  ) -> torch.Tensor:
      r"""Apply an affine transformation to the image.
  
      .. image:: _static/img/warp_affine.png
  
      Args:
          tensor: The image tensor to be warped in shapes of
              :math:`(H, W)`, :math:`(D, H, W)` and :math:`(B, C, H, W)`.
          matrix: The 2x3 affine transformation matrix.
          mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
          padding_mode: padding mode for outside grid values
            ``'zeros'`` | ``'border'`` | ``'reflection'``.
          align_corners: interpolation flag.
  
      Returns:
          The warped image with the same shape as the input.
  
      Example:
          >>> img = torch.rand(1, 2, 3, 5)
          >>> aff = torch.eye(2, 3)[None]
          >>> out = affine(img, aff)
          >>> print(out.shape)
          torch.Size([1, 2, 3, 5])
      """
      # warping needs data in the shape of BCHW
      is_unbatched: bool = tensor.ndimension() == 3
      if is_unbatched:
          tensor = torch.unsqueeze(tensor, dim=0)
  
      # we enforce broadcasting since by default grid_sample it does not
      # give support for that
      matrix = matrix.expand(tensor.shape[0], -1, -1)
  
      # warp the input tensor
      height: int = tensor.shape[-2]
      width: int = tensor.shape[-1]
      warped: torch.Tensor = warp_affine(
          tensor,
          matrix,
          (height, width),
          mode,
          padding_mode,
          align_corners,
      )
  
      # return in the original shape
      if is_unbatched:
          warped = torch.squeeze(warped, dim=0)
  
      return warped
  
imports: from typing import Optional, Tuple, Union
  import torch
  from torch import nn
  from kornia.core import ones, ones_like, zeros
  from kornia.filters import gaussian_blur2d
  from kornia.utils import _extract_device_dtype
  from kornia.utils.image import perform_keep_shape_image
  from kornia.utils.misc import eye_like
  from .imgwarp import get_affine_matrix2d, get_projective_transform, get_rotation_matrix2d, warp_affine, warp_affine3d
label_desc: Use image node_id 8 for calling a global function with example usage: affine(tensor, matrix, mode, padding_mode, align_corners) and returns: warped