Skip to content

Instantly share code, notes, and snippets.

@keon
Created January 4, 2018 09:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save keon/167fc97441f9f332895afc3bcc33ffd8 to your computer and use it in GitHub Desktop.
Save keon/167fc97441f9f332895afc3bcc33ffd8 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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