Skip to content

Instantly share code, notes, and snippets.

@benbotto
Created February 12, 2018 21:34
Show Gist options
  • Save benbotto/204707cd5562d107b4da3b76632467b1 to your computer and use it in GitHub Desktop.
Save benbotto/204707cd5562d107b4da3b76632467b1 to your computer and use it in GitHub Desktop.
huber_loss for Tensorflow Keras
def huber_loss(y_true, y_pred, clip_delta=1.0):
error = y_true - y_pred
cond = tf.keras.backend.abs(error) < clip_delta
squared_loss = 0.5 * tf.keras.backend.square(error)
linear_loss = clip_delta * (tf.keras.backend.abs(error) - 0.5 * clip_delta)
loss = tf.where(cond, squared_loss, linear_loss)
return tf.keras.backend.mean(loss)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment