Skip to content

Instantly share code, notes, and snippets.

@ChunML
Created April 28, 2019 12:02
Show Gist options
  • Save ChunML/34174eafb41cce35a5b7cb2fdfe3dc5c to your computer and use it in GitHub Desktop.
Save ChunML/34174eafb41cce35a5b7cb2fdfe3dc5c to your computer and use it in GitHub Desktop.
crossentropy = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True)
def loss_func(targets, logits):
mask = tf.math.logical_not(tf.math.equal(targets, 0))
mask = tf.cast(mask, dtype=tf.int64)
loss = crossentropy(targets, logits, sample_weight=mask)
return loss
optimizer = tf.keras.optimizers.Adam()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment