Skip to content

Instantly share code, notes, and snippets.

@nahidalam
Created August 20, 2020 04:05
Show Gist options
  • Save nahidalam/588ab134c41ef84d9afa310def5a4fa9 to your computer and use it in GitHub Desktop.
Save nahidalam/588ab134c41ef84d9afa310def5a4fa9 to your computer and use it in GitHub Desktop.
optimizer = tf.keras.optimizers.Adam()
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction='none')
def loss_function(real, pred):
mask = tf.math.logical_not(tf.math.equal(real, 0))
loss_ = loss_object(real, pred)
mask = tf.cast(mask, dtype=loss_.dtype)
loss_ *= mask
return tf.reduce_mean(loss_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment