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

Use the Keras set_random_seed in tests #30504

Merged
merged 1 commit into from
Apr 26, 2024

Conversation

Rocketknight1
Copy link
Member

Some tests load checkpoints where weights are missing (e.g. the pooler weights for ViT). To get reproducible results, we need to ensure that seeds are set consistently. This PR uses keras.set_random_seed() instead of tf.set_seed(), which fixes some issues the CI was seeing where models mismatched because of differing weight inits.

from transformers.pipelines import SUPPORTED_TASKS

set_seed_fn = lambda: tf.random.set_seed(0) # noqa: E731
set_seed_fn = lambda: keras.utils.set_random_seed(0) # noqa: E731
Copy link
Collaborator

@amyeroberts amyeroberts Apr 26, 2024

Choose a reason for hiding this comment

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

So tf.random.set_seed doesn't actually set the seed for the whole TF environment?

Does keras.utils.set_random_seed control just keras objects, or the whole TF env too?

Copy link
Member Author

@Rocketknight1 Rocketknight1 Apr 26, 2024

Choose a reason for hiding this comment

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

As far as I can tell, keras.utils.set_random_seed() basically setstf.random.set_seed() as before, but also sets the Python and Numpy random seeds. It's the currently recommended approach for reproducible initialization in the Keras docs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hope there is no other tests having the same init issue but no failure by luck 🤞 .
(otherwise we might get different results for them after this PR 😅 )

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not surprised it's the recommended one in the keras docs ;)

OK, well I just we just shrug and put it in the list of other TF quirks

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

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

thanks @Rocketknight1

(is this a API changes causing the problem?)

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks for fixing!

@Rocketknight1 Rocketknight1 merged commit 2de5cb1 into main Apr 26, 2024
18 checks passed
@Rocketknight1 Rocketknight1 deleted the fix_tf_reproducible_initialization branch April 26, 2024 15:14
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.

None yet

4 participants