Skip to content

Instantly share code, notes, and snippets.

@doleron
Created April 9, 2023 01:25
Show Gist options
  • Save doleron/fa03e8112cbed32dc8875348bb527024 to your computer and use it in GitHub Desktop.
Save doleron/fa03e8112cbed32dc8875348bb527024 to your computer and use it in GitHub Desktop.
def contrastive_loss_with_margin(margin=1):
def contrastive_loss(y_true, y_pred):
square_pred = K.square(y_pred)
margin_square = K.square(K.maximum(margin - y_pred, 0))
return (y_true * square_pred + (1 - y_true) * margin_square)
return contrastive_loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment