Introduction

In this document, we explore various loss functions commonly used in Natural Language Processing (NLP), Search, and Recommendation systems. These loss functions play a crucial role in optimizing models for specific tasks, such as classification, ranking, and regression. We will delve into their formulas, explanations, their application scenarios, and highlight some key differences based on the discussions.

1. Cross-Entropy Loss (CE)

Cross-Entropy Loss is widely used in classification tasks, particularly for multi-class classification. It measures the difference between the predicted probability distribution and the actual distribution.

Formula:

L = -sum(y_i * log(p_i))

Explanation: Cross-Entropy Loss calculates the difference between the true label (y_i) and the predicted probability (p_i) by taking the logarithm of the predicted probability and summing across all classes. This loss function is used to optimize the model to maximize the probability of the correct class.

Key Insight: The core distinction in classification tasks is that the model needs to identify the highest probability class (using argmax), unlike ranking tasks where the model focuses on comparing scores across items.

2. KL Divergence (KL)

KL Divergence is used to measure the difference between two probability distributions. It is often used in situations where we want the predicted distribution to match a target distribution as closely as possible.

Formula:

KL(P || Q) = sum(P(i) * log(P(i) / Q(i)))

Explanation: KL Divergence calculates the relative entropy between the target distribution P and the predicted distribution Q. It is non-negative and equals zero only when P and Q are identical.

Key Insight: KL Divergence considers the entire distribution, computing the weighted sum of log differences, which ensures the result is non-negative. This is different from cross-entropy, which focuses on specific categories.

3. Binary Cross-Entropy Loss (BCE)

Binary Cross-Entropy Loss is used for binary classification tasks. It is a special case of Cross-Entropy Loss designed for two classes.

Formula:

L = -1/N sum(y_i * log(p_i) + (1 - y_i) * log(1 - p_i))

Explanation: BCE Loss calculates the difference between the true binary labels and the predicted probabilities. It considers both positive and negative class probabilities.

Key Insight: BCE can be viewed as a specialized version of CE, which includes additional terms to account for both the positive and negative classes.

4. Contrastive Loss

Contrastive Loss is used in tasks where we need to learn a feature space where similar items are closer and dissimilar items are farther apart. It is commonly used in recommendation systems.

Formula:

L = 1/2N sum(y_i * max(0, D(x_i, x_j)^2 - m^2) + (1 - y_i) * D(x_i, x_j)^2)

Explanation: Contrastive Loss minimizes the distance between similar pairs and maximizes the distance between dissimilar pairs.

5. Triplet Loss

Triplet Loss is used in ranking tasks and is designed to learn a feature space where the anchor is closer to positive examples than negative examples.

Formula:

L = 1/N sum(D(x_a^i, x_p^i) - D(x_a^i, x_n^i) + alpha)_+

Explanation: Triplet Loss minimizes the distance between the anchor and the positive example while maximizing the distance between the anchor and the negative example, with a margin alpha.

6. NT-Xent Loss / InfoNCE

NT-Xent Loss (Normalized Temperature-scaled Cross Entropy Loss) or InfoNCE is commonly used in contrastive learning settings, such as in SimCLR and MoCo.

Formula:

L = -log(exp(sim(z_i, z_j) / tau) / sum(exp(sim(z_i, z_k) / tau) for all k != i))

Explanation: This loss function aims to maximize the similarity between positive pairs while minimizing the similarity between the positive pair and all other negative examples in the batch.

7. Ranking Losses

Ranking losses are specifically designed to optimize the relative order of items in search and recommendation tasks. Examples include RankNet Loss, ListNet Loss, and Hinge Loss.

7.1 RankNet Loss

Formula:

L = -sum(log(sigma(s_i - s_j)))

Explanation: RankNet Loss uses a sigmoid function to measure the difference between the scores of relevant and irrelevant items and aims to minimize this difference.

Key Insight: RankNet Loss is similar to KL Divergence in that it compares the relative difference between pairs of scores, focusing on making relevant items rank higher than irrelevant ones.

7.2 ListNet Loss

Formula:

L = -sum(softmax(y_i) * log(softmax(s_i)))

Explanation: ListNet Loss applies softmax normalization to both the predicted and true scores and computes the cross-entropy between them.

Key Insight: ListNet Loss is akin to Cross-Entropy Loss but is used in the context of ranking, where it compares entire lists of items rather than individual classes.

7.3 Hinge Loss

Formula:

L = sum(max(0, 1 - y_i * f(x_i)))

Explanation: Hinge Loss, commonly used in SVMs, aims to ensure that the correct items are ranked higher than incorrect ones by a margin.

Conclusion

This document provided an overview of various loss functions used in NLP, search, and recommendation systems. Understanding these loss functions and their applications is essential for optimizing models for specific tasks.

Core Difference Summary:

  • Classification tasks typically focus on identifying the highest probability class using argmax.
  • Ranking tasks require comparing scores across items, hence the use of softmax and sigmoid to handle these comparisons effectively.

<
Previous Post
Llm Powered Agent
>
Next Post
Scaling Test Time Compute 核心问题、解决方法、技术对比与总结