PyTorch torch.unravel_index function
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
indices: The flattened indexshape: The shape of the multi-dimensional array
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
)
(
"Flattened index:"
,
indices
)
(
"Array shape:"
,
shape
)
(
"Multi-dimensional indices:"
)
(
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]])