# Categorical Reparameterization with Gumbel-Softmax

Gumbel-Softmax lets gradients flow through categorical samples during training

Eric Jang, Shixiang Gu, Ben Poole · arXiv · 2016-11-3

- **Problem** Stochastic networks rarely use categorical latent variables, because backpropagation cannot pass through a sampling step. Score function estimators avoid this but have high variance when there are many categories.
- **Method** Gumbel-Softmax replaces the arg max in the Gumbel-Max trick with a softmax. This gives a differentiable sample that can be annealed toward a true categorical sample.
- **Results** On categorical structured prediction, Gumbel-Softmax scored 59.0 nats, compared to 61.1 for Slope-Annealed ST.

## Why categorical latent variables are hard to train

Stochastic networks with discrete latent variables appear in unsupervised learning, language modeling, attention and reinforcement learning. The trouble is that backpropagation computes parameter gradients efficiently but can't pass through a non-differentiable sampling layer. Existing estimators fall into two families: score function estimators with Monte Carlo variance reduction, and biased path derivative estimators built for Bernoulli variables.

The score function estimator never backpropagates through the sample, but its variance grows with the number of sample dimensions, which hurts categorical variables. No earlier gradient estimator was formulated specifically for categorical variables, and that gap is the one this paper addresses.

## Replacing the sample with a Gumbel-Softmax draw

The key idea is to keep the Gumbel-Max trick, which draws exact categorical samples, and replace its arg max with a softmax, which is differentiable. The result is a continuous vector on the simplex, so gradients can flow back to the class probabilities. The Gumbel-Max trick adds independent Gumbel(0, 1) noise to each log-probability and takes the arg max to get a one-hot sample.

Gumbel(0, 1) noise can be drawn by sampling u from a uniform distribution and computing g = -log(-log(u)).

The softmax sample is computed as follows, for i = 1, ..., k. \\\[ y\_i = \\frac{\\exp((\\log \\pi\_i + g\_i)/\\tau)}{\\sum\_{j=1}^{k} \\exp((\\log \\pi\_j + g\_j)/\\tau)} \\\] The temperature tau divides the perturbed logits before the softmax, so it sets how close the sample sits to a one-hot corner. As tau approaches zero, the samples become one-hot and the distribution matches the categorical distribution, which the authors show in their density derivation.

Small temperatures give samples close to one-hot but high gradient variance, while large temperatures give smooth samples with low variance. The authors therefore start at a high temperature and anneal it to a small but non-zero value during training. For structured output prediction they kept tau fixed at 1, because annealing wasn't needed there.

## Discrete forward pass with a continuous backward pass

For discrete actions or quantized codes, the Straight-Through variant takes the arg max of the relaxed sample in the forward pass. The backward pass still uses the continuous approximation, so the gradient of z is approximated by the gradient of y. This resembles the biased path derivative estimator of Bengio et al., and ST samples can stay sparse even at high temperature.

Chung et al.'s slope-annealed variant sets its slope with a hard sigmoid, whereas ST Gumbel-Softmax uses a softmax for the same role. The plain Straight-Through estimator can suffer when backpropagating through the sample-independent mean makes the forward and backward passes disagree. Because each Gumbel-Softmax sample is a differentiable proxy of the discrete sample, this disagreement doesn't arise in the same way.

## Score function and control variate baselines

The score function estimator uses the identity for the gradient of a log-probability and needs no backpropagation through f or the sample. A control variate can be subtracted from the learning signal, provided its analytical expectation is added back to keep the estimator unbiased. NVIL uses a moving-average baseline and an input-dependent baseline network, while variance normalization divides the signal by max(1, sigma\_f).

DARN uses a first-order Taylor baseline around the mean of the sample, and it fixes z at 1/2 for Bernoulli variables. MuProp uses the same Taylor baseline and replaces the discrete learning signal with a mean-field approximation. VIMCO was left out because the experiments focus on single-sample objectives.

Variance normalization was needed for the score function, DARN and MuProp estimators to be competitive in these experiments.

