Table of Contents
Shortcuts

# torch.triu_indices

torch.triu_indices(row, col, offset=0, *, dtype=torch.long, device='cpu', layout=torch.strided) → Tensor

  Returns the indices of the upper triangular part of a `row` by `col` matrix in a 2-by-N Tensor, where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns.

  The upper triangular part of the matrix is defined as the elements on and above the diagonal.

  The argument `offset` controls which diagonal to consider. If `offset` = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)}\lbrace (i, i) \rbrace{(i,i)} for i∈[0,min⁡{d 1,d 2}−1]i \in [0, \min\{d_{1}, d_{2}\} - 1]i∈[0,min{d 1​,d 2​}−1] where d 1,d 2 d_{1}, d_{2}d 1​,d 2​ are the dimensions of the matrix.

  Note

  When running on CUDA, `row * col` must be less than 2 59 2^{59}2 59 to prevent overflow during calculation.

  Parameters:

    - row (`int`) – number of rows in the 2-D matrix.

    - col (`int`) – number of columns in the 2-D matrix.

    - offset (`int`) – diagonal offset from the main diagonal. Default: if not provided, 0.

  Keyword Arguments:

    - dtype (`torch.dtype`, optional) – the desired data type of returned tensor. Default: if `None`, `torch.long`.

    - device (`torch.device`, optional) – the desired device of returned tensor. Default: if `None`, uses the current device for the default tensor type (see `torch.set_default_tensor_type()`). `device` will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

    - layout (`torch.layout`, optional) – currently only support `torch.strided`.

  Example:

      >>> a = torch.triu_indices(3, 3)
      >>> a
      tensor([[0, 0, 0, 1, 1, 2],
              [0, 1, 2, 1, 2, 2]])

      >>> a = torch.triu_indices(4, 3, -1)
      >>> a
      tensor([[0, 0, 0, 1, 1, 1, 2, 2, 3],
              [0, 1, 2, 0, 1, 2, 1, 2, 2]])

      >>> a = torch.triu_indices(4, 3, 1)
      >>> a
      tensor([[0, 0, 1],
              [1, 2, 2]])

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