Estimators¶
A gradient estimator
approximates the gradient of an objective that contains an expectation (Hoffman et al., 2013).
Reparameterized, the default, differentiates through a transformation of fixed noise
(reparameterization trick)
(Kingma and Welling, 2014). StickingTheLanding reduces its variance near convergence
(Roeder et al., 2017). DoublyReparameterized gives the DReG estimator for IWAEBound at large
\(K\) (Tucker et al., 2019). ScoreFunction gives the score function (REINFORCE) estimator with the draws
held fixed, for guides whose draws are not differentiable (Ranganath et al., 2014). ChiVI accepts
Reparameterized and ScoreFunction, which estimate the gradient of the exponentiated CUBO through
GradientEstimator.moment_surrogate (Dieng et al., 2017).
estimators
¶
Gradient estimators for variational objectives.
The lower-bound objectives in quivers.inference.objectives
are power-mean bounds of the per-draw log importance ratios
:math:\ell_n = \log p(z_n, y) - \log q_\phi(z_n) with particle
weights :math:\omega_n (1/K for independent draws, the stratum
weights for stratified ones):
.. math::
B_a = \frac{1}{a} \log \sum_n \omega_n e^{a \ell_n}
\quad (a \neq 0), \qquad
B_0 = \sum_n \omega_n \ell_n.
a = 0 is the ELBO, a = 1 the IWAE bound, and a = 1 - \alpha
the Rényi / VR-IWAE bound. A
GradientEstimator turns the per-draw log-densities into a surrogate
whose value is :math:B_a and whose gradient is an estimate of
:math:\nabla \mathbb{E}[B_a], the gradient of the expected Monte
Carlo objective, for both the variational parameters :math:\phi and
the model parameters :math:\theta:
Reparameterized: the pathwise gradient of :math:B_a. Unbiased for everya.StickingTheLanding: drops the direct dependence of :math:\log q_\phion :math:\phiand keeps the pathwise one (Roeder, Wu and Duvenaud 2017). The dropped term has mean zero only for the ELBO or a single draw, so other objectives with several draws reject it.DoublyReparameterized: rewrites the dropped score term with the reparameterization identity instead of discarding it (Tucker, Lawson, Gu and Maddison 2019 for IWAE; Daudel, Benton, Shi and Doucet 2023, Theorem 2, for VR-IWAE). Unbiased for independent draws; it coincides with sticking the landing ata = 0.ScoreFunction: REINFORCE (Ranganath, Gerrish and Blei 2014) with the draws held fixed, so it applies to guides whose draws are not differentiable.
ChiVI instead minimizes the exponentiated upper bound
:math:L_n = \mathbb{E}_q[(p/q)^n] = \exp(n\, \mathrm{CUBO}_n) of Dieng,
Tran, Ranganath, Paisley and Blei (2017), whose Monte Carlo estimate is
unbiased; it calls GradientEstimator.check_moment and
GradientEstimator.moment_surrogate, which Reparameterized (their
Eq. 3) and ScoreFunction (their Algorithm 1) implement and the other
estimators reject.
Estimators are stateless strategies. Objective implementations call
GradientEstimator.check with their bound and draw count, so an
(objective, estimator) pair without a valid estimator raises before
it is used, and GradientEstimator.surrogate on the particles they
draw.
References¶
- Reparameterization: Kingma and Welling 2013.
- Sticking the landing: Roeder, Wu and Duvenaud 2017.
- DReG: Tucker, Lawson, Gu and Maddison 2019.
- Generalized DReG for VR-IWAE: Daudel, Benton, Shi and Doucet 2023.
- Score function: Ranganath, Gerrish and Blei 2014.
- CHIVI: Dieng, Tran, Ranganath, Paisley and Blei 2017.
ParticleLogDensities
didactic-model
¶
ParticleLogDensities(*, log_p: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), log_q: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), log_weights: Tensor = field(opaque=True, extras=OPAQUE_BY_IDENTITY), log_q_detached: Tensor | None = field(opaque=True, extras=OPAQUE_BY_IDENTITY), log_p_fixed: Tensor | None = field(opaque=True, extras=OPAQUE_BY_IDENTITY), stratified: bool, num_particles: int)
Bases: ValueModel
Per-draw log-densities that an objective hands its estimator.
All tensors share the shape (N, batch), where N is the
number of draws per batch row (particles times strata). The
tensors compare by identity: two records are equal exactly when
they hold the same tensor objects and agree on stratified.
| PARAMETER | DESCRIPTION |
|---|---|
log_p
|
Model log-joint :math:
TYPE:
|
log_q
|
Guide log-density :math:
TYPE:
|
log_weights
|
Log particle weights :math:
TYPE:
|
log_q_detached
|
:math:
TYPE:
|
log_p_fixed
|
:math:
TYPE:
|
stratified
|
Whether the draws are stratified rather than independent draws from the guide.
TYPE:
|
num_particles
|
Number of independent particles
TYPE:
|
GradientEstimator
¶
Bases: ABC
Strategy for estimating :math:\nabla \mathbb{E}[B_a].
| ATTRIBUTE | DESCRIPTION |
|---|---|
fixed_draws |
Whether the estimator needs the guide's draws held fixed. When
true, objectives draw independent samples with
TYPE:
|
needs_detached_parameters |
Whether the estimator reads
TYPE:
|
needs_fixed_log_p
¶
Whether the estimator reads ParticleLogDensities.log_p_fixed.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/inference/estimators.py
check
¶
Raise if this estimator has no valid form for the bound.
Objectives call this at construction with their particle count and again on each call with the drawn particles.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
num_draws
|
Draws per batch row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the estimator is biased or undefined for this bound. |
Source code in src/quivers/inference/estimators.py
check_moment
¶
Raise unless this estimator applies to the exponentiated CUBO.
ChiVI minimizes :math:L_n = \mathbb{E}_q[(p/q)^n] and calls
this at construction and on each call. The default rejects:
an estimator supports :math:L_n only if it implements
moment_surrogate.
| PARAMETER | DESCRIPTION |
|---|---|
order
|
The order :math:
TYPE:
|
num_particles
|
Particles per batch row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
Always, naming the estimators that apply. |
Source code in src/quivers/inference/estimators.py
moment_surrogate
¶
moment_surrogate(order: float, particles: ParticleLogDensities) -> Tensor
Surrogate whose gradient estimates :math:\nabla L_n, scaled.
| PARAMETER | DESCRIPTION |
|---|---|
order
|
The order :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
For an estimator that does not support :math: |
Source code in src/quivers/inference/estimators.py
surrogate
abstractmethod
¶
surrogate(power: float, particles: ParticleLogDensities) -> Tensor
Surrogate for the bound :math:B_a.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the particles lack a tensor the estimator needs. |
Source code in src/quivers/inference/estimators.py
Reparameterized
¶
Bases: GradientEstimator
Pathwise gradient of the bound (Kingma and Welling 2013).
Differentiates :math:B_a through the reparameterized draws, the
guide density, and the particle weights. Unbiased for every bound.
surrogate
¶
surrogate(power: float, particles: ParticleLogDensities) -> Tensor
The bound, differentiated through the draws, guide and weights.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
Source code in src/quivers/inference/estimators.py
check_moment
¶
Accept every order: the pathwise moment gradient is unbiased.
| PARAMETER | DESCRIPTION |
|---|---|
order
|
The order :math:
TYPE:
|
num_particles
|
Particles per row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
Source code in src/quivers/inference/estimators.py
moment_surrogate
¶
moment_surrogate(order: float, particles: ParticleLogDensities) -> Tensor
The pathwise surrogate for :math:\nabla L_n (Dieng et al. Eq. 3).
| PARAMETER | DESCRIPTION |
|---|---|
order
|
The order :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
Source code in src/quivers/inference/estimators.py
StickingTheLanding
¶
Bases: GradientEstimator
Sticking-the-landing gradient (Roeder, Wu and Duvenaud 2017).
Replaces :math:\log q_\phi(z) with
:math:\log q_{\bar\phi}(z), whose parameters are detached
while the draw is not, so the gradient keeps only the pathwise
dependence through :math:z (and through stratum weights). The
dropped term is :math:-\sum_n \bar w_n \nabla_\phi \log
q_\phi(z_n) with :math:\bar w = \mathrm{softmax}(\log \omega
+ a \ell). Its expectation is zero for the ELBO (a = 0, where
:math:\bar w = \omega) and for a single draw, and nonzero
otherwise, so check rejects other bounds with several draws; for
those, DoublyReparameterized is the unbiased counterpart. As
:math:q \to p the gradient's variance vanishes.
check
¶
Accept the ELBO, or any bound with a single unstratified draw.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
num_draws
|
Draws per row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
For another bound with several draws, where dropping the score term biases the gradient. |
Source code in src/quivers/inference/estimators.py
surrogate
¶
surrogate(power: float, particles: ParticleLogDensities) -> Tensor
The bound with the guide density's parameters detached.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the particles carry no detached guide log-density. |
Source code in src/quivers/inference/estimators.py
DoublyReparameterized
¶
Bases: GradientEstimator
Doubly reparameterized gradient (DReG).
The gradient of :math:B_a contains the score term
:math:-\sum_n \bar w_n \nabla_\phi \log q_\phi(z_n) with
:math:\bar w = \mathrm{softmax}(\log \omega + a \ell). The
reparameterization identity
:math:\mathbb{E}_q[g(z) \nabla_\phi \log q_\phi(z)] =
\mathbb{E}_\epsilon[\nabla_z g(z)\, \partial z / \partial \phi]
rewrites it as a pathwise term, giving
.. math::
\nabla_\phi \mathbb{E}[B_a] = \mathbb{E}\Bigl[\sum_n
\bigl((1 - a)\bar w_n + a \bar w_n^2\bigr)
\nabla_z \bigl(\log p(z_n) - \log q_{\bar\phi}(z_n)\bigr)
\frac{\partial z_n}{\partial \phi}\Bigr].
At a = 1 this is the IWAE estimator of Tucker, Lawson, Gu and
Maddison (2019); for a = 1 - \alpha it is Theorem 2 of Daudel,
Benton, Shi and Doucet (2023); at a = 0 it is sticking the
landing. The derivation uses only the identity above, so it holds
for every a, including ChiVI. Model parameters get
:math:\sum_n \bar w_n \nabla_\theta \log p_\theta(z_n), the
gradient of :math:B_a itself.
The identity requires each draw to come from :math:q_\phi, so
stratified draws (an AutoMixtureGuide enumerating its components)
are rejected unless a = 0.
needs_fixed_log_p
¶
Whether the surrogate reads the log-joint at detached draws.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/inference/estimators.py
check
¶
Refuse stratified draws for any bound but the ELBO.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
num_draws
|
Draws per row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the draws are stratified and the bound is not the ELBO. |
Source code in src/quivers/inference/estimators.py
surrogate
¶
surrogate(power: float, particles: ParticleLogDensities) -> Tensor
The bound with the DReG pathwise rewrite of its score term.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the particles lack the detached guide log-density or, for a bound other than the ELBO, the log-joint at the detached draws. |
ValueError
|
If the draws are stratified and the bound is not the ELBO. |
Source code in src/quivers/inference/estimators.py
ScoreFunction
¶
Bases: GradientEstimator
REINFORCE / black-box VI gradient (Ranganath, Gerrish and Blei 2014).
Uses the log-derivative identity with the draws held fixed:
.. math::
\nabla_\phi \mathbb{E}_{q_\phi}[B_a]
= \mathbb{E}\Bigl[\nabla_\phi B_a\big|_{z}
+ B_a \sum_n \nabla_\phi \log q_\phi(z_n)\Bigr].
For the ELBO (a = 0) the first term has mean zero and the
second decouples across draws, so the estimate is
:math:\sum_n \omega_n \ell_n \nabla_\phi \log q_\phi(z_n).
The objectives draw independent samples and detach them
(GradientEstimator.fixed_draws), so the estimator applies to
guides whose rsample is not differentiable. Model parameters
receive :math:\nabla_\theta B_a at the fixed draws. Variance is
typically orders of magnitude higher than the reparameterized
estimator's.
check
¶
Refuse stratified draws, which are not independent draws from q.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
num_draws
|
Draws per row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the draws are stratified. |
Source code in src/quivers/inference/estimators.py
surrogate
¶
surrogate(power: float, particles: ParticleLogDensities) -> Tensor
The bound with the log-derivative term at the fixed draws.
| PARAMETER | DESCRIPTION |
|---|---|
power
|
The bound's exponent :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the draws are stratified. |
Source code in src/quivers/inference/estimators.py
check_moment
¶
Refuse stratified draws, as for the bound.
| PARAMETER | DESCRIPTION |
|---|---|
order
|
The order :math:
TYPE:
|
num_particles
|
Particles per row.
TYPE:
|
stratified
|
Whether the draws are stratified.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the draws are stratified. |
Source code in src/quivers/inference/estimators.py
moment_surrogate
¶
moment_surrogate(order: float, particles: ParticleLogDensities) -> Tensor
The score-function surrogate for :math:\nabla L_n.
| PARAMETER | DESCRIPTION |
|---|---|
order
|
The order :math:
TYPE:
|
particles
|
The drawn particles' log-densities.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the draws are stratified. |
RuntimeError
|
If a scaled importance weight overflows. |
Source code in src/quivers/inference/estimators.py
power_mean_bound
¶
The power-mean bound :math:B_a of weighted log ratios.
| PARAMETER | DESCRIPTION |
|---|---|
log_ratio
|
Per-draw :math:
TYPE:
|
log_weights
|
Per-draw :math:
TYPE:
|
power
|
The exponent :math:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
:math: |
Source code in src/quivers/inference/estimators.py
leave_one_out_log_scale
¶
Per-draw stabilizing log-scale from the other particles' draws.
For a draw in particle :math:k the scale is
:math:m_{-k} = \max_{j \notin k} n \ell_j, the largest scaled
log ratio among the draws of the other particles. It is independent
of particle :math:k's own draws, so dividing particle :math:k's
term by :math:e^{m_{-k}} rescales its expected gradient by the
positive constant :math:\mathbb{E}[e^{-m_{-k}}] without biasing
its direction.
| PARAMETER | DESCRIPTION |
|---|---|
scaled_log_ratio
|
:math:
TYPE:
|
num_particles
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Detached, shape |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/quivers/inference/estimators.py
Cited sources¶
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.
Diederik P. Kingma and Max Welling. Auto-Encoding Variational Bayes. In International Conference on Learning Representations. 2014. Accepted conference paper; manuscript available as arXiv:1312.6114. URL: https://arxiv.org/abs/1312.6114, doi:10.48550/arXiv.1312.6114.
Geoffrey Roeder, Yuhuai Wu, and David Duvenaud. Sticking the Landing: Simple, Lower-Variance Gradient Estimators for Variational Inference. In Advances in Neural Information Processing Systems, volume 30. 2017. URL: https://proceedings.neurips.cc/paper/2017/hash/e91068fff3d7fa1594dfdf3b4308433a-Abstract.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.
Rajesh Ranganath, Sean Gerrish, and David Blei. Black Box Variational Inference. In Proceedings of the Seventeenth International Conference on Artificial Intelligence and Statistics, volume 33 of Proceedings of Machine Learning Research, 814–822. 2014. URL: https://proceedings.mlr.press/v33/ranganath14.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.