Skip to content

Instantly share code, notes, and snippets.

@panchishin
Last active November 26, 2020 05:49
Show Gist options
  • Save panchishin/35e3f224108f3e47343748d7241ddbc4 to your computer and use it in GitHub Desktop.
Save panchishin/35e3f224108f3e47343748d7241ddbc4 to your computer and use it in GitHub Desktop.
top k loss
def top_k_loss(k=25):
@tf.function
def loss(y_true, y_pred):
y_error_of_true = tf.keras.losses.categorical_crossentropy(y_true=y_true,y_pred=y_pred)
topk, indexs = tf.math.top_k( y_error_of_true, k=tf.minimum(k, y_true.shape[0]) )
return topk
return loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment