Live data from Hacker News

A common mistake when NumPy’s RNG with PyTorch

tanelp.github.io

11–20 of 54 posts

Re: A common mistake when NumPy’s RNG with PyTorch

#11
I always randomly log a sample of my inputs to TendorBoard to manually review what my training data actually looks like and (hopefully) pick up on bugs like these. Similarly I find logging high loss inputs very informative.

Coincidentally I find this article timely as I was recently reviewing PyTorch DataLoader docs regarding random number generator seeding. It’s the kind of thing unit test don’t pick up since it only occurs when you use separate worker processes.

Re: A common mistake when NumPy’s RNG with PyTorch

#13

This is probably because I never read these kinds of blogposts but this is one of the most flagrantly clickbait titles I've ever seen. Like the article doesn't even suggest ditching numpy in favor of jax or some kind of other hot take (which would at least warrant such a bombastic title) it literally just presents one instance in which you might be making a mistake when using numpy's rng (not even something more uniq…

Maybe the intent is for it to be read as "If you're using pytorch and numpy, it's _very_ likely you're making this mistake", but the effect is still that the headline is clickbait

Re: A common mistake when NumPy’s RNG with PyTorch

#14
This post is yet another example of why you should never use APIs for random number generation that rely upon and mutate hidden global state, like the functions in numpy.random. Instead, use APIs that explicitly deal with RNG state, e.g., by calling methods on an explicitly created numpy.random.Generator object. JAX takes this one step further: there are no mutable RNG objects at all, and the users has to explicitly manipulate RNG state with pure functions.

It’s a little annoying to have to set and pass RNG state explicitly, but on the plus side you never hit these sorts of issues. Your code will also be completely reproducible, without any chance of spooky “action at a distance.” Once you’ve been burned by this a few times, you’ll never go back.

You might think that explicitly seeding the global RNG would solve reproducibility issues, but it really doesn’t. If you call into any code you didn’t write, it might also be using the same global RNG.

Re: A common mistake when NumPy’s RNG with PyTorch

#15
post #5

Forgetting to seed your RNG is a really classic bug. IMHO RNGs should auto seed unless explicitly set not to, but since the opposite behaviour was baked into C so many years ago it's kind of the default. The worst part is how easy a bug this is to miss unless you're explicitly printing out the first set of random numbers for some strange reason.

I’m of the opposite opinion and would get away from all auto RNG seeding:

1) this will help reproducibility a great deal, which is a pain so often.

2) forcing users to actually understand the seeding of RNGs from the point that they are novice programmers could help allay bugs of the sort seen in this post, which I believe stems from having too much faith that RNGs will simply work out of the box as substitutions for ‘real’ random variables.

Re: A common mistake when NumPy’s RNG with PyTorch

#16

This is probably because I never read these kinds of blogposts but this is one of the most flagrantly clickbait titles I've ever seen. Like the article doesn't even suggest ditching numpy in favor of jax or some kind of other hot take (which would at least warrant such a bombastic title) it literally just presents one instance in which you might be making a mistake when using numpy's rng (not even something more uniq…

I suppose... 1) This is an issue from 2018 ( https://github.com/pytorch/pytorch/issues/5059 ), which links to the closed numpy issue ( https://github.com/numpy/numpy/issues/9248 ) which just says: seed your random numbers folk. 2) The documentation in pytorch covers this ( https://pytorch.org/docs/stable/data.html#randomness-in-mult... ), but it's not really highlighted specifically in, eg. tutorials. (but it is in t…

Better title? Over 95% of GitHub repos using NumPy and PyTorch aren't getting the random numbers they think they are.

Re: A common mistake when NumPy’s RNG with PyTorch

#17

Earlier quoted context omitted.

I suppose... 1) This is an issue from 2018 ( https://github.com/pytorch/pytorch/issues/5059 ), which links to the closed numpy issue ( https://github.com/numpy/numpy/issues/9248 ) which just says: seed your random numbers folk. 2) The documentation in pytorch covers this ( https://pytorch.org/docs/stable/data.html#randomness-in-mult... ), but it's not really highlighted specifically in, eg. tutorials. (but it is in t…

Better title? Over 95% of GitHub repos using NumPy and PyTorch aren't getting the random numbers they think they are.

Probably over the HN Title character limit.

Re: A common mistake when NumPy’s RNG with PyTorch

#18
post #5

Forgetting to seed your RNG is a really classic bug. IMHO RNGs should auto seed unless explicitly set not to, but since the opposite behaviour was baked into C so many years ago it's kind of the default. The worst part is how easy a bug this is to miss unless you're explicitly printing out the first set of random numbers for some strange reason.

NumPy does auto-seed the RNG if you don't pass a seed yourself, using platform-specific code to pull some entropy from the OS. So that common case is handled reasonably well, unlike with C. In fact if you want exactly reproducible results (e.g. in testcases), you have to seed with a known seed, to avoid that default behavior. The issue here is a little more subtle: if you fork 10 copies of your Python process, all 10…

It’s even slightly more subtle than that.

Python multiprocessing doesn’t use fork on Windows. It starts a new process and so shouldn’t be affected by this.

So to trigger this you need to have num_processes != 0 on your DataLoader and be running on a non-Windows platform.

Post reply on HN