NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

From Softmax to Sparsemax: A Sparse Model of Attention and Multi-Label Classification

André F. T. Martins, Ramón Fernandez Astudillo

TL;DR: The authors introduce sparsemax, a new activation function that serves as a drop-in replacement for softmax but can output exact zeros. This allows models to produce sparse probability distributions, making attention mechanisms more interpretable and improving performance in multi-label classification tasks.

The Problem with Softmax

In modern machine learning, the softmax function is the undisputed king for converting raw scores into probability distributions. However, softmax has a fundamental limitation: its output is always "dense." Because it relies on the exponential function (exp(x)), the output is never truly zero. Every possible outcome is assigned at least a tiny sliver of probability.

In many real-world scenarios, this density is undesirable:

  • Multi-label classification: An image might have three relevant tags out of a thousand possibilities; ideally, the model should assign exactly zero to the other 997.
  • Attention mechanisms: When a model "looks" at a sentence, it should focus on the few words that actually matter, rather than spreading a tiny bit of "attention" over every single punctuation mark and stop word.

The authors sought a transformation that retains the mathematical perks of softmax (like differentiability) while gaining the ability to produce truly sparse outputs.

Comparison of softmax (blue, dashed) and sparsemax (red, solid) in 2D and 3D. Notice how sparsemax hits exactly zero, creating sparse probability distributions.

The Sparsemax Transformation

The core innovation is the sparsemax transformation. Mathematically, while softmax is based on the multinomial logistic model, sparsemax is defined as the Euclidean projection of an input vector onto the probability simplex.

Key technical contributions include:

  • Closed-Form Solution: The authors provide a simple thresholding logic to compute sparsemax. By sorting the input scores and finding a specific threshold $\tau$, any score below that threshold is simply truncated to zero. In the binary case, sparsemax reduces to a "hard sigmoid"—a piecewise linear function that hits exactly 0 and 1.
  • Efficient Backpropagation: They derive the Jacobian of the sparsemax function. Because the output is sparse, the gradient only needs to be computed for the non-zero elements. This sparsity can potentially make backpropagation faster than the softmax equivalent.

A New Loss Function

You cannot simply use standard cross-entropy loss with sparsemax. Taking the log of a zero probability would result in an undefined value. To solve this, the authors derived a new, convex, and differentiable loss function tailored for sparsemax.

Comparison of loss functions. The sparsemax loss (blue) acts as a smooth, differentiable alternative similar to the Huber loss.

Interestingly, they demonstrate that this new loss is a multi-class generalization of the Huber loss, a function famously used in robust statistics to handle outliers gracefully.

Key Results and Applications

The authors evaluated sparsemax across synthetic data, multi-label benchmarks (spanning images, music, audio, and text), and Natural Language Inference (NLI).

  • Multi-label Classification: The sparsemax loss outperformed or matched both standard softmax and tuned independent logistic regressors. It proved particularly effective on datasets with larger label sets, such as REUTERS (text) and BIRDS (audio).
  • Attention in NLI: On the SNLI 1.0 corpus (570,000 sentence pairs), the "SparseAttention" model achieved a test accuracy of 82.2%, slightly outperforming the standard "SoftAttention" baseline (82.08%).
  • Interpretability via Sparsity: Qualitatively, the sparse attention mechanism was much more selective. On the development set, the model only "looked" at 24.6% of the words in the premise, ignoring the rest entirely without losing predictive power. If an attention weight is zero, that word contributed nothing to the final representation, making the model's decision process highly transparent.

Limitations and Future Directions

While powerful, sparsemax comes with computational trade-offs. The authors note that it requires sorting or median-finding algorithms, which are historically less "GPU-friendly" than the simple element-wise exponentials used in softmax. Additionally, to get the most out of sparsity, practitioners often need to tune a "temperature" hyperparameter to scale the input scores before the sparsemax transformation.

Looking forward, sparsemax could be revolutionary for hierarchical attention or models with massive output spaces (like language modeling with huge vocabularies). In these scenarios, "pruning" the search space early by assigning exact zeros could save significant amounts of computation.

Summary updated

BibTeX
@inproceedings{martins2016from,
  author = {André F. T. Martins and Ramón Fernandez Astudillo},
  title = {{From Softmax to Sparsemax: A Sparse Model of Attention and Multi-Label Classification}},
  booktitle = {International Conference on Machine Learning},
  year = {2016},
  url = {https://openreview.net/forum?id=ZWLU8q5s3CR},
}
Figure