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.

`random.split` should support splitting starting at a particular index, such as `step` number

See original GitHub issue

Background: during training, we can deterministically derive the PRNG seed from the current step number, by seed=hash(step_number). This can sometimes be convenient because it avoids the need to checkpoint the PRNG seed, as the step number is all the state that is needed.

The computation seed=hash(step_number) can be implemented, albeit inefficiently, in JAX:

key_this_step = jax.random.split(global_key, step_number + 1)[step number]

Sadly, this is inefficient in that it materializes an intermediate array of length step_number + 1, even though we only want to access the last element of that array. Given that all of the elements of jax.random.split are computed in parallel anyway, it would be nice to directly have access to the nth split of jax.random.split rahter than having to produce all of them and selecting one.

One way to integrate this into the API is to add a parameter slice=None to jax.random.split, whose behavior is defined by ensuring that the following equivalence holds:

jax.random.split(key, slice=(lo, hi)) == jax.random.split(key, num)[lo:hi]

I note that there are other requests for extensions to jax.random.split, such as https://github.com/google/jax/issues/4013. Another alternative, which seems flexible enough to serve everyone’s need, would be to directly expose the underlying hash function as a function jax.random.hash(key, index), defined so that the following equivalence holds:

jax.random.split(key, num) == jax.random.hash(key, jax.lax.iota(jnp.uint32, num))

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
reinerpcommented, Aug 31, 2021

random.fold_in(key, step) looks great, thanks for pointing me to i!

1reaction
jakevdpcommented, Aug 30, 2021

Thanks for the suggestion! This is a really good time to bring this up, because @froystig has just finished a refactoring of the PRNG code so that users can override the API, including _split. This means that if we are planning to make any changes to what options _split might accept, now would be the time (before downstream libraries implement their own random generators with their own split methods).

I wonder if @froystig has thoughts about this request, and the one in #4013?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Train-Test Split for Evaluating Machine Learning Algorithms
This is to ensure that the train and test datasets are representative of the original dataset.
Read more >
Split Your Dataset With scikit-learn's train_test_split()
Splitting your dataset is essential for an unbiased evaluation of prediction performance. In most cases, it's enough to split your dataset randomly into...
Read more >
3 Things You Need To Know Before You Train-Test Split
You do a simple train-test split that does a random split totally disregarding the distribution or proportions of the classes.
Read more >
machine learning - Train/Test/Validation Set Splitting in Sklearn
First to split to train, test and then split train again into validation and ... not splitting it once but in a first...
Read more >
4 Simple Ways to Split a Decision Tree in Machine Learning
Decision Tree Splitting Method #1: Reduction in Variance · For each split, individually calculate the variance of each child node · Calculate the ......
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