Rate this Page

torch.functional.unravel_index#

torch.functional.unravel_index(indices,shape)[source]#

Converts a tensor of flat indices into a tuple of coordinate tensors thatindex into an arbitrary tensor of the specified shape.

Parameters
  • indices (Tensor) – An integer tensor containing indices into theflattened version of an arbitrary tensor of shapeshape.All elements must be in the range[0,prod(shape)-1].

  • shape (int,sequence ofints, ortorch.Size) – The shape of the arbitrarytensor. All elements must be non-negative.

Returns

Eachi-th tensor in the output corresponds withdimensioni ofshape. Each tensor has the same shape asindices and contains one index into dimensioni for each of theflat indices given byindices.

Return type

tuple of Tensors

Example:

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