Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass key instead of seed to algorithms with randomization #103

Closed
msluszniak opened this issue May 19, 2023 · 17 comments · Fixed by #105
Closed

Pass key instead of seed to algorithms with randomization #103

msluszniak opened this issue May 19, 2023 · 17 comments · Fixed by #105

Comments

@msluszniak
Copy link
Contributor

If we want this functionality I can implement this.

@josevalim
Copy link
Contributor

Let's try to apply this to one model and see how it looks like? Perhaps we can allow one of key or seed to be given?

@polvalente
Copy link
Contributor

I'd rather just allow key for uniformity, because that's our equivalent of seed in Nx land

@josevalim
Copy link
Contributor

@polvalente but it makes the API harder to use for everyone, doesn't it?

@polvalente
Copy link
Contributor

I don't think it does. If the key is optional, it's just what we have now.

If we accept a seed, it's much harder to use than a key in terms of defn usage because the natural System.system_time call isn't available as well.

I don't see any normal use case where a seed is really needed instead of a key.

Also, we need to either return the key or teach people to split the key beforehand.

@josevalim
Copy link
Contributor

Also, we need to either return the key or teach people to split the key beforehand.

That's exactly the point of how it makes it harder. For example, if you write a notebook, you may want to hardcode the seed in the notebook. But now we have to tell them to create a key before hand and split it?

@polvalente
Copy link
Contributor

Split is only needed if you don't return the key, which I think is the preferred approach.

@josevalim
Copy link
Contributor

Right, so we either have to return the key in all APIs or the user has to split before hand instead of passing a seed. All of those look like more work to me than just passing an integer. :) Plus, we may end-up generating the key outside of a defn, which will affect performance. So I think supporting both is the best of both worlds?

@polvalente
Copy link
Contributor

How does generating the key outside of defn affect performance?

@josevalim
Copy link
Contributor

@polvalente all of the Nx code to generate the key will be executed outside of defn and therefore it won't be compiled?

@josevalim
Copy link
Contributor

Nah, ignore me. key is very cheap to compute. :)

@josevalim
Copy link
Contributor

So I guess for the cases we don't need to split, we just need to replace seed: 42 by key: Nx.Random.key(42)? If that's the case, rolling with :key is 100% fine by me.

@polvalente
Copy link
Contributor

key must be a tensor input, though, because it is split and carried over multiple computations

@josevalim
Copy link
Contributor

Can you please expand, so we are in the same page?

@polvalente
Copy link
Contributor

Imagine we're using one of Scholar's algorithms that needs to receive a random key in the middle of some defn code.

If the previous code already used the key, it'll have a computation graph associated with it, even if it's just a sequence of splits.

And from my understanding, passing that as an option would be bad for the Nx compiler.

@josevalim
Copy link
Contributor

And from my understanding, passing that as an option would be bad for the Nx compiler.

We handle this today in Scholar by defining all fit functions as transform and we convert options into inputs.

@polvalente
Copy link
Contributor

Nice, so I'm ok with having key as option!

@josevalim
Copy link
Contributor

Alright, so :key as an option it is!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants