Search

Search papers and lessons

Entmax

Level 4 · 10 min read

Entmax is a family of score-to-probability maps indexed by a number alpha, which runs from softmax at one end to sparsemax at the other, so the amount of sparsity can be tuned.

On this page

  • Colour = topic mix
  • Linear algebra
  • Discrete math
  • Calculus
  • Neural network basics
  • Probability
  • ReadReady to readNot yet

TL;DR

Entmax is a family of functions that turn a vector of scores into probabilities, with one setting parameter alpha that controls how many entries get exactly zero. As alpha approaches 1 it becomes Softmax, which never gives zeros. At alpha equal to 2 it is Sparsemax, which gives many zeros. Values in between give a middle amount of sparsity. Because the parameter is continuous, a model can tune how selective its attention or its outputs are.

Builds on: Sparsemax, Softmax · Leads to: Sparse Attention, Attention Mechanism

Where it's used

Attention in translation and language models. Researchers used entmax in attention layers of sequence-to-sequence models for machine translation. A head that uses a middle setting of alpha puts weight on a few source words without the all-or-nothing choice of sparsemax. The 2019 work by Ben Peters, Vlad Niculae and André Martins developed this use.

Adaptively sparse transformers. A follow-up in 2019 by Gonçalo Correia, Vlad Niculae and André Martins let each attention head learn its own alpha. Some heads became sparse and focused, and others stayed dense, which made the amount of sparsity part of training.

Sparse outputs over a vocabulary. A model that predicts one word from a large list of options normally gives every word a small positive probability. An entmax output can give most of the list exactly zero, which removes those options from the loss and from the decision. This makes the output layer cheaper to read and to decode.

How it works

We will trace one small case with three scores and alpha = 1.5. The scores are z = (1.0, 0.2, −1.0).

  1. Scale the scores. Multiply by alpha − 1 = 0.5, a scaling of the same kind as temperature in softmax. This gives a = (0.5, 0.1, −0.5). Check: each entry is half of the original score.
  2. Choose the exponent. Entmax with alpha = 1.5 raises the positive parts to the power 1 / (alpha − 1) = 2. So each output is a square of a number that is positive when the entry is kept, and zero otherwise.
  3. Find the threshold. Look for a number tau such that the kept entries, each shifted by tau and squared, add to 1. Start by guessing that the top two entries are kept. Then (0.5 − tau)² + (0.1 − tau)² = 1. Expanding gives 2 tau² − 1.2 tau − 0.74 = 0, and the root that keeps both entries is tau = (1.2 − √7.36) / 4 ≈ −0.37823.
  4. Check the support. The top two entries are above tau: 0.5 > −0.378 and 0.1 > −0.378. The third entry, −0.5, is below tau, so it is dropped. Check: if the third entry had been above tau, the guess would have been wrong and we would include it.
  5. Compute the outputs. Each kept entry gives (a_i − tau)². So (0.5 + 0.37823)² ≈ 0.77129 and (0.1 + 0.37823)² ≈ 0.22871. The third entry is 0. Check: 0.77129 + 0.22871 + 0 = 1.
flowchart LR
  Z[Scores z] --> A[Scale by alpha minus 1]
  A --> T[Solve for threshold tau so the kept parts sum to 1]
  T --> K[Keep entries above tau]
  K --> P[Square the shifted kept entries]
  P --> O[Probabilities with exact zeros]

The math

Definition: Alpha-entmax

For alpha greater than 1 and a score vector z, the alpha-entmax map gives pi=[(α−1) zi−τ(z)]+1/(α−1)p_i = \left[ (\alpha - 1)\, z_i - \tau(z) \right]_+^{1/(\alpha - 1)} where [x]+[x]_+ means the larger of xx and 0, and the threshold τ(z)\tau(z) is chosen so that the entries of p add to 1. The output lies in the probability simplex, as in Sparsemax.

Step 1 in symbols: scaling

The factor alpha − 1 scales the scores before the threshold is applied. For alpha = 1.5 it is 0.5, so a = 0.5 z.

