Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ranking losses #35

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import tensorflow as tf
from typing import Tuple

def pairwise_ranking_loss(y_true: tf.Tensor, y_pred: tf.Tensor, margin: float = 1.0) -> tf.Tensor:
"""
Computes the pairwise ranking loss for a batch of pairs.

Args:
y_true: Tensor of true labels (0 for negative pairs, 1 for positive pairs).
y_pred: Tensor of predicted similarities/distances, expected to be a tensor of shape (batch_size, 2, embedding_dim) where
y_pred[:, 0] is the anchor and y_pred[:, 1] is the positive/negative.
margin: Margin parameter for the pairwise ranking loss.

Returns:
loss: Computed pairwise ranking loss as a scalar tensor.
"""
anchor, positive_or_negative = y_pred[:, 0], y_pred[:, 1]

distances = tf.reduce_sum(tf.square(anchor - positive_or_negative), axis=-1)
positive_loss = y_true * distances
negative_loss = (1 - y_true) * tf.maximum(margin - distances, 0.0)

loss = positive_loss + negative_loss
return tf.reduce_mean(loss)

# Example usage:
# model.compile(optimizer='adam', loss=pairwise_ranking_loss)
26 changes: 26 additions & 0 deletions ML/Algorithms/Losses/Ranking losses/Triplet Loss/Triplet_loss.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import tensorflow as tf
from typing import Tuple

def triplet_loss_func(y_true: tf.Tensor, y_pred: tf.Tensor, alpha: float = 0.3) -> tf.Tensor:
"""
Computes the triplet loss for a batch of triplets.

Args:
y_true: True values of classification (unused in this implementation, typically required for compatibility with Keras).
y_pred: Predicted values, expected to be a tensor of shape (batch_size, 3, embedding_dim) where
y_pred[:, 0] is the anchor, y_pred[:, 1] is the positive, and y_pred[:, 2] is the negative.
alpha: Margin parameter for the triplet loss.

Returns:
loss: Computed triplet loss as a scalar tensor.
"""
anchor, positive, negative = y_pred[:, 0], y_pred[:, 1], y_pred[:, 2]

positive_dist = tf.reduce_sum(tf.square(anchor - positive), axis=-1)
negative_dist = tf.reduce_sum(tf.square(anchor - negative), axis=-1)

loss = tf.maximum(positive_dist - negative_dist + alpha, 0.0)
return tf.reduce_mean(loss)

# Example usage:
# model.compile(optimizer='adam', loss=triplet_loss_func)