Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.unravel_index function

PyTorch torch.unravel_index function


PyTorch torch reference manual PyTorch torch reference manual

torch.unravel_index is a function in PyTorch used to unravel a flat index into multi-dimensional indices. It is the opposite of torch.ravel_index ; it converts a position in a flattened array back into coordinates in a multi-dimensional array.

Function definition

torch.unravel_index(indices, shape)

Parameter Description


Usage examples

Example

import

torch

Flatten the index

indices

=

torch.

tensor

(

[

0

,

1

,

5

,

6

,

7

]

)

Array shape

shape

=

(

2

,

4

)

Unravel into multi-dimensional indices

result

=

torch.

unravel_index

(

indices

,

shape

)

print

(

"Flattened index:"

,

indices

)

print

(

"Array shape:"

,

shape

)

print

(

"Multi-dimensional indices:"

)

print

(

result

)

The output is:

展平索引: tensor([0, 1, 5, 6, 7])

数组形状: (2, 4)

Multidimensional index:

tensor([[0, 0, 1, 1, 1],

        [0, 1, 1, 0, 1]])


PyTorch torch reference manual PyTorch torch reference manual