Skip to content

Instantly share code, notes, and snippets.

@xmodar
Created December 4, 2019 05:28
Show Gist options
  • Save xmodar/213b0e8d3a305e7c37d5b4380c6d5036 to your computer and use it in GitHub Desktop.
Save xmodar/213b0e8d3a305e7c37d5b4380c6d5036 to your computer and use it in GitHub Desktop.
import torch
def unravel_index(index, shape):
out = []
for dim in reversed(shape):
out.append(index % dim)
index = index // dim
return tuple(reversed(out))
x = torch.arange(30).view(10, 3)
for i in range(x.numel()):
assert i == x[unravel_index(i, x.shape)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment