---
title: Bilateral Softmax Loss (BSL)
url: https://www.emergentmind.com/topics/bilateral-softmax-loss-bsl
type: topic
---

# Bilateral Softmax Loss (BSL)

Bilateral Softmax Loss (BSL) is a principled loss function that builds on the conventional sampled softmax loss by symmetrically applying its robustifying structure to both positive and negative samples. BSL provides superior accuracy, robustness to label noise, and fairness by explicitly incorporating distributionally robust optimization (DRO) and variance penalization for both positive and negative data, with notable applications in recommendation systems and deep feature learning for classification. Its implementation requires minimal modification of existing code using softmax-based losses, making it attractive for practical adoption across a variety of architectures and data scenarios [2312.12882, 2206.11589].

## 1. Formal Definition and Mathematical Formulation

Let $f(u,i)\in\mathbb{R}$ denote a model score for user $u$ and item $i$. Define distributions $P^+_u$ and $P^-_u$ over positive and negative items (typically uniform on observed positives and sampled negatives).

The sampled softmax loss (SL) is:
$$
L_{SL}(u) = - \mathbb{E}_{i\sim P^+_u}\left[ f(u,i) \right] + \tau \cdot \log \mathbb{E}_{j\sim P^-_u}\left[ \exp(f(u,j)/\tau) \right]
$$
or, in normalized fashion:
$$
L_{SL}(u) = - \mathbb{E}_{i\sim P^+_u}\left[ \log \frac{\exp(f(u,i)/\tau)}{ \exp(f(u,i)/\tau) + N^- \mathbb{E}_{j\sim P^-_u}\left[\exp(f(u,j)/\tau)\right]} \right]
$$

Bilateral Softmax Loss (BSL) augments SL by applying the log-expectation-exp (log-sum-exp) structure to the positive term as well, with separate temperatures $\tau_1$ (positive) and $\tau_2$ (negative):
$$
L_{BSL}(u) = -\tau_1 \cdot \log \mathbb{E}_{i\sim P^+_u}\left[ \exp(f(u,i)/\tau_1) \right] + \tau_2 \cdot \log \mathbb{E}_{j\sim P^-_u}\left[ \exp(f(u,j)/\tau_2) \right]
$$
For sampled positives $S^+_u$ and negatives $S^-_u$:
$$
L_{BSL}(u) = -\tau_1 \log \left( \frac{1}{|S^+_u|} \sum_{i\in S^+_u} \exp( f(u,i)/\tau_1 ) \right)  + 
\tau_2 \log \left( \frac{1}{|S^-_u|} \sum_{j\in S^-_u} \exp( f(u,j)/\tau_2 ) \right)
$$
Standard SL is recovered as a special case when $\tau_1\to\infty$ and $\tau_2=\tau$ [2312.12882].

## 2. Theoretical Underpinnings: DRO and Variance Penalization

SL can be interpreted as a worst-case optimization (DRO), providing robustness to distribution shifts and noise in negatives:
$$
\min_\theta \left\{ -\mathbb{E}_{i\sim P^+_u}[f(u,i)] + \max_{Q: D_{KL}(Q\|P^-_u)\leq \eta } \mathbb{E}_{j\sim Q}[f(u,j)] \right\}
$$
The log-sum-exp negative term exactly realizes this DRO objective with $\tau$ as the Lagrange multiplier.

BSL extends this DRO principle to positives, introducing a symmetric worst-case (max-over-KL-ball) reweighting around $P^+_u$. This guards against both mislabeled negatives and positives, yielding robustness to both spurious negative and positive signals [2312.12882].

A second-order Taylor expansion for the log-sum-exp term reveals that both SL and BSL introduce implicit variance penalties:
$$
\tau \cdot \log \mathbb{E}[\exp(f/\tau)] = \mathbb{E}[f] + \frac{\operatorname{Var}(f)}{2\tau} + o(1/\tau)
$$
SL thus penalizes the variance among negative scores, shrinking the prediction gap between popular/unpopular items. BSL introduces a matching variance penalty term for positives, regularizing overconfidence in positive predictions and increasing robustness to noisy positives [2312.12882].

