Skip to content

← Generative Models step 3 of 11

Easy Primitives

VAE Reparameterization Trick

A VAE encoder predicts a distribution over latent vectors rather than a single vector. For a diagonal Gaussian, it supplies a mean mu and a log variance log_var for each coordinate.

To draw a sample, first draw independent standard normal noise eps, then shift and scale it:

z = mu + exp(0.5 * log_var) * eps

The factor exp(0.5 * log_var) is the standard deviation. exp(log_var) is the variance, so using it as the scale would give the wrong distribution. For a coordinate with mean 3, variance 4, and noise draw -0.5, the result is 3 + 2 * (-0.5) = 2.

This is the reparameterization trick. For a fixed noise draw, the sample depends differentiably on the encoder's outputs. Its derivative with respect to the mean is 1, and with respect to log variance is 0.5 * exp(0.5 * log_var) * eps. The noise is independent of the encoder parameters and is held fixed during that forward/backward computation.

Your task

Implement reparameterize(mu, log_var, seed). Both tensors have shape (N, d), with positive dimensions, and contain float32 values. Return one sample tensor of the same shape using the formula above.

The seeded noise draw is part of the task, not just its distribution:

In PyTorch

Seed a fresh CPU torch.Generator with int(seed), then call torch.randn(mu.shape, generator=gen) once. The exercise uses CPU inputs and float32 noise.

In JAX

Create key = jax.random.PRNGKey(int(seed)), then call jax.random.normal(key, mu.shape) once. Use Threefry with the runtime-pinned setting jax_threefry_partitionable = True.

Do not split the JAX key before this draw or consume extra random samples. The frameworks produce different samples from the same seed, so each mode has its own expected values. Results are compared numerically; you do not need to reproduce the other framework's random sequence.

For zero means and zero log variances, your output is exactly the noise draw. For very negative log variances, the scale becomes tiny and the result approaches the mean. No KL or reconstruction loss is needed here.

Reference: Auto-Encoding Variational Bayes.