06 · Train and sample a mixture
This part is a Jupyter notebook. It implements the training objective from Part 3 and uses the fitted score in the probability-flow ODE. Mathematical explanations in LaTeX precede each code step; the notebook includes saved outputs and figures.
Read the worked notebook · Download the Jupyter notebook
The data distribution is
\[p_0(x)=\tfrac12\mathcal N(x;-2,0.25)+\tfrac12\mathcal N(x;2,0.25).\]
The notebook fixes a linear VP schedule, derives the noisy mixture’s reference score, trains a small noise-prediction network, converts its output into a score, and generates samples using explicit Euler integration with decreasing time. It follows Song et al., particularly Eqs. (7) and (13).
The main experiment starts from the actual terminal mixture, so its initial law matches the derived vector field. A second learned-field run starts from standard Gaussian noise, as in practical generation. Both sample runs are compared with the positive-time target density. A separate paired quantile experiment with the exact field isolates terminal-prior substitution. The notebook includes score errors, density and CDF plots, mode mass, moments, Wasserstein distance, and an exact-score Euler convergence check.
Run locally
Use a Python environment with NumPy, SciPy, Matplotlib, PyTorch, and Jupyter support. The notebook contains all helper functions; no external data, checkpoint, or GPU is required. Run cells from top to bottom. The default 8,000 training updates can be reduced for a quick code check, with an expected loss of accuracy.
Download the complete teaching bundle for requirements and all related notebooks. Part 3 develops the training mathematics; Part 4 contains the earlier focused experiments.
Other densities to investigate
| Density | What to investigate |
|---|---|
| Shifted or rescaled Gaussian | Check signs, means, variances, and the exact transport |
| Unequally weighted Gaussian mixture | Whether generated samples recover mode probabilities |
| Gaussian mixture with unequal variances | Whether narrow and broad components are both learned |
| Three-component Gaussian mixture | Coverage of multiple modes |
| Standard Laplace | A sharp peak and exponential tails |
| Student t with five degrees of freedom | Tail quantiles and finite-rate terminal approximation |
| Uniform on a bounded interval | Smoothed boundaries and positive-time endpoint effects |
Start with unequal weights in the existing mixture, then unequal component variances. Both changes preserve analytic noisy densities and scores. The notebook indicates the additional reference calculations needed for Laplace, Student t, and bounded-support examples.
Continue to 07 · Empirical-score sampling to replace neural estimation with exact softmax weights on a stored observation bank. The scalar and MNIST notebooks make the supporting observations visible.