## Structured output prediction results

The task predicts the lower half of a 28×28 binarized MNIST digit from its top half, using a stochastic binary network with two hidden layers of 200 units each. The network has either 200 Bernoulli variables or 20 categorical variables with 10 classes each. Training used one importance sample per step, and evaluation used 1000 samples.

With Bernoulli latent variables, Gumbel-Softmax scored 58.5 nats, compared to 58.7 for Slope-Annealed ST and 58.9 for MuProp. The margin is small here, so the Bernoulli case shows parity rather than a clear advantage.

With categorical latent variables, Gumbel-Softmax scored 59.0 nats, compared to 61.1 for Slope-Annealed ST and 63.0 for MuProp. ST Gumbel-Softmax scored 59.7 on the same categorical setup, while the plain score function estimator came to 73.1. The authors read the gap as evidence that the single-sample relaxation suits categorical variables.

Learning rates were picked from six values using the MNIST validation set, and the test set was used only for reporting.

## Variational autoencoder results

The authors train VAEs on binarized MNIST with either 200 Bernoulli latent variables or 20 categorical variables with 10 classes each. They use a learned categorical prior instead of a Gumbel-Softmax prior in the training objective. The temperature is set to max(0.5, exp(-rt)) and updated every N steps, with N and r chosen on the validation set.

Evaluation uses a multi-sample bound with 1000 samples.

With categorical latent variables, Gumbel-Softmax gave a negative variational lower bound of 101.5 nats, compared to 107.0 for MuProp and 107.8 for Slope-Annealed ST. On Bernoulli latent variables it gave 105.0, compared to 109.7 for MuProp. Lower bound values are better here, so these gaps favour Gumbel-Softmax on both latent types.

## Semi-supervised classification and training speed

Marginalization sums the unlabeled objective over all k classes, so its cost per step is \\( O(D + k(I+G)) \\). Here D, I and G are the costs of sampling q(y\|x), sampling q(z\|x,y) and evaluating p(x\|y,z). Gumbel-Softmax backpropagates through a single sample from q(y\|x), so its cost is \\( O(D + I + G) \\) per step.

The experiment used 100 labeled examples, spread evenly over 10 classes, and 50,000 unlabeled examples with dynamic binarization. Marginalization reached a bound of -106.8 and 92.6% accuracy, while Gumbel-Softmax reached -109.6 and 92.4%. ST Gumbel-Softmax gave the highest accuracy at 93.6%, with a bound of -110.7. So single-sample inference keeps accuracy close to marginalization, with a slightly lower bound.

Speed was measured in training steps per second on random labels, using a GTX Titan X GPU. The alpha weight was searched over five values for each estimator, and the annealing schedule was updated every 2000 steps.

## Limitations

Temperature trades bias against variance, and the authors report that several annealing schedules worked without giving a rule for choosing one. Gumbel-Softmax samples differ from categorical samples at any non-zero temperature.

With a learned categorical prior, the training objective is no longer a variational bound when samples aren't discrete. The authors report that annealing still tightened the true bound on validation and test sets.

On categorical VAE latent variables, the ST Gumbel-Softmax bound of 107.8 matches Slope-Annealed ST and trails MuProp's 107.0. So the advantage for categorical VAEs belongs to the plain Gumbel-Softmax estimator, not the ST variant.

All three experiment families use binarized MNIST. The semi-supervised speed comparison used randomly generated labels.

## Paper info