Step 2 in symbols: the exponent

The exponent 1 / (alpha − 1) controls the shape of the output. For alpha = 1.5 it is 2, which gives squares. For alpha = 2 it is 1, which gives the sparsemax map. As alpha moves toward 1, the exponent grows without bound, and the map approaches Softmax. This is the standard limit.

Step 3 in symbols: the threshold

For alpha = 1.5 the condition on tau is ∑i∈S(ai−τ)2=1\sum_{i \in S} (a_i - \tau)^2 = 1 where a = 0.5 z and S is the set of kept entries. If S has two entries, this is a quadratic in tau. The example above solves it for S = {1, 2}. With three entries it becomes 3 tau² − 2 (sum of a) tau + (sum of a²) − 1 = 0. In general there is no closed form for every alpha, so a root search for tau is the usual method.

Step 4 in symbols: the support

An entry is kept when a_i − tau > 0. The entries that are kept form the support. A sort of the scores, followed by checks of the support size, finds it exactly for alpha = 1.5 and for alpha = 2.

Step 5 in symbols: the output

pi=(ai−τ)2for kept entries,pi=0otherwise.p_i = (a_i - \tau)^2 \quad \text{for kept entries}, \qquad p_i = 0 \quad \text{otherwise.} The output sums to 1 by the choice of tau. For the example, p = (0.77129, 0.22871, 0).

Worked examples

Example
1. Tiny: three equal scores

Take z = (1, 1, 1) and alpha = 1.5.

  1. Scale: a = (0.5, 0.5, 0.5).
  2. All three entries are equal, so all are kept. The condition is 3 (0.5 − tau)² = 1.
  3. Solve: (0.5 − tau)² = 1/3, so 0.5 − tau = 0.57735 and tau = −0.07735.
  4. Output: each entry is (0.5 + 0.07735)² = 0.57735² = 1/3.
  5. Check: 1/3 + 1/3 + 1/3 = 1.

Interpretation: equal scores give equal probabilities, as softmax would, so equal inputs get equal weight.

Example
2. Standard: entmax between softmax and sparsemax

Take z = (1.0, 0.2, −1.0). Compare three maps on the same scores.

  1. Softmax: the exponentials are e¹ ≈ 2.71828, e^0.2 ≈ 1.22140 and e^−1 ≈ 0.36788, which sum to 4.30756. The output is (0.63105, 0.28355, 0.08540). Every entry is positive.
  2. Sparsemax (alpha = 2): sorted, the support is the top two entries, with threshold tau = (1.0 + 0.2 − 1) / 2 = 0.1. The output is (0.90, 0.10, 0).
  3. Entmax with alpha = 1.5, from the walkthrough: (0.77129, 0.22871, 0).
  4. Compare: the third entry is zero for both sparse maps, and softmax keeps it at 0.085. The top entry is 0.63 under softmax, 0.77 under alpha = 1.5, and 0.90 under sparsemax.

Interpretation: alpha = 1.5 sits between the two ends. It keeps the same zero as sparsemax but spreads more weight onto the second entry, which is the tuning the family offers.

Example
3. Realistic: attention over four positions

An attention head scores four input positions with z = (2.0, 1.8, 0.2, −0.5), and uses alpha = 1.5.

  1. Scale: a = (1.0, 0.9, 0.1, −0.25).
  2. Guess that the top two entries are kept. Then (1.0 − tau)² + (0.9 − tau)² = 1. Expanding gives 2 tau² − 3.8 tau + 0.81 = 0, and the root that keeps both entries is tau = (3.8 − √7.96) / 4 ≈ 0.24466.
  3. Check the support: 0.1 is below tau, so the third entry is dropped, and −0.25 is dropped too. The guess holds.
  4. Output: (1.0 − 0.24466)² ≈ 0.57053 and (0.9 − 0.24466)² ≈ 0.42947. The other two are 0.

Interpretation: the head reads two of the four positions and gives them weights 0.57 and 0.43. Softmax would spread weight over all four, with the two small scores getting several percent each.

Try it: take z = (0.9, 0.3, 0.2) with alpha = 1.5. Find the scaled vector, solve for tau with all three entries kept, and compute the outputs. Answer: a = (0.45, 0.15, 0.10), tau ≈ −0.32294, and the outputs are about (0.5974, 0.2237, 0.1789). All three entries are nonzero.

Common mistakes and tradeoffs

It is tempting to think alpha is a temperature that just sharpens or flattens the output. In fact alpha changes the shape of the map, not just its scale. Its effect on sparsity is the main one: alpha near 1 gives dense outputs like softmax, and alpha near 2 gives sparse outputs like sparsemax.

It is tempting to think entmax is always sparser than sparsemax. In fact any alpha below 2 tends to keep more entries positive than sparsemax does, so entmax with alpha = 1.5 is usually less sparse than sparsemax on the same scores. The sparse end of the family is alpha = 2.

It is tempting to think the zeros come for free. In fact the threshold must be found for every vector, which costs more than softmax. For alpha = 1.5 and alpha = 2 there are exact sort-based methods, and for other values a root search is used, so the extra cost depends on the method.

When to use entmax: when some outputs should be exactly zero but the amount of sparsity should be tuned, or when different heads or layers benefit from different sparsity. When not to use it: when every option should keep some weight, which is the job of softmax, or when the extra compute matters more than the zeros. Sparsemax is the simpler choice if a fixed sparse map is enough.

History

Softmax was the standard way to turn scores into probabilities for classification and attention. Its outputs are never zero, which makes it hard to see which inputs actually drove a decision. In 2016, André Martins and Ramón Fernandez Astudillo proposed sparsemax as a drop-in map with exact zeros, as described in Sparsemax.

In 2019, Ben Peters, Vlad Niculae and André Martins introduced the alpha-entmax family, which generalizes both maps and gives a continuous knob for sparsity. They used it in sequence-to-sequence models for machine translation. The same group, with Gonçalo Correia, then showed that letting attention heads learn their own alpha gives a mix of dense and sparse heads.

Recap

  • Entmax is a family of maps indexed by alpha. In the limit alpha goes to 1 it gives Softmax, and alpha = 2 gives Sparsemax.
  • The map raises the shifted, scaled scores to the power 1 / (alpha − 1) and keeps only the positive parts.
  • The threshold tau is chosen so that the output sums to 1, which is a root-finding problem in general.
  • A middle alpha gives a middle amount of sparsity, so the choice can be tuned or learned.
  • Read next: Sparse Attention for sparsity in attention patterns, and Attention Mechanism for where the weights are used.

Check yourself

QuestionWhat does entmax approach as alpha goes to 1, and what is it at alpha = 2?Show answer

As alpha approaches 1 it approaches Softmax, which gives every entry a positive value. At alpha = 2 it is Sparsemax, which can give exact zeros.

QuestionFor alpha = 1.5, what is the exponent applied to the kept entries, and why does this make the output a square?Show answer

The exponent is 1 / (alpha − 1) = 1 / 0.5 = 2. Each kept entry is raised to that power, so the output is the square of the shifted, scaled score.

QuestionCompute the output of entmax with alpha = 1.5 for z = (1, 1), using the rule that the outputs are equal by symmetry.Show answer

Scaled scores are (0.5, 0.5). By symmetry each output is 0.5. Check: the threshold satisfies 2 (0.5 − tau)² = 1, so 0.5 − tau = 0.70711 and tau = −0.20711. Each output is (0.70711)² = 0.5, and the two sum to 1.

QuestionA model's attention head is too spread out, with many small weights that add noise. Would you move alpha up or down, and what does that do to the zeros?Show answer

Move alpha up toward 2. Higher alpha gives more zeros, so small weights are removed. The risk is that a head with too much sparsity can drop positions that were useful, so the value should be checked on held-out data.

Sources

Entmax — explained simply · ungoon.me