Skip to main content
Open In Colab A generative model turns samples from a simple distribution into samples from a complex one. This section builds the smallest example that shows the idea: a one-dimensional Gaussian latent variable is pushed through a fixed function and ends up as a half-moon in the plane. The function plays the role that a neural network plays in a deep latent variable model. The construction has three steps. The latent variable is Z∼N(0,1).Z \sim \mathcal{N}(0, 1). The map Φ\Phi below is the standard normal CDF. It turns the latent variable into an angle, ϑ=π Φ(Z)∈(0,π),\vartheta = \pi\,\Phi(Z) \in (0, \pi), and the generator places that angle on the unit circle, G(Z)=(cos⁡ϑ, sin⁡ϑ).G(Z) = \big(\cos\vartheta,\ \sin\vartheta\big). Finally, isotropic Gaussian noise is added in the plane, X=G(Z)+σ ε,ε∼N(0,I2).X = G(Z) + \sigma\,\varepsilon, \qquad \varepsilon \sim \mathcal{N}(0, I_2). Because Φ(Z)\Phi(Z) is uniform on (0,1)(0, 1) when ZZ is standard normal, the angle ϑ\vartheta is uniform on (0,π)(0, \pi). The latent Gaussian is therefore transformed into an even spread of points along the upper half of the circle.

The latent variable and the angle

The code draws the latent samples and applies the two maps. The generator, the angle map and the noise scale σ\sigma are the only ingredients.
Output from cell 2 The left panel shows the latent samples with the standard normal density. The right panel shows the angles. The gray line at height 1/π1/\pi is the density of a uniform distribution on [0,π][0, \pi].

The curve without noise

Without the noise term, every sample is G(Z)G(Z) and lies exactly on the unit half-circle. Output from cell 3 The color shows the latent value that produced each point. Small zz lands near the right end of the arc and large zz near the left end. The map is smooth and one-to-one from the real line onto the open half-circle. Here is a fact that later sections rely on. The generated distribution lives on a one-dimensional curve inside a two-dimensional space. A curve has zero area, so the probability that a sample falls in any region of positive area that avoids the curve is zero, and the distribution has no density with respect to area in the plane. You cannot write p(x)p(\mathbf{x}) as a function of two coordinates and integrate it over a region. A density only appears once the noise spreads the mass off the curve.

Adding noise

Adding σ ε\sigma\,\varepsilon gives each point a small Gaussian cloud around its position on the arc. The result is a half-moon whose band extends about ±σ\pm\sigma around the arc.
Output from cell 5 With σ>0\sigma > 0 the distribution of XX has a density on the whole plane. It is the conditional Gaussian p(x∣z)=N(x; G(z), σ2I2)p(\mathbf{x} \mid z) = \mathcal{N}\big(\mathbf{x};\, G(z),\, \sigma^2 I_2\big) averaged over the latent variable, p(x)=∫N(x; G(z), σ2I2) N(z; 0,1) dz.p(\mathbf{x}) = \int \mathcal{N}\big(\mathbf{x};\, G(z),\, \sigma^2 I_2\big)\, \mathcal{N}(z;\, 0, 1)\, dz. This is the deep latent variable model of the variational inference introduction, with a fixed function in place of a trained network. The integral has no closed form here, which is the difficulty that variational inference addresses. The noise also matters beyond this example. Adding Gaussian noise to a distribution that lives on a curve or a surface gives it a density, and the density is smooth. Diffusion models use this fact: they add noise to data so that every intermediate distribution has a density that can be studied and learned.

Mapping a lower dimension to a higher one

The latent space here has one dimension and the data space has two. A latent variable model can do this because GG is only required to be a function from the latent space to the data space. An invertible flow cannot. A flow is a smooth bijection, so its input and output have the same dimension, and the change of variables formula relates the two densities through a square Jacobian. A one-dimensional Gaussian cannot be turned into a distribution on a two-dimensional space by such a map. The latent variable model avoids the restriction by letting the noise term fill in the missing dimension.

Angle and radius distributions

The angle ϑ\vartheta is uniform on [0,π][0, \pi]. The Kolmogorov-Smirnov statistic, the largest gap between the empirical CDF and the uniform CDF, is of order 1/n1/\sqrt{n} for nn samples from the uniform distribution. The radius R=∥X∥R = \lVert X \rVert follows a Rice distribution. The generator places every point at distance 1 from the origin, so RR is the length of a vector with mean of length 1 plus isotropic Gaussian noise of scale σ\sigma. That length is Rice distributed with parameters ν=1\nu = 1 and σ\sigma. The code compares the empirical radii with this distribution.
The threshold 1.63/n1.63/\sqrt{n} is the 1% critical value of the one-sample Kolmogorov-Smirnov test.
Found an error on this page? Report an issue.