- **Authors**: Eric Jang, Shixiang Gu, Ben Poole
- **Venue**: arXiv · Preprint
- **First published**: 2016-11-3
- **Added**: 2026-11-11
- **Updated**: 2017-8-5
- **Version**: v5
- **arXiv**: 1611.01144
- **Category**: stat.ML · cs.LG
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/1611.01144v5) · [PDF ↗](https://arxiv.org/pdf/1611.01144v5)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Eric Jang, Shixiang Gu, Ben Poole (2016). Categorical Reparameterization with Gumbel-Softmax. arXiv:1611.01144v5. https://arxiv.org/abs/1611.01144v5

**BibTeX**

```
@misc{jang2016categorical,
  title = {{Categorical Reparameterization with Gumbel-Softmax}},
  author = {Eric Jang and Shixiang Gu and Ben Poole},
  year = {2016},
  eprint = {1611.01144},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1611.01144v5}
}
```

## Other paper commentary

- [A Survey Of Methods For Explaining Black Box Models](https://refivu.com/en/papers/e8fafeb3-dfaa-4e9a-b461-dc36765f1ed6-a-survey-of-methods-for-explaining-black-box.md)
- [Explanation in Artificial Intelligence: Insights from the Social Sciences](https://refivu.com/en/papers/97a0e87f-2056-41e6-ba4d-00a7f2aa4ab0-explanation-in-artificial-intelligence-insights-from-the.md)
- [MissForest - nonparametric missing value imputation for mixed-type data](https://refivu.com/en/papers/ab5b3748-ade4-4bb9-b016-975b4f82baee-missforest-nonparametric-missing-value-imputation-for-mixed.md)
- [Federated Machine Learning: Concept and Applications](https://refivu.com/en/papers/d845d53f-b001-4e9b-be12-12d49f74b856-federated-machine-learning-concept-and-applications.md)
- [A Comprehensive Survey on Transfer Learning](https://refivu.com/en/papers/c3ef16ed-6b27-4ef6-a0f1-b20616ba3e47-a-comprehensive-survey-on-transfer-learning.md)
- [Deep Learning with Differential Privacy](https://refivu.com/en/papers/a0a5e243-9f30-4599-aa16-17cc0c7b4ba8-deep-learning-with-differential-privacy.md)
- [Bottom-Up and Top-Down Attention for Image Captioning and Visual Question Answering](https://refivu.com/en/papers/6c6f90a1-fe13-482d-9dc4-046620898e40-bottom-up-and-top-down-attention-for-image.md)
- [SwinIR: Image Restoration Using Swin Transformer](https://refivu.com/en/papers/16285f4a-51a3-41dc-9740-c5a9259d2b18-swinir-image-restoration-using-swin-transformer.md)
- [Unsupervised Domain Adaptation by Backpropagation](https://refivu.com/en/papers/5b06ce85-04aa-40bd-a8ba-094e15a092f1-unsupervised-domain-adaptation-by-backpropagation.md)
- [GhostNet: More Features from Cheap Operations](https://refivu.com/en/papers/54dc4d80-177d-40dc-bde2-a797c18077da-ghostnet-more-features-from-cheap-operations.md)
- [Visual Genome: Connecting Language and Vision Using Crowdsourced Dense Image Annotations](https://refivu.com/en/papers/f53fd829-f514-4443-ae13-a5df28e84536-visual-genome-connecting-language-and-vision-using.md)
- [Spatial Transformer Networks](https://refivu.com/en/papers/6eb29eca-41eb-4d69-ace3-032bf11562a8-spatial-transformer-networks.md)

## Terms in this paper

- [Variational Autoencoder](https://refivu.com/en/terms/03e579ac2e7c-variational-autoencoder)
- [reparameterization trick](https://refivu.com/en/terms/67c18035066e-reparameterization-trick)
- [ELBO](https://refivu.com/en/terms/aa20f9febf69-elbo)
- [Gumbel-Max trick](https://refivu.com/en/terms/05472cd65e0a-gumbel-max-trick)
- [NVIL](https://refivu.com/en/terms/f2ff78ccb2d4-nvil)
- [softmax temperature](https://refivu.com/en/terms/709b46363f44-softmax-temperature)
- [score function estimator](https://refivu.com/en/terms/6e1b6ebaa650-score-function-estimator)
- [Structured output prediction](https://refivu.com/en/terms/1d489c67cc24-structured-output-prediction)
- [VIMCO](https://refivu.com/en/terms/cdb92ab7a4ca-vimco)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/c907a642-9316-44dc-97d4-0d4eebe881fa-categorical-reparameterization-with-gumbel-softmax)
