Table of Contents
Shortcuts

# torch.narrow

torch.narrow(input, dim, start, length) → Tensor

  Returns a new tensor that is a narrowed version of `input` tensor. The dimension `dim` is input from `start` to `start + length`. The returned tensor and `input` tensor share the same underlying storage.

  Parameters:

    - input (Tensor) – the tensor to narrow

    - dim (int) – the dimension along which to narrow

    - start (int or Tensor) – index of the element to start the narrowed dimension from. Can be negative, which means indexing from the end of dim. If Tensor, it must be an 0-dim integral Tensor (bools not allowed)

    - length (int) – length of the narrowed dimension, must be weakly positive

  Example:

      >>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
      >>> torch.narrow(x, 0, 0, 2)
      tensor([[ 1,  2,  3],
              [ 4,  5,  6]])
      >>> torch.narrow(x, 1, 1, 2)
      tensor([[ 2,  3],
              [ 5,  6],
              [ 8,  9]])
      >>> torch.narrow(x, -1, torch.tensor(-1), 1)
      tensor([[3],
              [6],
              [9]])

## 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.