[np.random] shuffle is not compatible with pytorch tensor
See original GitHub issueWhen shuffling a pytorch tensor with numpy.random.shuffle
, incorrect result will be produced with 100% (so far) chance.
Reproducing code example:
import torch as th
import numpy as np
N = 10
x = th.arange(N)
print(x)
np.random.shuffle(x)
print(x)
assert(len(set(x.tolist())) == N)
Error message:
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
tensor([0, 0, 2, 1, 3, 4, 6, 3, 3, 9])
Traceback (most recent call last):
File "x.py", line 9, in <module>
assert(len(set(x.tolist())) == N)
AssertionError
In the shuffled result, 0
and 3
occured twice, which is clearly problematic.
NumPy/Python version information:
numpy ‘1.18.5’ through anaconda.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
in-place shuffle torch.Tensor in the order of a numpy.ndarray
shuffle(order) meets your requirement to absolutely not occupy extra memory). Generally the official website discourages in-place operations, ...
Read more >Shuffling a Tensor - PyTorch Forums
Hi Everyone - Is there a way to shuffle/randomize a tensor. Something equivalent to numpy's random.shuffle. Thanks!
Read more >Torch equivalent of numpy.random.choice? - PyTorch Forums
I am trying to extract random “slices” of tensors. Is there a torch equivalent of numpy.random.choice ? And if not, is there a...
Read more >Reproducibility — PyTorch 1.13 documentation
Reproducibility. Completely reproducible results are not guaranteed across PyTorch releases, individual commits, or different platforms.
Read more >torch.utils.data — PyTorch 1.13 documentation
DataLoader(dataset, batch_size=1, shuffle=False, sampler=None, ... It automatically converts NumPy arrays and Python numerical values into PyTorch Tensors.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve added some comments on pytorch/pytorch#50880
I suspect that we’ll need input from both communities to properly resolve this, so I apologize to all for the bifurcated discussion.
Thanks. Fix in gh-18211.