From 85463a37dfb95bdeeeb8c723a190b259bfad12d6 Mon Sep 17 00:00:00 2001 From: Mayuresh Dharwadkar <98738585+Mayureshd-18@users.noreply.github.com> Date: Sat, 15 Jun 2024 16:39:03 +0530 Subject: [PATCH 1/4] Create TripletLoss.py --- .../RankingLosses/TripletLoss/TripletLoss.py | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ML/Algorithms/Losses/RankingLosses/TripletLoss/TripletLoss.py diff --git a/ML/Algorithms/Losses/RankingLosses/TripletLoss/TripletLoss.py b/ML/Algorithms/Losses/RankingLosses/TripletLoss/TripletLoss.py new file mode 100644 index 0000000..d1c1daa --- /dev/null +++ b/ML/Algorithms/Losses/RankingLosses/TripletLoss/TripletLoss.py @@ -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) From bcb4b11bd179bb3927c1ba9100ea3752e5ee279a Mon Sep 17 00:00:00 2001 From: Mayuresh Dharwadkar <98738585+Mayureshd-18@users.noreply.github.com> Date: Sat, 15 Jun 2024 16:39:59 +0530 Subject: [PATCH 2/4] Create PairWiseRankingLoss.py --- .../PairWiseRankingLoss.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ML/Algorithms/Losses/RankingLosses/PairWiseRankingLoss/PairWiseRankingLoss.py diff --git a/ML/Algorithms/Losses/RankingLosses/PairWiseRankingLoss/PairWiseRankingLoss.py b/ML/Algorithms/Losses/RankingLosses/PairWiseRankingLoss/PairWiseRankingLoss.py new file mode 100644 index 0000000..5e0d5a9 --- /dev/null +++ b/ML/Algorithms/Losses/RankingLosses/PairWiseRankingLoss/PairWiseRankingLoss.py @@ -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) From a7e64070d7476fbbaeb6501adc7170102cab62b2 Mon Sep 17 00:00:00 2001 From: Mayuresh Dharwadkar <98738585+Mayureshd-18@users.noreply.github.com> Date: Sat, 15 Jun 2024 16:43:55 +0530 Subject: [PATCH 3/4] Update README.md --- ML/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ML/README.md b/ML/README.md index 4e85be8..be8088c 100644 --- a/ML/README.md +++ b/ML/README.md @@ -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 From 96a156e2e2b467aa3ced7f45e6bb2b6786293fe3 Mon Sep 17 00:00:00 2001 From: Mayuresh Dharwadkar <98738585+Mayureshd-18@users.noreply.github.com> Date: Sat, 15 Jun 2024 16:45:09 +0530 Subject: [PATCH 4/4] Update README.md --- ML/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ML/README.md b/ML/README.md index be8088c..8639b67 100644 --- a/ML/README.md +++ b/ML/README.md @@ -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 | [Triplet Loss](./Algorithms/Losses/RankingLosses/TripletLoss) | 9 | [Pairwise Ranking Loss](./Algorithms/Losses/RankingLosses/PairwiseRankingLoss) | +| 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