Skip to content

Instantly share code, notes, and snippets.

@mkocabas
Last active February 6, 2018 08:55
Show Gist options
  • Save mkocabas/62dcd2f14ad21f3b25eac2d39ec2cc95 to your computer and use it in GitHub Desktop.
Save mkocabas/62dcd2f14ad21f3b25eac2d39ec2cc95 to your computer and use it in GitHub Desktop.
def focal_loss(gamma=2, alpha=0.75):
def focal_loss_fixed(y_true, y_pred): # compatible with tf backend
pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))
pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))
return -K.sum(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1))-K.sum((1-alpha) * K.pow( pt_0, gamma) * K.log(1. - pt_0))
return focal_loss_fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment