This practical notebook introduces how normalizing flows can be used to improve Monte Carlo estimation for a multi-modal target distribution.
The tutorial combines:
- flow-based density modeling,
- importance sampling,
- and Markov Chain Monte Carlo (MCMC).
By the end of the notebook, you should be able to:
- build and inspect a simple 2D Mixture of Gaussians (MoG) target,
- instantiate and validate a Real-NVP style normalizing flow,
- train the flow with a Monte Carlo approximation of KL divergence,
- compute and interpret importance weights,
- identify failure modes of importance sampling,
- use a trained flow as a proposal in MCMC.
The notebook is organized in five main parts:
-
Toy target distribution (2D MoG)
Defines a two-component Gaussian mixture with imbalanced weights. -
Normalizing flow basics
Instantiates a minimal flow and checks invertibility (forward/backward). -
Training the flow
Uses an empirical KL objective,$$\mathcal{L}(\theta) \approx \frac{1}{B}\sum_{k=1}^B \left[\log \rho_\theta(x^k)-\log \rho_*(x^k)\right],\quad x^k\sim \rho_\theta$$ and monitors convergence through loss and density plots.
-
Importance sampling with the trained flow
Computes importance weights,$$w_i = \frac{\rho_*(x_i)}{\rho_\theta(x_i)}$$ and uses normalized weights to estimate target-region probabilities and discuss when IS can fail.
-
Flow-based MCMC proposal
Demonstrates MALA and independent Metropolis-Hastings style sampling with multiple parallel chains.
sampling_gen_model_tutorial.ipynb: main tutorial notebook.models.py: MoG and normalizing flow implementations.utils_plot.py: plotting helpers.utils_mcmc.py: MCMC utilities (MALA and MH helpers).
- Open the notebook
sampling_gen_model_tutorial.ipynb. - Run cells from top to bottom.
- If you modify Python modules (
models.py,utils_mcmc.py), restart the kernel or reload modules before re-running dependent cells.
Suggested environment:
- Python 3.10+
- PyTorch
- Matplotlib
- NumPy
- tqdm
This tutorial is based on course material by Miss Marylou Gabrié, teacher at École Polytechnique, CMAP.
Please keep this attribution if you reuse, adapt, or share this notebook.