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

Ensure metric results are JSON-serializable #10632

Merged
merged 1 commit into from Mar 11, 2021
Merged

Ensure metric results are JSON-serializable #10632

merged 1 commit into from Mar 11, 2021

Conversation

sgugger
Copy link
Collaborator

@sgugger sgugger commented Mar 10, 2021

What does this PR do?

Metrics returned from numpy (with an np.mean() for instance) are not real Python floats but np.float32 (or other type) objects that are not serializable. This causes problems when the metrics are saved in JSON format in the Trainer, for instance when using load_best_model_at_end. This PR fixes that by recursively applying a .item() on the metrics dictionary.

Fixes #10299

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

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

Yes, very clean! Thanks @sgugger

@LysandreJik LysandreJik merged commit 63c295a into master Mar 11, 2021
@LysandreJik LysandreJik deleted the fix_10299 branch March 11, 2021 14:00
Iwontbecreative pushed a commit to Iwontbecreative/transformers that referenced this pull request Jul 15, 2021
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.

Object of type 'int64' is not JSON serializable in Trainer.save_checkpoint
2 participants