Objectives¶
A variational objective measures how well
a guide approximates the posterior. The
evidence lower bound (ELBO) is the expected
log joint density minus the expected log density of the guide \(q\), and its gap from the log
evidence is the KL divergence \(\mathrm{KL}(q \,\|\, p)\) between the guide and the posterior \(p\)
(Blei et al., 2017; Hoffman et al., 2013; Pyro Developers, n.d.-g). The module gives ELBO, IWAEBound (Burda et al., 2016), RenyiBound (Li and Turner, 2016),
VRIWAEBound (Daudel et al., 2023), ChiVI (Dieng et al., 2017), RWS (Bornschein and Bengio, 2015), and
DReGsBound (Tucker et al., 2019). Each pairs with a gradient estimator.
objectives
¶
Variational objectives.
An Objective is a torch.nn.Module-callable that, given
(model, guide, x, observations), returns a scalar loss whose
backward() produces a gradient on the model and guide
parameters.
ELBO, IWAEBound, RenyiBound, VRIWAEBound and DReGsBound
are Monte Carlo power-mean lower bounds of the per-draw log
importance ratios :math:\ell_n = \log p(z_n, y) - \log q_\phi(z_n):
.. math::
B_a = \frac{1}{a} \log \sum_n \omega_n e^{a \ell_n}, \qquad
B_0 = \sum_n \omega_n \ell_n,
with exponent a = 0 (ELBO), 1 (IWAE) or 1 - alpha (Rényi
and VR-IWAE), and particle weights :math:\omega_n.
Each takes a quivers.inference.estimators.GradientEstimator
that estimates :math:\nabla \mathbb{E}[B_a] for the model and
variational parameters, and checks at construction and on each call
that the estimator is valid for the bound; a pair without a valid
estimator raises ValueError naming one that is. ChiVI minimizes
the exponentiated upper bound :math:\exp(n\, \mathrm{CUBO}_n) of
Dieng et al. (2017), whose Monte Carlo estimate is unbiased, and
reports the :math:K-sample CUBO estimate. RWS is not a
bound and takes no estimator: it implements the wake-phase updates of
reweighted wake-sleep.
Each particle is a call to Guide.rsample_strata, which returns
one or more weighted strata whose weighted average estimates
expectations under the guide density. An ordinary guide returns one
stratum; AutoMixtureGuide returns one per component, so every bound
here becomes its stratified counterpart for a mixture guide (the
stratified ELBO and SIWAE of
Morningstar, Vikram, Ham, Gallagher and Dillon 2021),
with gradients to the mixture logits through the stratum weights. An
estimator that needs fixed draws (ScoreFunction) gets independent
Guide.rsample draws instead, detached before they are scored.
Per-draw log-densities are stacked along a leading axis of shape
(N, batch). An input of batch rows is that many independent
runs of the model, each with its own latent draws, so every objective
sums its per-run terms: the loss of B runs is the sum of B
one-run losses, and its optimum is the one-run optimum. The draws run in a Python for loop over
num_particles, since the model's runtime path is not vectorized
over a Monte Carlo dimension; the per-step cost scales linearly in the
number of draws.
References¶
- Standard ELBO: Kingma and Welling 2013.
- IWAE: Burda, Grosse and Salakhutdinov 2016.
- Rényi divergence VI: Li and Turner 2016.
- VR-IWAE: Daudel, Benton, Shi and Doucet 2023.
- CUBO / chi-square VI: Dieng, Tran, Ranganath, Paisley and Blei 2017.
- Reweighted wake-sleep: Bornschein and Bengio 2015.
- DReG: Tucker, Lawson, Gu and Maddison 2019.
Objective
¶
Bases: Module, ABC
Base class for variational objectives.
Subclasses implement forward to return a scalar loss whose
backward() produces the objective's gradient.
forward
abstractmethod
¶
forward(model: MonadicProgram, guide: Guide, x: Tensor, observations: dict[str, Tensor]) -> Tensor
Scalar loss for one optimisation step.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
The generative model.
TYPE:
|
guide
|
The variational guide.
TYPE:
|
x
|
Program input, shape
TYPE:
|
observations
|
Observed-site values.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar loss: the sum of the per-run losses. |
Source code in src/quivers/inference/objectives.py
ELBO
¶
ELBO(num_particles: int = 1, estimator: GradientEstimator | None = None)
Bases: _PowerMeanBound
Evidence lower bound objective.
.. math::
\mathcal{L}_{\mathrm{ELBO}}
= \mathbb{E}_{q_\phi(z)} \bigl[ \log p(z, y) - \log q_\phi(z) \bigr]
\leq \log p(y).
Returns the negated estimate so it can be plugged into a
minimizer. num_particles averages independent Monte Carlo
estimates (the bound's exponent is a = 0). Every estimator is
valid: DoublyReparameterized coincides with StickingTheLanding
here.
| PARAMETER | DESCRIPTION |
|---|---|
num_particles
|
Number of independent guide samples per step. Default
TYPE:
|
estimator
|
Gradient-estimator strategy. Default
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
IWAEBound
¶
IWAEBound(num_particles: int = 8, estimator: GradientEstimator | None = None)
Bases: _PowerMeanBound
Importance-weighted bound (Burda-Grosse-Salakhutdinov 2016).
.. math::
\mathcal{L}_{\mathrm{IWAE}}
= \mathbb{E}\Bigl[\log \frac{1}{K} \sum_{k=1}^{K}
\frac{p(z_k, y)}{q_\phi(z_k)}\Bigr] \leq \log p(y),
tighter than the ELBO for :math:K > 1 and approaching
:math:\log p(y) as :math:K \to \infty (exponent a = 1).
The default estimator is DoublyReparameterized, because the
reparameterized gradient's signal-to-noise ratio for the
variational parameters collapses as :math:K grows
(Tucker-Lawson-Gu-Maddison 2019). StickingTheLanding is biased
for this bound and is rejected when num_particles > 1.
For a guide with several strata per draw (AutoMixtureGuide) the
bound is the stratified IWAE bound
:math:\log \frac{1}{K} \sum_k \sum_s \omega_s\,
p(z_{ks}, y) / q_\phi(z_{ks}), still a lower bound on
:math:\log p(y). DoublyReparameterized does not apply to
stratified draws and raises; pass estimator=Reparameterized().
| PARAMETER | DESCRIPTION |
|---|---|
num_particles
|
Number of guide samples per step. Default
TYPE:
|
estimator
|
Gradient-estimator strategy. Default
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
RenyiBound
¶
RenyiBound(alpha: float = 0.5, num_particles: int = 8, estimator: GradientEstimator | None = None)
Bases: _PowerMeanBound
Rényi α-divergence variational bound (Li-Turner 2016).
.. math::
\mathcal{L}_\alpha = \frac{1}{1 - \alpha}
\log \mathbb{E}_q\Bigl[ \bigl(p(z, y) / q_\phi(z)\bigr)^{1-\alpha}\Bigr],
estimated with num_particles draws as
:math:\frac{1}{1-\alpha} \log \frac{1}{K} \sum_k
(p_k/q_k)^{1-\alpha} (exponent a = 1 - alpha), the same Monte
Carlo estimator as VRIWAEBound. Recovers the ELBO as
:math:\alpha \to 1 and the IWAE bound at :math:\alpha = 0.
For :math:\alpha \in [0, 1) the estimate's expectation is a
lower bound on :math:\log p(y). For :math:\alpha < 0 the
exact :math:\mathcal{L}_\alpha is an upper bound, but the
:math:K-sample estimate is biased below it, so its expectation is
not guaranteed to bound :math:\log p(y) from either side.
StickingTheLanding is biased here and is rejected when
num_particles > 1; DoublyReparameterized is valid
(Daudel-Benton-Shi-Doucet 2023, Theorem 2).
| PARAMETER | DESCRIPTION |
|---|---|
alpha
|
Divergence order.
TYPE:
|
num_particles
|
Number of guide samples per step. Default
TYPE:
|
estimator
|
Gradient-estimator strategy. Default
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
VRIWAEBound
¶
VRIWAEBound(alpha: float = 0.0, num_particles: int = 8, estimator: GradientEstimator | None = None)
Bases: _PowerMeanBound
Variational Rényi-IWAE bound (Daudel-Benton-Shi-Doucet 2023).
Unifies ELBO, IWAEBound, and
RenyiBound into a single bound parameterized by
alpha and num_particles:
.. math::
\mathcal{L}_{\mathrm{VR\text{-}IWAE}}
= \mathbb{E}\Bigl[\frac{1}{1 - \alpha} \,\log\,
\frac{1}{K} \sum_{k=1}^{K} \Bigl(\frac{p}{q}\Bigr)^{1-\alpha}\Bigr].
Special cases:
alpha = 0, K > 1→ IWAE bound.K = 1→ ELBO, for everyalpha.
For alpha in [0, 1) it is a lower bound on
:math:\log p(y) that interpolates between the IWAE bound and the
ELBO. DoublyReparameterized is the generalized DReG estimator of
Theorem 2 of Daudel, Benton, Shi and Doucet (2023);
StickingTheLanding is biased here and is rejected when
num_particles > 1.
| PARAMETER | DESCRIPTION |
|---|---|
alpha
|
Divergence order;
TYPE:
|
num_particles
|
Number of guide samples per step. Default
TYPE:
|
estimator
|
Gradient-estimator strategy. Default
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
ChiVI
¶
ChiVI(n: float = 2.0, num_particles: int = 8, estimator: GradientEstimator | None = None)
Bases: Objective
Chi-squared variational inference, CHIVI (Dieng et al. 2017).
Minimises the :math:\chi^n divergence from the posterior to the
guide through the CUBO
.. math::
\mathrm{CUBO}_n(\phi)
= \tfrac{1}{n} \log \mathbb{E}_{q_\phi}
\Bigl[\bigl(p(z, y) / q_\phi(z)\bigr)^{n}\Bigr]
\geq \log p(y) \qquad (n > 1).
What is minimised. Following Dieng, Tran, Ranganath, Paisley
and Blei (2017, Section 2.2), the objective minimises the
exponentiated bound :math:L_n = \exp(n\, \mathrm{CUBO}_n) =
\mathbb{E}_q[(p/q)^n], which has the same minimisers (exp is
increasing) and an unbiased Monte Carlo estimate
:math:\hat L_n = \frac{1}{K}\sum_k (p_k/q_k)^n, so its gradient
estimate is unbiased too. The log of :math:\hat L_n, by contrast,
is biased downward by Jensen's inequality, and so is its gradient.
Stabilisation. :math:(p/q)^n overflows for moderate log
weights, so particle :math:k's term is divided by
:math:e^{m_{-k}}, where :math:m_{-k} = \max_{j \neq k} n \log
w_j is the largest scaled log weight among the other particles
(quivers.inference.estimators.leave_one_out_log_scale). This
is the max-subtraction of Dieng et al.'s Algorithm 1 with the
particle's own draws left out: :math:m_{-k} is independent of
particle :math:k's draws, so the expected update is
:math:c\, \nabla L_n with :math:c = \mathbb{E}[e^{-m_{-k}}] > 0,
the same for every particle. The stabilisation therefore rescales
the gradient by a positive, parameter-dependent factor (an
adaptive step size) and leaves its direction unbiased and the
stationary points of :math:L_n unchanged; subtracting the
maximum over all draws would correlate the scale with the
gradient and bias its direction. It requires at least two
particles. The estimate's variance is finite only when
:math:\mathbb{E}_q[(p/q)^{2n}] is, that is, when the guide's tails
are at least as heavy as the posterior's to that order, so start
from a guide wider than the posterior.
What is reported. The returned tensor's value is the
:math:K-sample estimate
:math:\widehat{\mathrm{CUBO}}_n = \tfrac{1}{n} \log \hat L_n,
summed over the runs, directly comparable to the negated
ELBO loss for the sandwich :math:\mathrm{ELBO} \le \log p(y)
\le \mathrm{CUBO}_n. Its gradient is the stabilised gradient of
:math:\hat L_n above, not the gradient of the reported value.
The reported estimate is biased downward at finite :math:K
(:math:\mathbb{E}[\widehat{\mathrm{CUBO}}_n] \le
\mathrm{CUBO}_n), so for small :math:K it can fall below
:math:\log p(y); the bias vanishes as :math:K grows.
Estimators. Reparameterized gives Dieng et al.'s Eq. 3,
:math:\frac{n}{K}\sum_k w_k^n \nabla \log w_k, and
ScoreFunction their score-function form (Algorithm 1),
:math:(1 - n)\, w^n \nabla_\phi \log q_\phi for the guide and
:math:n\, w^n \nabla_\theta \log p_\theta for the model, at fixed
draws. StickingTheLanding and DoublyReparameterized are rejected.
With an AutoMixtureGuide the stratified draws weight each term by
its stratum weight, and :math:\hat L_n stays unbiased.
Reference: Dieng, Tran, Ranganath, Paisley and Blei 2017.
| PARAMETER | DESCRIPTION |
|---|---|
n
|
Order of the divergence (
TYPE:
|
num_particles
|
Number of independent guide samples
TYPE:
|
estimator
|
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
RWS
¶
Bases: Objective
Reweighted wake-sleep, wake phase (Bornschein and Bengio 2015).
Draws num_particles samples :math:z_k \sim q_\phi, holds them
fixed, and forms self-normalised importance weights
:math:\tilde w_k \propto \omega_k\, p(z_k, y) / q_\phi(z_k)
(detached). The loss
.. math::
\mathcal{L}_{\mathrm{RWS}}
= -\sum_k \tilde w_k \log p_\theta(z_k, y)
-\sum_k \tilde w_k \log q_\phi(z_k)
gives the wake-phase model update
:math:\sum_k \tilde w_k \nabla_\theta \log p_\theta(z_k, y)
and the wake-phase guide update
:math:\sum_k \tilde w_k \nabla_\phi \log q_\phi(z_k), a
self-normalised importance-sampling estimate of
:math:-\nabla_\phi \mathrm{KL}(p(z \mid y) \| q_\phi).
Because the draws are fixed, neither term moves the guide through
the draws, and the model term does not move the guide at all. With
an AutoMixtureGuide the draws are its stratified draws and
:math:\omega_k the stratum weights. The sleep phase, which trains
an amortised guide on samples from the model, is not part of this
objective. RWS is not a bound and takes no gradient estimator.
Reference: Bornschein and Bengio 2015.
| PARAMETER | DESCRIPTION |
|---|---|
num_particles
|
Number of guide samples per step. Default
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
DReGsBound
¶
Bases: _PowerMeanBound
Doubly reparameterised IWAE bound (Tucker et al. 2019).
The bound is the IWAE bound; the estimator is the DReG
surrogate that removes the score-function term whose signal-
to-noise ratio collapses as :math:K grows in the naive
reparameterised IWAE gradient. Equivalent to
IWAEBound(num_particles=K, estimator=DoublyReparameterized())
but exposed as a first-class objective for callers that want
to switch bound + estimator together.
Reference: Tucker, Lawson, Gu and Maddison 2019.
The DReG surrogate requires independent draws from the
guide; with a stratified guide (AutoMixtureGuide) it raises,
and IWAEBound with estimator=Reparameterized() gives the
stratified bound instead.
| PARAMETER | DESCRIPTION |
|---|---|
num_particles
|
Number of guide samples per step. DReG's variance-
reduction benefit is largest for
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/objectives.py
Cited sources¶
David M. Blei, Alp Kucukelbir, and Jon D. McAuliffe. Variational Inference: A Review for Statisticians. Journal of the American Statistical Association, 112(518):859–877, 2017. URL: https://doi.org/10.1080/01621459.2017.1285773, doi:10.1080/01621459.2017.1285773.
Matthew D. Hoffman, David M. Blei, Chong Wang, and John Paisley. Stochastic Variational Inference. Journal of Machine Learning Research, 14(40):1303–1347, 2013. URL: https://www.jmlr.org/papers/v14/hoffman13a.html.
Pyro Developers. SVI part I: an introduction to stochastic variational inference in Pyro. Pyro Tutorials, version 1.9.1, n.d.-g. URL: https://pyro.ai/examples/svi_part_i.html (visited on 2026-11-06).
Yuri Burda, Roger Grosse, and Ruslan Salakhutdinov. Importance Weighted Autoencoders. In International Conference on Learning Representations. 2016. URL: https://www.iclr.cc/archive/www/2016.html.
Yingzhen Li and Richard E. Turner. Rényi Divergence Variational Inference. In Advances in Neural Information Processing Systems, volume 29. 2016. URL: https://proceedings.neurips.cc/paper/2016/hash/7750ca3559e5b8e1f44210283368fc16-Abstract.html.
Kamélia Daudel, Joe Benton, Yuyang Shi, and Arnaud Doucet. Alpha-divergence Variational Inference Meets Importance Weighted Auto-Encoders: Methodology and Asymptotics. Journal of Machine Learning Research, 24(243):1–83, 2023. URL: https://jmlr.org/papers/v24/22-1160.html.
Adji Bousso Dieng, Dustin Tran, Rajesh Ranganath, John Paisley, and David Blei. Variational Inference via Chi Upper Bound Minimization. In Advances in Neural Information Processing Systems, volume 30. 2017. URL: https://proceedings.neurips.cc/paper/2017/hash/35464c848f410e55a13bb9d78e7fddd0-Abstract.html.
Jörg Bornschein and Yoshua Bengio. Reweighted Wake-Sleep. In International Conference on Learning Representations. 2015. URL: https://iclr.cc/archive/www/2015.html.
George Tucker, Dieterich Lawson, Shixiang Gu, and Chris J. Maddison. Doubly Reparameterized Gradient Estimators for Monte Carlo Objectives. In International Conference on Learning Representations. 2019. URL: https://openreview.net/forum?id=HkG3e205K7.