Skip to content

Use RandomState instead of global random seed in Orthogonal initializer #12232

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

Closed
wants to merge 1 commit into from

Conversation

vikua
Copy link
Contributor

@vikua vikua commented Feb 8, 2019

Summary

np.random.seed modifies global seed that other packages or programs where Keras is being imported into have access to. This may lead to confusion and errors.

Related Issues

PR Overview

It looks like seeding random globally is not required in Orthogonal initializer.
Using np.random.RandomState instead of global np.random.seed to generate normal distribution.

  • This PR requires new unit tests [y/n] (make sure tests are included)
  • This PR requires to update the documentation [y/n] (make sure the docs are up-to-date)
  • [y] This PR is backwards compatible [y/n]
  • This PR changes the current API [y/n] (all API changes need to be approved by fchollet)

@gabrieldemarmiesse
Copy link
Contributor

np.random.seed modifies global seed that other packages or programs where Keras is being imported into have access to. This may lead to confusion and errors.

I'm not sure I understand.

If we follow your reasonning, which packages should use np.random.seed and which packages shouldn't?

fchollet
fchollet previously approved these changes Feb 8, 2019
Copy link
Collaborator

@fchollet fchollet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the PR!

@fchollet
Copy link
Collaborator

fchollet commented Feb 8, 2019

I'm not sure I understand.

Ideally, calling black-box Keras functions shouldn't modify the randomness behavior of downstream calls to Numpy that are unrelated to Keras. So we should avoid changing the global seed if possible.

@gabrieldemarmiesse
Copy link
Contributor

Ok that makes sense.

@vikua vikua force-pushed the vikua/orthogonal-global-seed branch from f44cbc0 to df14783 Compare February 8, 2019 20:27
@vikua
Copy link
Contributor Author

vikua commented Feb 8, 2019

@fchollet PR is green now.
The change in accuracy in test_temporal_classification_functional can be explained by the fact that previously orthogonal initializer was using random state after it has been modified by get_test_data (and maybe something else), but now it uses fresh random state.

@fchollet
Copy link
Collaborator

fchollet commented Feb 9, 2019

If I'm not mistaken, this PR would have the unintended consequence that people using np.random.seed at the start of their program for determinism would no longer have deterministic initializers, right?

@vikua
Copy link
Contributor Author

vikua commented Feb 9, 2019

Yes, I'm afraid this is true.

@fchollet
Copy link
Collaborator

I think we should prioritize this use case over "Keras should not modify the global random state".

I think it might be possible to have both, if we use the global random state to generate a local one?

@vikua
Copy link
Contributor Author

vikua commented Feb 12, 2019

Interesting idea, let me try that out.

@vikua vikua force-pushed the vikua/orthogonal-global-seed branch 4 times, most recently from 9a8094b to 68ced41 Compare February 12, 2019 10:04
@vikua
Copy link
Contributor Author

vikua commented Feb 12, 2019

Yeah, the original test passes as before with global random seed. Also added another test to verify initializer with local random seed.

@YuriyGuts
Copy link
Contributor

I have a very similar PR that also removes global seeding from the dataset readers, as well as the orthogonal initializer. Sorry I haven't discovered this PR earlier because I looked for this issue in the tickets rather than PRs. #12259

@vikua
Copy link
Contributor Author

vikua commented Feb 21, 2019

Closing, reason is -> #12259 (comment)

@vikua vikua closed this Feb 21, 2019
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 this pull request may close these issues.

4 participants