## 3. BSL in Margin-Based and Feature-Learning Frameworks

Recent work unifies BSL with margin-based deep feature learning objectives by defining two key geometric quantities:

- **Class margin** ($m_c$): Smallest angular separation between class prototypes.
- **Sample margin** ($\gamma(x,y)$): Logit gap between the true class and the highest competing class for a sample.

A generalized margin softmax (GM-Softmax) objective incorporates affine transforms of cosine/logit angles. BSL is realized by augmenting GM-Softmax with two regularizers:
- **Sample-margin regularization ($R_{sm}$):** Drives each sample’s true-class logit above its hardest negative.
- **Zero-centroid regularization ($R_w$):** Enforces uniformity in class centers under imbalance.

The closed-form BSL objective in this context is:
$$
L_{BSL} = \frac{1}{N} \sum_{i=1}^N L_i + \mu R_{sm} + \lambda R_w
$$
with $L_i$ as the per-sample GM-Softmax loss [2206.11589].

BSL recovers or subsumes prior heuristic margin-based losses (SphereFace, CosFace, ArcFace) by the appropriate choice of transformations, providing a more principled and unifying framework for discriminative feature learning and robust prediction [2206.11589].

## 4. Implementation and Practical Guidelines

BSL can be adopted by replacing the averaged positive logits in SL with a log-sum-exp aggregation, requiring only one additional line in standard deep learning frameworks. In PyTorch-style pseudocode:

```python
# SL positive (original)
pos_loss = - pos_scores.mean()

# BSL positive (augmented)
pos_loss = - tau1 * torch.logsumexp(pos_scores/tau1, dim=0)
```
The negative term remains unchanged. Typical hyperparameters are $\tau_1\in[0.05, 0.15]$ and $\tau_2\in[0.1, 0.2]$, with negative sampling budgets of 64–512. This minimal modification suffices for immediate deployment in any SL-based system [2312.12882].

For feature learning and classification, BSL is implemented as GM-Softmax plus weighted regularization with recommended weights $\mu\in\{0.5,1.0\}$ and $\lambda\in\{1,2,5,10,20\}$ [2206.11589].

## 5. Empirical Performance and Fairness Benefits

Empirical evaluations across diverse datasets and model architectures substantiate the effectiveness of BSL:

| Backbone/Dataset      | SL vs. Pointwise Losses | BSL vs. SL      | Robustness to Noise           |
| --------------------- | ----------------------- | --------------- | ----------------------------- |
| MF, LightGCN, NGCF    | SL outperforms BPR, BCE, MSE by >15% | BSL improves SL by 2–5% | BSL tolerates up to 40% noisy positives with 5–15% performance drop |
| Yelp2018, Amazon, Gowalla, MovieLens-1M | Consistent relative wins | Universally positive improvement | Stable under negative noise; maintains performance where pointwise losses degrade |

Group-wise analyses demonstrate a reduction in score variance and improved NDCG for tail (unpopular) items, confirming enhanced fairness in recommendations [2312.12882]. In feature learning benchmarks (MNIST, CIFAR-10/100, Market1501/DukeMTMC, various face verification datasets), BSL raises class margins, improves top-1 accuracy, and maintains large inter-class separations, particularly benefitting imbalanced data [2206.11589].

## 6. Adoption, Extensions, and Related Losses

BSL generalizes and unifies a range of popular softmax and margin-based losses. In the class-imbalanced regime, zero-centroid regularization restores regular simplex optima and large class margins. For balanced data, sample-margin regularization alone suffices to maximize margin and representation quality.

By situating BSL within the DRO and variance penalization framework, and by aligning its implementation with standard softmax practices, the loss function is broadly compatible with a variety of recommendation and representation learning systems. BSL’s close connection to existing methods such as SphereFace, CosFace, and ArcFace suggests its utility as a drop-in replacement with superior theoretical guarantees and practical results on metrics such as Recall@20, NDCG@20, and accuracy [2312.12882, 2206.11589].

Source: https://www.emergentmind.com/topics/bilateral-softmax-loss-bsl