torch.unravel_index#
- torch.unravel_index(indices, shape)[source]#
将平坦索引张量转换为坐标张量元组,这些元组可以索引到任意指定形状的张量。
- 参数
indices (Tensor) – 一个包含展平后的任意形状为
shape
的张量的索引的整数张量。所有元素必须在范围[0, prod(shape) - 1]
内。shape (int, ints 的 sequence, or torch.Size) – 任意张量的形状。所有元素必须是非负的。
- 返回
输出中的每个
i
-th 张量对应于shape
的第i
维。每个张量具有与indices
相同的形状,并且包含一个索引到第i
维,对应于indices
中给出的每个展平索引。- 返回类型
tuple of Tensors
示例
>>> import torch >>> torch.unravel_index(torch.tensor(4), (3, 2)) (tensor(2), tensor(0)) >>> torch.unravel_index(torch.tensor([4, 1]), (3, 2)) (tensor([2, 0]), tensor([0, 1])) >>> torch.unravel_index(torch.tensor([0, 1, 2, 3, 4, 5]), (3, 2)) (tensor([0, 0, 1, 1, 2, 2]), tensor([0, 1, 0, 1, 0, 1])) >>> torch.unravel_index(torch.tensor([1234, 5678]), (10, 10, 10, 10)) (tensor([1, 5]), tensor([2, 6]), tensor([3, 7]), tensor([4, 8])) >>> torch.unravel_index(torch.tensor([[1234], [5678]]), (10, 10, 10, 10)) (tensor([[1], [5]]), tensor([[2], [6]]), tensor([[3], [7]]), tensor([[4], [8]])) >>> torch.unravel_index(torch.tensor([[1234], [5678]]), (100, 100)) (tensor([[12], [56]]), tensor([[34], [78]]))