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]]))