Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
unsort pytorch
x = torch.randn(10)
print(x)
y, ind = torch.sort(x, 0)
print("y", y)
print("ind", ind)
unsorted = y.new(*y.size())
unsorted.scatter_(0, ind, y)
print("unsorted:", unsorted)
print((x - unsorted).abs().max())
@keon

This comment has been minimized.

Copy link
Owner Author

@keon keon commented Jan 4, 2018

0.5140
1.8583
1.2117
-0.8053
-0.5985
-0.9133
-0.0411
-0.8822
0.2321
-0.3467
[torch.FloatTensor of size 10]

y
-0.9133
-0.8822
-0.8053
-0.5985
-0.3467
-0.0411
0.2321
0.5140
1.2117
1.8583
[torch.FloatTensor of size 10]

ind
5
7
3
4
9
6
8
0
2
1
[torch.LongTensor of size 10]

unsorted:
0.5140
1.8583
1.2117
-0.8053
-0.5985
-0.9133
-0.0411
-0.8822
0.2321
-0.3467
[torch.FloatTensor of size 10]

0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment