TL;DR
Many models must pick one category from a list, such as a discrete code, a word or an architectural choice, and training needs gradients to flow through that pick. A hard pick cannot be differentiated, because a tiny change in the scores either changes nothing or flips the choice. The Gumbel-softmax trick adds independent Gumbel noise to the scores, then applies a Softmax with a temperature, which gives a smooth vector that behaves like a one-hot sample when the temperature is small.
Builds on: Categorical Distribution, Gumbel Distribution, Softmax, Reparameterization Trick · Leads to: discrete latent-variable models and relaxed sampling in general
Where it's used
Discrete codes in autoencoders. A variational autoencoder whose latent code is a choice among categories needs to sample that choice and still train by gradient descent. The Gumbel-softmax relaxation gives a differentiable sample, so the encoder can be trained end to end. Jang, Gu and Poole introduced the trick in 2016, and their experiments include this setting.
Image tokenizers. The first stage of DALL-E, which turns an image into a grid of discrete tokens, used a discrete autoencoder trained with a Gumbel-softmax relaxation over a codebook of tokens, so that the choice of each token could be learned by gradients.
Choosing among architecture operations. In neural architecture search, a network can choose one operation (a convolution, a pooling layer, or a skip connection) at each position. Each choice is a categorical variable. The relaxation lets the search learn the choice weights with gradients instead of trying every option.
Routing and gating. A model that sends each input to one of several experts, or that picks one action from a discrete set, can use the same relaxation during training. At test time the hard choice is often used, which is why the gap between training and test behaviour is a practical concern.
How it works
Take one tiny instance with three categories and the probabilities from the categorical lesson: . The scores we will work with are , the logarithms of the probabilities, which are to two decimals.
- Start from the scores. Each category has a score . Higher score means more likely. The scores are , and . Check: the largest score belongs to the most likely category, which is category 1 with probability 0.5.
- Add Gumbel noise. Draw one independent Gumbel value per category. Say the draws are . Add them to the scores: , which gives . Check: each noisy score is the old score plus its own noise, so the noise for one category does not touch another.
- Take the hard choice. The largest noisy score is , in category 1. A hard sample picks category 1, which is the output of the Gumbel-max trick and is an exact sample from the categorical distribution. Check: the hard choice is not differentiable, because a tiny change in any score leaves the choice unchanged until the top two scores swap.
- Soften the choice. Replace "take the largest" with a Softmax over the noisy scores at temperature . Exponentiate each score (the exponential): , , . Divide each by the total to get . Check: the entries are positive and add up to 1, so is a probability vector, but it is spread out rather than one-hot.
- Sharpen with a small temperature. Divide each score by before the softmax. The scores become , and the softmax gives about . Check: the largest entry is near 1 and the others are near 0, so looks like the hard choice of step 3.
The flow below shows the same steps. The noise makes the choice random, the softmax makes it smooth, and the temperature sets how close to a hard choice the output is.
flowchart LR A["Scores log p_k"] --> B["Add Gumbel noise g_k"] B --> C["Divide by temperature tau"] C --> D["Softmax"] D --> E["Soft sample y, near one-hot when tau is small"]
The math
Definition
Let be the scores of categories, let be independent standard Gumbel draws, and let be a temperature. The Gumbel-softmax sample is the vector with entries
Each lies between 0 and 1, and the sum to 1, so lies on the probability simplex.
The symbols are the scores, the are the noise, and is the temperature. In practice the scores are the outputs of the model before the softmax, which are called logits, and is replaced by the logit .
Step 1 in symbols: the scores
For the walkthrough, gives
The logarithm is used because the Gumbel-max argument is stated for , and it turns products of probabilities into sums.
Step 2 in symbols: the noise
Each is drawn independently from a standard Gumbel distribution. The draws do not depend on the parameters of the model, which matters for the gradient in step 4. In the walkthrough, , so gives .
Step 3 in symbols: the hard choice and its soft version
The hard choice is
which for the walkthrough is . The soft version replaces the with a softmax over the same scores:
At the entries are , as in step 4 of the walkthrough.
Step 4 in symbols: the gradient
The noise is fixed once it is drawn, so the sample is a deterministic function of the parameters and the noise. Writing makes the reparameterization explicit: the randomness is in , and is a smooth function of . The derivative with respect to exists, so gradient descent can train it, which the reparameterization trick requires.
Step 5 in symbols: the temperature limits
As , the largest entry of approaches 1 and the rest approach 0, so approaches the one-hot vector of the hard choice. As , every entry approaches . For the walkthrough, gives , which is near one-hot, and gives a spread-out vector.
Worked examples
Two categories have scores , and we use .
- Exponentiate the scores: and .
- The total is .
- The soft sample is .
- Check: , so is a valid probability vector.
- Now lower the temperature to . The scores become . Exponentiate: , so .
- Check: the entries still sum to 1, and the top entry rose from 0.731 to 0.881.
- Interpretation: a smaller temperature makes the output closer to a hard choice of the top category.
Take and noise , with .
- The scores are to two decimals.
- Add the noise: .
- The hard choice is the largest score, , which is category 2.
- Exponentiate: and .
- The total is .
- The soft sample is .
- Check: the entries sum to , and the largest entry is in category 2, matching the hard choice.
- Interpretation: the noise shifted category 1 up by 1.0, but the soft sample still gives category 2 more weight, because the original probability of category 2 is so much larger.
An encoder gives a categorical latent code with three values and probabilities . The noise draws are .
- The scores are to two decimals.
- Add the noise: .
- The hard choice is the largest score, , which is code 1.
- Use and divide each score by 0.5: .
- Subtract the largest value, , from each entry to keep the numbers small: . Subtracting the same constant from every entry does not change the softmax.
- Exponentiate: , and .
- The total is .
- The soft sample is , with the entries summing to 1 up to rounding.
- Interpretation: the encoder's gradient flows through these three numbers, and the decoder sees a nearly one-hot code that still changes smoothly when the encoder's parameters change.
Try it: using the same scores and noise from example 3, what does become at ? Answer: dividing by 0.1 gives , and subtracting the largest entry gives . The exponentials are about , and , so , which is essentially the hard choice of code 1.
Common mistakes and tradeoffs
Mistake 1: "The Gumbel-softmax sample is an exact sample from the categorical distribution." It is tempting to think that adding Gumbel noise gives the right answer at every temperature. In fact only the hard of the noisy scores is exact (the Gumbel-max trick). The softmax version is a smooth approximation whose bias shrinks as the temperature falls. At finite the sample is biased, and the size of the bias is the price of a gradient.
Mistake 2: "A higher temperature makes the sample sharper." It is tempting to read as a sharpness knob that works like a multiplier on the probabilities. In fact dividing by a small magnifies the differences between scores, so the softmax concentrates on the top entry. A large shrinks the differences, so the output goes toward uniform.
Mistake 3: "The noise is learned." It is tempting to think the Gumbel draws are part of the model. In fact they are fresh random numbers at each step, not parameters. The model learns only the logits , and the noise is what makes the sample random while keeping the expression differentiable in .
Tradeoffs. A low temperature gives samples close to one-hot and so a model close to the discrete one, but the gradients become large and noisy. A high temperature gives smooth gradients but outputs that do not look like real discrete choices. Many systems anneal the temperature from a high value to a lower one during training. A common variant, the straight-through estimator, uses the hard choice in the forward pass and the soft gradient in the backward pass. The forward pass is then exactly discrete, but the gradient is biased in a different way.
When to use it. Use it when you need to backpropagate through a choice among a small, fixed set of categories and an exact discrete sample is not essential during training.
When not to use it. If the sample only needs to be drawn once, for example at inference, a plain categorical sample is enough. If the categories are continuous, use the reparameterization trick directly. For a large number of categories, a learned embedding or a score-function estimator such as REINFORCE may be more practical.
History
Emil Julius Gumbel studied the largest and smallest values in samples, and his 1958 book Statistics of Extremes made the distribution that bears his name a standard tool in extreme value theory. The Gumbel-max fact, that adding Gumbel noise to log-probabilities and taking the largest gives an exact categorical sample, was used in statistics and later in machine learning for sampling. The version used in deep learning came in 2016. Two groups, Jang, Gu and Poole, and Maddison, Mnih and Teh, posted their papers in November 2016, each introducing a softmax relaxation of a categorical sample with a temperature. The trick caught on because it let discrete latent variables be trained with the same gradient methods as everything else in a neural network.
Recap
- A hard choice among categories cannot be differentiated, so training needs a smooth stand-in.
- Adding independent Gumbel noise to the log-probabilities and taking the largest gives an exact categorical sample.
- Replacing the largest with a softmax over the same noisy scores gives a differentiable sample .
- The temperature controls how close is to one-hot: small is sharp and biased, large is smooth and spread out.
- The noise is fixed and independent of the parameters, so the gradient with respect to the logits exists.
Read next: Softmax Temperature, which explains the temperature on its own, and the papers by Jang, Gu and Poole and by Maddison, Mnih and Teh, which introduced the trick in 2016.
Check yourself
QuestionWhy does a hard categorical choice block gradient-based training, and what does the Gumbel-softmax sample replace it with?Show answer
A hard choice is the output of an , which is piecewise constant: a small change in the scores leaves it unchanged, and at the point where the top two scores swap it jumps. Its derivative is zero almost everywhere, so there is no gradient to follow. The Gumbel-softmax sample replaces it with a softmax over the noisy scores, which is smooth in the scores and so has a derivative.
QuestionTwo categories have scores and temperature . What is the soft sample , and which category does the hard choice pick?Show answer
The exponentials are and . The total is . So . The hard choice is the largest score, which is 2, so category 2 is picked. The soft sample puts most of its weight on category 2, as it should.
QuestionA colleague says that because the temperature is 0.01, the Gumbel-softmax sample is exactly the categorical sample. Explain what is right and what is wrong about this claim.Show answer
What is right is that a small temperature makes the sample close to one-hot, so it approximates the hard choice. What is wrong is the word exactly: at any finite temperature the softmax is a smooth approximation, and the sample is biased relative to the true categorical sample. Only the hard of the noisy scores is exact, and the approximation improves only as the temperature goes toward zero.
QuestionIn example 1, the scores are . What happens to the soft sample if the temperature rises to ?Show answer
Dividing the scores by 100 gives . The exponentials are and , with total . So . The output is close to uniform, which is what a very large temperature does.