Gumbel-softmax is a continuous, differentiable relaxation of sampling from a categorical distribution, obtained by adding Gumbel noise to log-probabilities and passing the result through a temperature-controlled softmax. In probabilistic models trained with gradient descent, it allows a network to make approximately discrete choices while still backpropagating gradients through them, which ordinary categorical sampling does not permit.
The technique was introduced independently in 2016 by two research groups, one calling it the Gumbel-softmax trick and the other the concrete distribution (short for continuous relaxation of discrete random variables). In artificial intelligence it is used in discrete variational autoencoders, differentiable architecture search, hard attention, token selection and reinforcement learning with discrete actions.
How the Gumbel-softmax relaxation works
The method rests on the Gumbel-max trick: if each log-probability log π_i is perturbed by independent noise g_i = -log(-log u_i) with u_i drawn uniformly from (0, 1), then the index of the largest perturbed value is an exact sample from the categorical distribution with probabilities π. The problem is that the argmax operation has zero gradient almost everywhere.
Gumbel-softmax replaces the argmax with a softmax: y_i = exp((log π_i + g_i) / τ) / Σ_j exp((log π_j + g_j) / τ). The output y is a point on the probability simplex rather than a one-hot vector, and it is a smooth function of the logits, so gradients can pass through it. Because the randomness enters only through the noise g, this is a form of the reparameterization trick applied to discrete variables.
The following table summarizes the components of the computation and their role.
| Component | Definition | Role |
|---|---|---|
| Logits | Unnormalized log-probabilities log π_i | Learnable parameters of the distribution |
| Gumbel noise | -log(-log u) with uniform u | Injects randomness independent of parameters |
| Temperature τ | Positive scalar dividing the perturbed logits | Controls how close samples are to one-hot |
| Softmax | Normalized exponential over categories | Smooth substitute for argmax |
| Relaxed sample y | Vector on the probability simplex | Differentiable input to later layers |
The role of temperature
The temperature τ governs the central trade-off of the method: low temperatures give nearly discrete samples but high-variance gradients, while high temperatures give smooth samples with low-variance but strongly biased gradients. As τ approaches zero, the distribution of y converges to the true categorical distribution; as τ grows, samples approach the uniform vector.
| Temperature range | Sample shape | Gradient behavior |
|---|---|---|
| τ above 5 | Close to uniform | Low variance, heavy bias |
| τ around 1 | Soft but peaked | Balanced, common starting point |
| τ around 0.5 | Near one-hot | Moderate variance, small bias |
| τ below 0.1 | Effectively one-hot | High variance, numerically unstable |
A widely used practice is temperature annealing: training starts near τ = 1 and decays exponentially toward a floor of roughly 0.3 to 0.5. Some implementations learn τ as a parameter, although this can collapse toward extreme values without regularization.
Straight-through Gumbel-softmax
The straight-through variant produces a hard one-hot vector in the forward pass and uses the soft Gumbel-softmax gradient in the backward pass. It is implemented by taking the argmax of y, building a one-hot vector, and adding the difference between that vector and y with gradient stopping, so the forward value is discrete while the backward pass sees y.
This variant is preferred when downstream computation requires genuinely discrete inputs, such as selecting one expert, one token or one architectural operation. The price is an additional gradient bias, because the backward pass differentiates a function that the forward pass never actually computed.
| Aspect | Soft Gumbel-softmax | Straight-through Gumbel-softmax |
|---|---|---|
| Forward output | Relaxed vector on the simplex | Exact one-hot vector |
| Backward gradient | Gradient of the relaxed sample | Gradient of the relaxed sample |
| Train and test consistency | Gap between soft training and hard inference | Same discrete behavior in both phases |
| Gradient bias | Depends on temperature | Higher, even at moderate temperature |
| Typical application | Discrete VAEs with soft decoders | Routing, token selection, architecture search |
In practice, teams often try the soft variant first for its cleaner gradients and switch to the straight-through form only when the model must behave identically during training and deployment. Comparing both on a validation set with hard samples is the most reliable way to choose.
Comparison with other discrete gradient estimators
Gumbel-softmax is one of several ways to estimate gradients through discrete random variables, and the choice depends on whether bias or variance is the greater concern. The table compares it with the main alternatives.
| Estimator | Bias | Variance | Typical use |
|---|---|---|---|
| Score function (REINFORCE) | Unbiased | High without baselines | Reinforcement learning, black-box objectives |
| Gumbel-softmax | Biased for τ above zero | Low to moderate | Discrete VAEs, attention, architecture search |
| Straight-through Gumbel | More biased | Low to moderate | Hard selection in the forward pass |
| REBAR and RELAX | Unbiased | Reduced with control variates | When unbiased gradients matter |
| Exact marginalization | None | None | Only when categories are few |
When the number of categories is small, summing over all outcomes exactly is often simpler and more accurate than any estimator. Gumbel-softmax becomes attractive when categories are numerous or when many discrete variables interact, which makes enumeration intractable.
Practical use and common pitfalls
In a discrete variational autoencoder, the encoder outputs logits, Gumbel-softmax produces relaxed latent codes, and the evidence lower bound uses a KL divergence to a uniform categorical prior. A subtle point is that the KL term is usually computed against the unrelaxed categorical distribution, which is itself an approximation that practitioners should keep in mind.
- Numerical instability: compute Gumbel noise with clamped uniforms, because
log(0)produces infinities. - Too low a temperature: gradients explode or vanish; anneal gradually rather than starting near zero.
- Mismatch at evaluation: relaxed training followed by hard inference can shift behavior; validate with hard samples.
- Logits versus probabilities: add noise to log-probabilities or logits, never to raw probabilities.
- Overconfident logits: large logit magnitudes make the noise irrelevant and reduce exploration.
Most deep learning frameworks provide a built-in function with a hard flag for the straight-through variant, which removes the need to implement the noise and gradient tricks manually and reduces the risk of subtle errors.
A plain-language summary
Neural networks learn by making small adjustments. That works for smooth quantities. It fails for choices like “pick option A, B or C”, because a hard choice gives no hint about which way to adjust.
Gumbel-softmax solves this with a soft version of the choice. The network adds a little randomness and then picks mostly one option, while keeping a small share for the others. That small share tells the network how to improve. A dial called temperature sets how firm the choice is. Turned down, the choice becomes almost a clean pick.
An example from practice
A research team compresses speech recordings into a sequence of discrete codes, with each time step choosing one of 512 codebook entries. Enumerating every combination is impossible, and the score function estimator makes training unstable, so the team uses straight-through Gumbel-softmax in the encoder.
Training starts at τ = 1.0 and anneals to 0.4 over the first 100,000 steps. Early runs at a fixed τ of 0.1 diverged within hours, while the annealed schedule converged smoothly. The final model uses hard argmax codes at inference, and validation with hard samples shows only a small drop in reconstruction quality compared with the relaxed training objective, confirming that the relaxation gap stayed acceptable.