Skip to content

Commit

Permalink
Merge pull request #44 from Mayureshd-18/mayud
Browse files Browse the repository at this point in the history
Added Ranking Losses
  • Loading branch information
Avdhesh-Varshney authored Jun 16, 2024
2 parents 12bca60 + 96a156e commit 95a48b5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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/RankingLosses/TripletLoss/TripletLoss.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)
2 changes: 1 addition & 1 deletion ML/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
|-------|-----------|-------|-----------|-------|-----------|
| 1 | [Mean Squared Error](./Algorithms/Losses/MeanSquaredError) | 2 | [R2 Squared](./Algorithms/Losses/R2Squared) | 3 | [Cross Entropy Loss](./Algorithms/Losses/CrossEntropyLoss) |
| 4 | [Hinge Loss](./Algorithms/Losses/HingeLoss) | 5 | [Kullback Leibler (KL) Divergence Loss](./Algorithms/Losses/KullbackLeiblerDivergenceLoss) | 6 | [Mean Absolute Error](./Algorithms/Losses/MeanAbsoluteError) |
| 7 | [Root Mean Squared Error](./Algorithms/Losses/RootMeanSquaredError) | 8 | | 9 | |
| 7 | [Root Mean Squared Error](./Algorithms/Losses/RootMeanSquaredError) | 8 | [Triplet Loss](./Algorithms/Losses/RankingLosses/TripletLoss) | 9 | [Pairwise Ranking Loss](./Algorithms/Losses/RankingLosses/PairWiseRankingLoss) |


## Available Documentations
Expand Down

0 comments on commit 95a48b5

Please sign in to comment.