Perplexity: add clipping and from_logits#47
Merged
jshin1394 merged 1 commit intogoogle:mainfrom Mar 31, 2025
Merged
Conversation
Collaborator
Author
|
Looking into the test failures... looks like it only fails when the whole test suite is run? |
It was pointed out that Perplexity returns NaNs for negative values. This is because our implementation did not clip logit values to [0, 1], whereas the Keras implementation does. [1] Even with that fix, the tests were failing because Keras defaults to the TensorFlow version of the metric, which applies softmax to the outputs unconditionally [2], unlike the JAX implementation which does not. [3] I also added a `from_logits` arg, similar to Keras, for users who want to pass raw logits and have us apply softmax internally. [1] https://github.com/keras-team/keras/blob/3f8b065e82b17884bd43fcfbd4bd79f18a7019fe/keras/src/backend/jax/nn.py#L582 [2] https://www.tensorflow.org/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits [3] https://github.com/keras-team/keras/blob/3f8b065e82b17884bd43fcfbd4bd79f18a7019fe/keras/src/backend/jax/nn.py#L578-L579
4151482 to
dedc238
Compare
Collaborator
Author
|
Found the issue - when Keras is imported in other test files first it doesn't have |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It was pointed out that Perplexity returns NaNs for negative values. This is because our implementation did not clip logit values to [0, 1], whereas the Keras implementation does. [1]
Even with that fix, the tests were failing because Keras defaults to the TensorFlow version of the metric, which applies softmax to the outputs unconditionally [2], unlike the JAX implementation which does not. [3]
Also:
from_logitsarg, similar to Keras, for users who want to pass raw logits and have us apply softmax internally.[1] https://github.com/keras-team/keras/blob/3f8b065e82b17884bd43fcfbd4bd79f18a7019fe/keras/src/backend/jax/nn.py#L582
[2] https://www.tensorflow.org/api_docs/python/tf/nn/sparse_softmax_cross_entropy_with_logits
[3] https://github.com/keras-team/keras/blob/3f8b065e82b17884bd43fcfbd4bd79f18a7019fe/keras/src/backend/jax/nn.py#L578-L579