Skip to content

Instantly share code, notes, and snippets.

@Shivam-316
Created November 17, 2020 17:11
Show Gist options
  • Save Shivam-316/48a534b3b75f628cb905ec568205d20d to your computer and use it in GitHub Desktop.
Save Shivam-316/48a534b3b75f628cb905ec568205d20d to your computer and use it in GitHub Desktop.
optimizer=keras.optimizers.Adam()
loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True,reduction='none')
def loss_fn(true,pred):
mask = tf.math.logical_not(tf.math.equal(true, 0))
loss_=loss(true,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