Skip to content

Instantly share code, notes, and snippets.

@edumunozsala
Created October 11, 2020 16:15
Show Gist options
  • Save edumunozsala/9521a049ba0bc9c431576131e7085fb8 to your computer and use it in GitHub Desktop.
Save edumunozsala/9521a049ba0bc9c431576131e7085fb8 to your computer and use it in GitHub Desktop.
Create a custom loss and accuracy functions for the seq2seq model
def loss_func(targets, logits):
crossentropy = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True)
# Mask padding values, they do not have to compute for loss
mask = tf.math.logical_not(tf.math.equal(targets, 0))
mask = tf.cast(mask, dtype=tf.int64)
# Calculate the loss value
loss = crossentropy(targets, logits, sample_weight=mask)
return loss
def accuracy_fn(y_true, y_pred):
# y_pred shape is batch_size, seq length, vocab size
# y_true shape is batch_size, seq length
pred_values = K.cast(K.argmax(y_pred, axis=-1), dtype='int32')
correct = K.cast(K.equal(y_true, pred_values), dtype='float32')
# 0 is padding, don't include those
mask = K.cast(K.greater(y_true, 0), dtype='float32')
n_correct = K.sum(mask * correct)
n_total = K.sum(mask)
return n_correct / n_total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment