-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
API: sparse.linalg.svds: transition to rng (SPEC 7) #21899
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
Conversation
There's a doc_string test (line ~292 in >>> rng = np.random.RandomState(0)
>>> X_dense = rng.random(size=(100, 100))
>>> X_dense[:, 2 * np.arange(50)] = 0
>>> X = sparse.csr_array(X_dense)
>>> _, singular_values, _ = svds(X, k=5, random_state=rng)
>>> print(singular_values)
[ 4.3293... 4.4491... 4.5420... 4.5987... 35.2410...] I could change the first line I've shown here to use Do I understand correctly? Let me know if I should change the test from using RandomState to using default_rng. |
Please do change it, actually. It is good to do it now because we want new users who refer to the examples to use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks close. Besides the comments above, I'd just like to preserve at least one use of random_state
as a sanity check that the decorator is doing its job.
I think this addresses all points brought up -- and lead to a couple others which were easy to fix. One last point is that if random_array PR gh-21888 gets merged first, this one could change quite a few |
I made all all svds tests deterministric and all propack tests that directly call Every function call to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! I've suggested marking the one remaining use of random_state
in a test so it's obvious that it's not a mistake, and there are two other little things, but I think we can merge this shortly. I do think this will merge before gh-21888, if that's OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the rng normalization from _svdp
and replaced it with a check that rng
input is a Generator
instance. I updated the tests and I think we're all ready to go here.
I had to move the sanity check of random_state=
kwarg to a different test that doesn't use _svdp
. :}
@dschult I'm not sure I understand - does |
Oops.. somehow I didn't commit my last set of changes.
Just to be clear: It should work with a RandomState or a Generator instance as |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I put code to check if rng is None and raise a ValueError if that happens. But it would only happen if someone if directly using the private function.
OK. Note that it is not required to provide helpful error messages to users in private functions. That is why I have been advocating for removing it entirely. But if you think it might be helpful to developers in the future, this isn't causing problems, so I wouldn't ask for it to be changed now.
Let's see if tests come back green, and if so, I'll go ahead and merge.
Thanks @dschult!
I am bit confused with this one here, I am receiving some errors while trying to enable the rng in #22748 but the |
Immediately changing to that behavior would have been backward incompatible. The decorator |
True but if |
It depends on if the global seed has been set. If so, it needs to use the global |
Ah OK so this is from the |
Reference issue
Toward #21833
What does this implement/fix?
This allows for use of new keyword rng alongside random_state in sparse.linalg.svds to prepare for the deprecation and removal of random_state and its legacy behavior.
In short:
if you pass the RNG argument by position or keyword random_state, it should work however it did before
if you pass a value val by keyword rng, it should behave as if you had passed np.random.default_rng(val) into the function.