-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1605a4
commit 7e51bbb
Showing
5 changed files
with
43 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,21 @@ | ||
from .cross_entropy_loss import cross_entropy_loss | ||
from jax import Array | ||
import jax.numpy as jnp | ||
import optax | ||
|
||
def cross_entropy_loss(logits: Array, labels: Array, *, mask: Array) -> Array: | ||
''' | ||
Computes the mean softmax cross entropy loss for the provided logits and labels, only considering elements where the mask is True. | ||
Args: | ||
logits (Array): The model's predictions. Typically an array of shape (batch_size, seq_len, vocab_size). | ||
labels (Array): The true labels. Typically an array of shape (batch_size, seq_len). | ||
mask (Array): Typically a boolean array of shape (batch_size, seq_len). Specifies which elements of the loss array should be included in the mean loss calculation. | ||
Returns: | ||
Array: The mean masked softmax cross entropy loss. | ||
Notes: | ||
The mask array typically specifies which elements of the sequence are actual tokens as opposed to padding tokens, so the loss is not calculated over padding tokens. | ||
''' | ||
loss = optax.softmax_cross_entropy_with_integer_labels(logits=logits, labels=labels) | ||
return jnp.mean(loss, where=mask) |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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