Gumbel-softmax

Also known as: Gumbel-softmax trick · concrete distribution

On this page 7

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.

ComponentDefinitionRole
LogitsUnnormalized log-probabilities log π_iLearnable parameters of the distribution
Gumbel noise-log(-log u) with uniform uInjects randomness independent of parameters
Temperature τPositive scalar dividing the perturbed logitsControls how close samples are to one-hot
SoftmaxNormalized exponential over categoriesSmooth substitute for argmax
Relaxed sample yVector on the probability simplexDifferentiable input to later layers
Components of a Gumbel-softmax sample

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 rangeSample shapeGradient behavior
τ above 5Close to uniformLow variance, heavy bias
τ around 1Soft but peakedBalanced, common starting point
τ around 0.5Near one-hotModerate variance, small bias
τ below 0.1Effectively one-hotHigh variance, numerically unstable
Typical temperature regimes and their effects

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.

AspectSoft Gumbel-softmaxStraight-through Gumbel-softmax
Forward outputRelaxed vector on the simplexExact one-hot vector
Backward gradientGradient of the relaxed sampleGradient of the relaxed sample
Train and test consistencyGap between soft training and hard inferenceSame discrete behavior in both phases
Gradient biasDepends on temperatureHigher, even at moderate temperature
Typical applicationDiscrete VAEs with soft decodersRouting, token selection, architecture search
Soft and straight-through variants side by side

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.

EstimatorBiasVarianceTypical use
Score function (REINFORCE)UnbiasedHigh without baselinesReinforcement learning, black-box objectives
Gumbel-softmaxBiased for τ above zeroLow to moderateDiscrete VAEs, attention, architecture search
Straight-through GumbelMore biasedLow to moderateHard selection in the forward pass
REBAR and RELAXUnbiasedReduced with control variatesWhen unbiased gradients matter
Exact marginalizationNoneNoneOnly when categories are few
Gradient estimators for discrete latent variables

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.