TL;DR
Sparsemax is a function that takes a vector of real scores and returns a vector of probabilities, the same kind of output that Softmax gives. The difference is that sparsemax can return exact zeros, so a few entries get all the probability and the rest get none. It does this by finding the point closest to the scores among all probability vectors, which is a projection onto the probability simplex.
Builds on: Softmax, Orthogonal Projection · Leads to: TabNet: Attentive Interpretable Tabular Learning
Where it's used
Attention with a clear choice. In attention layers of a Transformer, a model scores each input position and turns the scores into weights. With softmax every position gets some weight, however small. With sparsemax the weights can be exactly zero, so the model reads only a few positions and the choice is easy to inspect.
Multi-label classification. Softmax assumes one correct class. A logistic output per label allows several labels, but gives no shared distribution. When several labels can be true at once, sparsemax lets the model assign probability only to the labels that matter. The paper that introduced sparsemax covers this use alongside attention.
Feature selection in tabular models. The TabNet architecture (see the TabNet paper) learns a sparse mask over the input features at each decision step, so each step uses only a few features. The mask is built from the same family of ideas: normalise scores into a probability-like vector and let most entries drop to zero.
How it works
Take a three-entry score vector . Here is what sparsemax does with it.
- Sort the scores from largest to smallest. The sorted vector is . Call the sorted entries . Check: the largest score is first, and the order is the only thing sorting changes.
- Find how many entries stay positive. For each , test whether is greater than the sum of
the first sorted scores. Keep the largest that passes.
- : against . Passes.
- : against . Passes.
- : against . Fails. So . Check: the test fails once a score is too small to keep, so the count stops growing.
- Compute a threshold. Set . This is the amount that each kept score is lowered by, chosen so the result sums to one. Check: is the average excess of the kept scores over 1.
- Subtract the threshold and clip at zero. Each output is . That gives . Check: the entries are non-negative and add to .
- Compare with softmax. Softmax on the same scores gives about . Every entry is positive. Sparsemax gives the third entry exactly zero. Check: both outputs sum to one, and only sparsemax has a true zero.
flowchart LR Z["Scores z"] --> S[Sort descending] S --> K["Find largest k that passes the test"] K --> T["Threshold tau from the first k scores"] T --> C["Output max of z minus tau, and 0"] C --> P["Probabilities with exact zeros"]
The math
For a score vector , sparsemax is the point of the probability simplex closest to in Euclidean distance: where is the set of probability vectors with entries. The solution has the closed form given below.
Step 1 in symbols: sorting
Write the sorted scores as . Sorting does not change the answer; it only makes the test below easy to run.
Step 2 in symbols: the support size
Let The set of the first sorted entries is the support, the entries that stay positive. For the example the test passes for and and fails for , so .
Step 3 in symbols: the threshold
For the example, .
Step 4 in symbols: the output
The output is zero exactly when . For the example this gives .
Comparison with softmax
Softmax is , built from the Exponential Function, which is strictly positive for every entry. The two functions share the same output space, the probability simplex . They differ in the map: softmax is smooth and never zero, while sparsemax is piecewise linear and can be exactly zero. The clip at zero in step 4 is the same operation as a ReLU. The Gradient of softmax is dense; the Gradient of sparsemax is zero for entries outside the support. That is the price of sparsity: no signal flows through the entries that were set to zero.
Worked examples
Take .
- Sorted: , .
- For : . Passes.
- For : against . The test needs strictly greater, so it fails.
- So , and .
- Output: and , so .
Interpretation: when one score is larger than the other by 1, sparsemax puts all probability on it. Softmax would put on the first entry, using the Exponential Function of each score.
Take , as in "How it works".
- The support has entries, found in the second step of the walkthrough.
- Threshold: .
- Subtract and clip: .
- Sum check: .
Interpretation: the third score is far below the others, so it falls below the threshold and gets no probability at all. Softmax would still give it .
A model scores four input positions with .
- Sorted: .
- For : . Passes.
- For : . Passes.
- For : ? No. Fails.
- So , and .
- Output: .
Interpretation: the model reads exactly two of the four positions, and the weights 0.6 and 0.4 say how much each one counts. Softmax would spread weight over all four and make the choice harder to read.
Try it: for , which entries of the output are nonzero, and what are they? Answer: all three are nonzero. Here because , so , and the output is .
Common mistakes and tradeoffs
It is tempting to think sparsemax is always sparser in practice, so it can replace softmax anywhere. In fact the number of zeros depends on the spread of the scores. When the scores are close together, the support is large and sparsemax behaves much like softmax. The zeros appear only when a few scores clearly stand out.
It is tempting to think a zero in the output means the entry was irrelevant to training. In fact the gradient is zero for entries outside the support, so those entries receive no learning signal in that step, while the entries inside it receive a dense one. Check that the scores can still move into the support under gradient descent, or the model may stop using some positions.
It is tempting to think the exact zeros make the model easier to explain. They help, because the reader can see which positions were used. But the zeros depend on the score threshold, and a small change in scores can move an entry across it.
When to use sparsemax: when a few options should carry all the weight, when several labels can be true together, or when a sparse selection is part of the design. When not to use it: when every option should keep some weight, or when the training signal must flow to all entries. The usual alternative is softmax, which is smooth and dense. Sparse selection is also familiar from lasso regression, which sets some weights exactly to zero. A middle path is the alpha-entmax family, which interpolates between softmax and sparsemax.
History
Softmax was the standard way to turn scores into probabilities for classification and attention. Its outputs are never zero, which made it hard to tell which inputs really mattered. In 2016 André Martins and Ramón Fernandez Astudillo proposed sparsemax as a drop-in map with the same output space and exact zeros, and showed it on attention and on multi-label classification. The idea caught on because the sparse weights are easy to read and cheap to compute, since the threshold comes from a sort and a single pass.
Recap
- Softmax turns scores into probabilities that are always positive. Sparsemax turns them into probabilities that can be exactly zero.
- Sparsemax is the Euclidean projection of the scores onto the probability simplex, so its output is the closest valid probability vector.
- Its computation is a sort, a search for the support size , and a threshold that is subtracted from the scores.
- The zeros make attention and multi-label outputs easier to read, and they remove gradient from the entries outside the support.
- Read next: Attention Mechanism for where the weights are used, and TabNet for a sparse feature mask.
Check yourself
QuestionIn one sentence, what extra property does sparsemax have that softmax does not?Show answer
Sparsemax can output exact zeros, so some entries get no probability at all, while softmax gives every entry a positive value.
QuestionFor the scores , what is the output of sparsemax?Show answer
Sorted: . For : , passes. For : ? No, fails. So and . Output: , , . So the output is .
QuestionWhy might a model with sparsemax attention be easier to inspect than one with softmax attention, and what is a risk of relying on the zeros?Show answer
With sparsemax, the entries that get no weight are visibly absent, so the reader sees which positions were used. The risk is that the zeros depend on a threshold, so small changes in the scores can move an entry across it and change which positions are used, and entries outside the support get no gradient in that step.
QuestionA classifier must label each image with any subset of ten tags, and the tags are not exclusive. Why might sparsemax suit this better than softmax?Show answer
Softmax forces one probability distribution that assumes one correct class, so it spreads mass across tags even when only two apply. Sparsemax can give probability exactly zero to the tags that do not apply, so the output reflects a set of active tags more directly. This is the multi-label use described in the paper that introduced it.