BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / Paper implementations17 of 28 in topic

844. Gumbel-Softmax sample from the paper

EasyPaper implementations

Why it matters Discrete choices show up in VAEs with categorical latents, mixture routing and neural architecture search. Gumbel-Softmax is the standard way to make them trainable with backprop.

Paper: "Categorical Reparameterization with Gumbel-Softmax", Jang, Gu and Poole, 2016 (ICLR 2017), arXiv 1611.01144.

Sampling a category is not differentiable. The paper replaces the hard argmax with a softmax over noisy logits: y_i = exp((log pi_i + g_i) / tau) / sum_j exp((log pi_j + g_j) / tau), where g_i is Gumbel(0, 1) noise and tau is a temperature. Low tau gives nearly one-hot samples.

Write gumbel_softmax(logits, gumbel_noise, tau, hard=False). The noise is passed in so the answer is deterministic.

Inputs:
- logits: unnormalised log probabilities, a list or ndarray of shape (k,) or (batch, k). Use np.asarray(logits, dtype=float).
- gumbel_noise: same shape as logits.
- tau: a float greater than 0.
- hard: if True, return the one-hot vector of the argmax of the soft sample along the last axis (ties go to the lowest index, as np.argmax does).

Return an ndarray of floats with the same shape as logits. Each row sums to 1. Subtract the row maximum before exp so large logits do not overflow. No rounding.

Functiondef gumbel_softmax(logits, gumbel_noise, tau, hard=False)

Example 1

Input: gumbel_softmax([0.0, 1.0, 2.0], [0.5, 0.0, -1.0], 1.0)

Output: [0.2327, 0.3837, 0.3837]

Example 2

Input: logits = [0.0, 1.0, 2.0], gumbel_noise = [0.5, 0.0, -1.0], tau = 0.1

Output: [0.003358, 0.498321, 0.498321]

Example 3

Input: logits = [[1.0, 1.0], [3.0, 0.0]], gumbel_noise = [[0.2, 0.0], [0.0, 0.0]], tau = 0.5

Output: [[0.598688, 0.401312], [0.997527, 0.002473]]

Constraints

  • Python 3 with NumPy. Code runs in your browser.
  • Your code must pass all 6 tests to be accepted.
Hint 1

Add the noise to the logits, then divide by tau.

Hint 2

Use a stable softmax along axis -1: subtract the max first.

Hint 3

For hard, take argmax along the last axis and build a one-hot array of the same shape.

Common mistakes
  • Dividing only the noise by tau instead of the sum.
  • Softmax over the wrong axis for batched input.
  • Skipping the max shift, which overflows for logits near 1000.
Understand the idea

The Gumbel-Max trick says argmax(log pi + g) with g = -log(-log(u)) is an exact sample from the categorical distribution pi. The paper swaps argmax for a softmax with temperature tau so gradients flow. With logits [0, 1, 2], noise [0.5, 0, -1] and tau = 1, the noisy logits are [0.5, 1, 1] and the softmax is about [0.2327, 0.3837, 0.3837].

The straight-through hard version returns a one-hot vector in the forward pass. In a framework you would add y_hard - stop_gradient(y_soft) + y_soft; here you only return the forward value.

Shapes and data flow
logits and noise: (k,) or (batch, k). Output: same shape, rows sum to 1.
Topics
Paper implementations
Similar questions
  • word2vec skip-gram negative sampling loss (Mikolov et al., 2013)Easy
  • Plan a training run: compute-optimal model size and tokens (Chinchilla rule)Easy
  • Evaluate the Chinchilla scaling lawEasy
  • Build a global magnitude pruning maskEasy
All Paper implementations problems
Report
844 / 903
Python 3solution.pyPython idle
1
2
3
4
5
6
import numpy as np
​
def gumbel_softmax(logits, gumbel_noise, tau, hard=False):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks all 6 tests.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS