question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[np.random] shuffle is not compatible with pytorch tensor

See original GitHub issue

When 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.

See also: https://github.com/pytorch/pytorch/issues/50880

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rkerncommented, Jan 22, 2021

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.

0reactions
rgommerscommented, Jan 23, 2021

Thanks. Fix in gh-18211.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found