Skip to content
Ahmed Haroon
Machine Learning

Continuous Latent-Variable Models

Factor Analysis

When the number of features dd is large relative to the number of examples nn, estimating a full d×dd \times d covariance matrix can become unreliable or singular.

One simple solution is to assume the covariance matrix is diagonal, so each feature has its own variance but different features cannot be correlated:

Σjj=1ni=1n(xj(i)μj)2 \Sigma_{jj} = \frac{1}{n} \sum_{i=1}^n \left( x_j^{(i)}-\mu_j \right)^2

The problem is that a diagonal covariance cannot capture correlations between features.

Factor analysis keeps the model compact while still allowing correlations by using a lower-dimensional latent variable.

Let:

xRd x\in\mathbb R^d

and let:

zRk,k<d z\in\mathbb R^k, \qquad k<d

be a lower-dimensional latent variable.

The loading matrix is:

WRd×k W\in\mathbb R^{d\times k}

and:

ΨRd×d \Psi\in\mathbb R^{d\times d}

is a positive diagonal matrix.

The factor analysis model is:

zN(0,Ik) z \sim \mathcal N(0,I_k) xzN(μ+Wz,Ψ) x\mid z \sim \mathcal N(\mu+Wz,\Psi)

We can equivalently write:

zN(0,Ik) z \sim \mathcal N(0,I_k) ϵN(0,Ψ) \epsilon \sim \mathcal N(0,\Psi) x=μ+Wz+ϵ x = \mu+Wz+\epsilon

We assume zz and ϵ\epsilon are independent.

The latent variable zz captures shared variation across features, while ϵ\epsilon represents feature-specific noise.

For a random variable:

xN(μ,σ2) x \sim \mathcal N(\mu,\sigma^2)

we can write:

x=μ+σϵ x = \mu+\sigma\epsilon

where:

ϵN(0,1) \epsilon \sim \mathcal N(0,1)

Because xx is a linear combination of Gaussian variables, it is also Gaussian.

Its distribution is:

xN(μ,WWT+Ψ) x \sim \mathcal N \left( \mu, WW^T+\Psi \right)
A data cloud with the diagonal covariance drawn as an axis-aligned dashed ellipse that misses the tilt, alongside the factor analysis covariance drawn as density bands tilted to follow the correlation

A diagonal Σ\Sigma can only stretch along the coordinate axes, while the factor analysis covariance WWT+ΨWW^T+\Psi can capture correlations between features.

The loading matrix is not unique.

For any orthogonal matrix:

RRk×k R\in\mathbb R^{k\times k}

replacing WW by WRWR and zz by RTzR^Tz preserves both the standard-normal prior and the covariance:

WWT+Ψ WW^T+\Psi

This creates a rotational ambiguity, so the orientation of the latent factors is not uniquely identified.

More generally, because Ψ\Psi is also unknown, identifying the loading subspace can require additional conditions.

See derivation
E[x]=E[μ+Wz+ϵ] \mathbb E[x] = \mathbb E[\mu+Wz+\epsilon] =E[μ]+E[Wz]+E[ϵ] = \mathbb E[\mu] + \mathbb E[Wz] + \mathbb E[\epsilon]
=μ+WE[z]+E[ϵ] = \mu + W\mathbb E[z] + \mathbb E[\epsilon]
=μ = \mu

Now consider the covariance:

Var(x)=E[(xE[x])(xE[x])T] \operatorname{Var}(x) = \mathbb E \left[ (x-\mathbb E[x]) (x-\mathbb E[x])^T \right] =E[(μ+Wz+ϵμ)(μ+Wz+ϵμ)T] = \mathbb E \left[ (\mu+Wz+\epsilon-\mu) (\mu+Wz+\epsilon-\mu)^T \right] =E[(Wz+ϵ)(Wz+ϵ)T] = \mathbb E \left[ (Wz+\epsilon) (Wz+\epsilon)^T \right] =E[WzzTWT+WzϵT+ϵzTWT+ϵϵT] = \mathbb E \left[ Wzz^TW^T + Wz\epsilon^T + \epsilon z^TW^T + \epsilon\epsilon^T \right] =WE[zzT]WT+WE[zϵT]+E[ϵzT]WT+E[ϵϵT] = W\mathbb E[zz^T]W^T + W\mathbb E[z\epsilon^T] + \mathbb E[\epsilon z^T]W^T + \mathbb E[\epsilon\epsilon^T]
=WE[zzT]WT+E[ϵϵT] = W\mathbb E[zz^T]W^T + \mathbb E[\epsilon\epsilon^T]
=WWT+Ψ = WW^T+\Psi

Thus, the log-likelihood of the data under the model is:

(μ,W,Ψ)=logi=1n1(2π)d/2WWT+Ψ1/2exp(12(x(i)μ)T(WWT+Ψ)1(x(i)μ)) \ell(\mu,W,\Psi) = \log \prod_{i=1}^{n} \frac{ 1 }{ (2\pi)^{d/2} |WW^T+\Psi|^{1/2} } \exp \left( -\frac12 (x^{(i)}-\mu)^T (WW^T+\Psi)^{-1} (x^{(i)}-\mu) \right)

The mean has the closed-form estimate:

μ=1ni=1nx(i) \mu = \frac1n \sum_{i=1}^n x^{(i)}

The parameters WW and Ψ\Psi, however, are coupled inside the covariance:

WWT+Ψ WW^T+\Psi

so we estimate them using Expectation Maximization.

Posterior Inference

In the E-step, we infer the latent factors zz for each observation using the current values of WW and Ψ\Psi.

Because the model is linear-Gaussian, the posterior p(zx)p(z\mid x) is Gaussian and can be computed exactly:

zxN(μzx,Σzx) z\mid x \sim \mathcal N \left( \mu_{z\mid x}, \Sigma_{z\mid x} \right)

where:

μzx=WT(WWT+Ψ)1(xμ) \mu_{z\mid x} = W^T \left( WW^T+\Psi \right)^{-1} (x-\mu)

and:

Σzx=IkWT(WWT+Ψ)1W \Sigma_{z\mid x} = I_k - W^T \left( WW^T+\Psi \right)^{-1} W

For fixed model parameters, every observation has the same posterior covariance, but a different posterior mean determined by xμx-\mu.

See derivation

For jointly Gaussian random vectors zz and xx, the conditional distribution zxz\mid x is also Gaussian:

zxN(μzx,Σzx) z\mid x \sim \mathcal N \left( \mu_{z\mid x}, \Sigma_{z\mid x} \right)

with:

μzx=μz+ΣzxΣxx1(xμx) \mu_{z\mid x} = \mu_z + \Sigma_{zx} \Sigma_{xx}^{-1} (x-\mu_x)

and:

Σzx=ΣzzΣzxΣxx1Σxz \Sigma_{z\mid x} = \Sigma_{zz} - \Sigma_{zx} \Sigma_{xx}^{-1} \Sigma_{xz}

From the model:

μz=0 \mu_z=0

and:

Σzz=Var(z)=Ik \Sigma_{zz} = \operatorname{Var}(z) = I_k

From the marginal distribution of xx:

μx=μ \mu_x=\mu

and:

Σxx=WWT+Ψ \Sigma_{xx} = WW^T+\Psi

Now:

Σzx=Cov(z,x) \Sigma_{zx} = \operatorname{Cov}(z,x) =E[(zE[z])(xE[x])T] = \mathbb E \left[ (z-\mathbb E[z]) (x-\mathbb E[x])^T \right]
=E[z(xμ)T] = \mathbb E \left[ z(x-\mu)^T \right]
=E[z(Wz+ϵ)T] = \mathbb E \left[ z(Wz+\epsilon)^T \right]
=E[zzTWT]+E[zϵT] = \mathbb E \left[ zz^TW^T \right] + \mathbb E \left[ z\epsilon^T \right]
=E[zzT]WT = \mathbb E[zz^T]W^T
=WT = W^T

Similarly:

Σxz=Cov(x,z)=W \Sigma_{xz} = \operatorname{Cov}(x,z) = W

Putting everything together:

zxN(μzx,Σzx) z\mid x \sim \mathcal N \left( \mu_{z\mid x}, \Sigma_{z\mid x} \right)

with:

μzx=WT(WWT+Ψ)1(xμ) \mu_{z\mid x} = W^T \left( WW^T+\Psi \right)^{-1} (x-\mu)

and:

Σzx=IkWT(WWT+Ψ)1W \Sigma_{z\mid x} = I_k - W^T \left( WW^T+\Psi \right)^{-1} W

The E-step for EM is therefore:

Qi(z(i))=p(z(i)x(i);μ,W,Ψ) Q_i(z^{(i)}) = p \left( z^{(i)} \mid x^{(i)}; \mu,W,\Psi \right) =1(2π)k/2Σz(i)x(i)1/2exp(12(z(i)μz(i)x(i))TΣz(i)x(i)1(z(i)μz(i)x(i))) = \frac{ 1 }{ (2\pi)^{k/2} |\Sigma_{z^{(i)}\mid x^{(i)}}|^{1/2} } \exp \left( -\frac12 \left( z^{(i)}-\mu_{z^{(i)}\mid x^{(i)}} \right)^T \Sigma_{z^{(i)}\mid x^{(i)}}^{-1} \left( z^{(i)}-\mu_{z^{(i)}\mid x^{(i)}} \right) \right)

EM Parameter Updates

In the M-step, we maximize the evidence lower bound:

L(D;Q,μ,W,Ψ)=i=1nEz(i)Qi[logp(x(i),z(i);μ,W,Ψ)Qi(z(i))] \mathcal L \left( \mathcal D; Q, \mu, W, \Psi \right) = \sum_{i=1}^n \mathbb E_{z^{(i)}\sim Q_i} \left[ \log \frac{ p(x^{(i)},z^{(i)};\mu,W,\Psi) }{ Q_i(z^{(i)}) } \right]

The marginal model has mean μ\mu, so its likelihood is maximized at the sample mean.

We calculate this once and center the data before running EM:

μ=1ni=1nx(i) \mu = \frac1n \sum_{i=1}^n x^{(i)}

The M-step only needs the first two moments of each posterior QiQ_i, so define:

mi=EQi[z(i)]=μz(i)x(i) m_i = \mathbb E_{Q_i}[z^{(i)}] = \mu_{z^{(i)}\mid x^{(i)}}

and:

Ci=EQi[z(i)z(i)T] C_i = \mathbb E_{Q_i} \left[ z^{(i)}z^{(i)T} \right] =Σz(i)x(i)+mimiT = \Sigma_{z^{(i)}\mid x^{(i)}} + m_im_i^T

At EM iteration tt, we compute mim_i and CiC_i from the posterior under the old parameters:

(W(t),Ψ(t)) (W^{(t)},\Psi^{(t)})

These moments remain fixed throughout the M-step.

The remaining updates are:

W=(i=1n(x(i)μ)miT)(i=1nCi)1 W = \left( \sum_{i=1}^n (x^{(i)}-\mu)m_i^T \right) \left( \sum_{i=1}^n C_i \right)^{-1}

and:

Ψ=diag(1ni=1nEQi[(x(i)μWz(i))(x(i)μWz(i))T]) \Psi = \operatorname{diag} \left( \frac1n \sum_{i=1}^n \mathbb E_{Q_i} \left[ (x^{(i)}-\mu-Wz^{(i)}) (x^{(i)}-\mu-Wz^{(i)})^T \right] \right)

Intuitively, WW learns how the latent factors explain shared variation in the data, while Ψ\Psi captures the remaining feature-specific variance.

See derivation
L(D;Q,μ,W,Ψ)=i=1nEz(i)Qi[logp(x(i),z(i);μ,W,Ψ)Qi(z(i))] \mathcal L \left( \mathcal D; Q, \mu, W, \Psi \right) = \sum_{i=1}^n \mathbb E_{z^{(i)}\sim Q_i} \left[ \log \frac{ p(x^{(i)},z^{(i)};\mu,W,\Psi) }{ Q_i(z^{(i)}) } \right] =i=1nEz(i)Qi[logp(x(i)z(i);μ,W,Ψ)+logp(z(i))logQi(z(i))] = \sum_{i=1}^n \mathbb E_{z^{(i)}\sim Q_i} \left[ \log p(x^{(i)}\mid z^{(i)};\mu,W,\Psi) + \log p(z^{(i)}) - \log Q_i(z^{(i)}) \right]
J(μ,W,Ψ)=i=1nEz(i)Qi[logp(x(i)z(i);μ,W,Ψ)] \mathcal J(\mu,W,\Psi) = \sum_{i=1}^n \mathbb E_{z^{(i)}\sim Q_i} \left[ \log p(x^{(i)}\mid z^{(i)};\mu,W,\Psi) \right]

Therefore:

J(μ,W,Ψ)=i=1nEz(i)Qi[log(1(2π)d/2Ψ1/2exp(12(x(i)μWz(i))TΨ1(x(i)μWz(i))))] \mathcal J(\mu,W,\Psi) = \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ \log \left( \frac{ 1 }{ (2\pi)^{d/2}|\Psi|^{1/2} } \exp \left( -\frac12 (x^{(i)}-\mu-Wz^{(i)})^T \Psi^{-1} (x^{(i)}-\mu-Wz^{(i)}) \right) \right) \right] =i=1nEz(i)Qi[12logΨd2log(2π)12(x(i)μWz(i))TΨ1(x(i)μWz(i))] = \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -\frac12\log|\Psi| - \frac d2\log(2\pi) - \frac12 (x^{(i)}-\mu-Wz^{(i)})^T \Psi^{-1} (x^{(i)}-\mu-Wz^{(i)}) \right]

The marginal distribution is:

xN(μ,WWT+Ψ) x \sim \mathcal N \left( \mu, WW^T+\Psi \right)

For any fixed covariance, differentiating its observed-data log-likelihood with respect to μ\mu gives:

μ=1ni=1nx(i) \mu = \frac1n \sum_{i=1}^n x^{(i)}

We therefore calculate μ\mu once and center the observations.

With centered data, each posterior mean is a linear transformation of x(i)μx^{(i)}-\mu, so:

imi=0 \sum_i m_i=0

exactly.

For matrices of compatible dimensions:

Atr(ABATC)=CAB+CTABT \nabla_A \operatorname{tr} \left( ABA^TC \right) = CAB + C^TAB^T

Taking the derivative with respect to WW:

12Wi=1nEz(i)Qi[(x(i)μWz(i))TΨ1(x(i)μWz(i))] -\frac12 \nabla_W \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ (x^{(i)}-\mu-Wz^{(i)})^T \Psi^{-1} (x^{(i)}-\mu-Wz^{(i)}) \right]
=12i=1nEz(i)Qi[W(z(i)TWTΨ1(x(i)μ)) = -\frac12 \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -\nabla_W \left( z^{(i)T} W^T \Psi^{-1} (x^{(i)}-\mu) \right) \right.
=12i=1nEz(i)Qi[Wtr(z(i)TWTΨ1(x(i)μ)) = -\frac12 \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -\nabla_W \operatorname{tr} \left( z^{(i)T} W^T \Psi^{-1} (x^{(i)}-\mu) \right) \right.
=12i=1nEz(i)Qi[Wtr(z(i)TWTΨ1(x(i)μ)) = -\frac12 \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -\nabla_W \operatorname{tr} \left( z^{(i)T} W^T \Psi^{-1} (x^{(i)}-\mu) \right) \right.
=12i=1nEz(i)Qi[2Wtr(z(i)TWTΨ1(x(i)μ)) = -\frac12 \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -2 \nabla_W \operatorname{tr} \left( z^{(i)T} W^T \Psi^{-1} (x^{(i)}-\mu) \right) \right.
=12i=1nEz(i)Qi[2Wtr(WTΨ1(x(i)μ)z(i)T) = -\frac12 \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -2 \nabla_W \operatorname{tr} \left( W^T \Psi^{-1} (x^{(i)}-\mu) z^{(i)T} \right) \right.
=12i=1nEz(i)Qi[2Ψ1(x(i)μ)z(i)T+Ψ1Wz(i)z(i)T+ΨTWz(i)z(i)T] = -\frac12 \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ -2 \Psi^{-1} (x^{(i)}-\mu) z^{(i)T} + \Psi^{-1} Wz^{(i)}z^{(i)T} + \Psi^{-T} Wz^{(i)}z^{(i)T} \right]
=i=1nEz(i)Qi[Ψ1(x(i)μ)z(i)TΨ1Wz(i)z(i)T] = \sum_{i=1}^{n} \mathbb E_{z^{(i)}\sim Q_i} \left[ \Psi^{-1} (x^{(i)}-\mu) z^{(i)T} - \Psi^{-1} Wz^{(i)}z^{(i)T} \right]

Setting the derivative equal to 00:

Ψ1(i=1nEQi[(x(i)μ)z(i)T]) \Psi^{-1} \left( \sum_{i=1}^{n} \mathbb E_{Q_i} \left[ (x^{(i)}-\mu)z^{(i)T} \right] \right) =Ψ1W(i=1nEQi[z(i)z(i)T]) = \Psi^{-1} W \left( \sum_{i=1}^{n} \mathbb E_{Q_i} \left[ z^{(i)}z^{(i)T} \right] \right)

Therefore:

W=(i=1n(x(i)μ)EQi[z(i)T])(i=1nEQi[z(i)z(i)T])1 W = \left( \sum_{i=1}^{n} (x^{(i)}-\mu) \mathbb E_{Q_i} [z^{(i)T}] \right) \left( \sum_{i=1}^{n} \mathbb E_{Q_i} [z^{(i)}z^{(i)T}] \right)^{-1}
=(i=1n(x(i)μ)miT)(i=1n(mimiT+Σz(i)x(i)))1 = \left( \sum_{i=1}^{n} (x^{(i)}-\mu)m_i^T \right) \left( \sum_{i=1}^{n} \left( m_im_i^T + \Sigma_{z^{(i)}\mid x^{(i)}} \right) \right)^{-1}

Thus:

W=(i=1n(x(i)μ)miT)(i=1nCi)1 W = \left( \sum_{i=1}^{n} (x^{(i)}-\mu)m_i^T \right) \left( \sum_{i=1}^{n} C_i \right)^{-1}

For the diagonal-noise update, let:

r(i)=x(i)μWz(i) r^{(i)} = x^{(i)}-\mu-Wz^{(i)}

and write:

Ψ=diag(ψ1,,ψd) \Psi = \operatorname{diag} (\psi_1,\ldots,\psi_d)

The terms involving one diagonal entry ψj\psi_j are:

n2logψj12ψji=1nEQi[(rj(i))2] -\frac n2 \log\psi_j - \frac{1}{2\psi_j} \sum_{i=1}^n \mathbb E_{Q_i} \left[ (r_j^{(i)})^2 \right]

Setting the derivative with respect to ψj\psi_j equal to 00 gives:

ψj=1ni=1nEQi[(rj(i))2] \psi_j = \frac1n \sum_{i=1}^n \mathbb E_{Q_i} \left[ (r_j^{(i)})^2 \right]

Thus, if:

Φ=1ni=1n[(x(i)μ)(x(i)μ)TWmi(x(i)μ)T \Phi = \frac1n \sum_{i=1}^n \left[ (x^{(i)}-\mu) (x^{(i)}-\mu)^T - Wm_i (x^{(i)}-\mu)^T \right.(x(i)μ)miTWT+WCiWT] \left. - (x^{(i)}-\mu) m_i^TW^T + WC_iW^T \right]

then the constrained M-step is:

Ψ=diag(Φ) \Psi = \operatorname{diag}(\Phi)

Variational Autoencoders

Let:

zN(0,Ik) z \sim \mathcal N(0,I_k)

be a latent variable, and let θ\theta be the parameters of a decoder network:

g(z;θ) g(z;\theta)

that maps:

zRk z\in\mathbb R^k

to:

Rd \mathbb R^d

For real-valued observations, suppose the decoder likelihood is:

xzN(g(z;θ),σ2Id) x\mid z \sim \mathcal N \left( g(z;\theta), \sigma^2I_d \right)

So the decoder takes a low-dimensional latent vector zz and maps it to a distribution over observations xx.

Variational Inference and the VAE Objective

To train the model, we would ideally compute the posterior:

p(zx;θ) p(z\mid x;\theta)

In simple latent-variable models such as factor analysis, this posterior has a closed form.

With a nonlinear decoder g(z;θ)g(z;\theta), it generally does not, because the marginal likelihood requires the integral:

p(x;θ)=p(xz;θ)p(z)dz p(x;\theta) = \int p(x\mid z;\theta) p(z)\,dz

VAEs therefore approximate the posterior with a tractable distribution:

Q(zx) Q(z\mid x)

The ELBO would be tight if this approximate posterior were equal to the exact posterior.

Recall:

ELBO(x;Q,θ)=logp(x;θ)DKL(Q(z)p(zx;θ)) \text{ELBO}(x;Q,\theta) = \log p(x;\theta) - D_{KL} \left( Q(z) \parallel p(z\mid x;\theta) \right)

Therefore:

logp(x;θ)=ELBO(x;Q,θ)+DKL(Q(z)p(zx;θ)) \log p(x;\theta) = \text{ELBO}(x;Q,\theta) + D_{KL} \left( Q(z) \parallel p(z\mid x;\theta) \right)

For fixed θ\theta, maximizing the ELBO over a variational family Q\mathcal Q is equivalent to finding the member closest to the exact posterior in this KL direction:

Qθ(zx)=argmaxQQELBO(x;Q,θ) Q_\theta^*(z\mid x) = \arg\max_{Q\in\mathcal Q} \text{ELBO}(x;Q,\theta) =argminQQDKL(Q(z)p(zx;θ)) = \arg\min_{Q\in\mathcal Q} D_{KL} \left( Q(z) \parallel p(z\mid x;\theta) \right)

If the exact posterior belongs to Q\mathcal Q, then:

Qθ=p(zx;θ) Q_\theta^* = p(z\mid x;\theta)

Otherwise, QθQ_\theta^* is the closest available approximation in this KL direction.

A common choice is a diagonal Gaussian approximate posterior:

Q(zx)=j=1kQj(zjx) Q(z\mid x) = \prod_{j=1}^k Q^j(z_j\mid x)

The diagonal covariance means the latent coordinates are independent under Q(zx)Q(z\mid x).

Let the encoder networks:

q(x;ϕ) q(x;\phi)

and:

v(x;ψ) v(x;\psi)

output the posterior mean and a positive variance vector, respectively.

Then:

Qi(z(i)x(i))=N(q(x(i);ϕ),diag(v(x(i);ψ))) Q_i \left( z^{(i)} \mid x^{(i)} \right) = \mathcal N \left( q(x^{(i)};\phi), \operatorname{diag} \left( v(x^{(i)};\psi) \right) \right)
An encoder maps x to a mean and variance, a latent z is sampled from them, and a decoder maps z back to a reconstruction

The encoder turns xx into the parameters of QQ: a mean q(x;ϕ)q(x;\phi) and a positive variance v(x;ψ)v(x;\psi). We sample a latent zQz\sim Q, and the decoder maps it to a distribution over xx.

The per-example ELBO is:

Li(ϕ,ψ,θ)=EzQi[logp(x(i)z;θ)]DKL(Qi(zx(i))p(z)) \mathcal L_i(\phi,\psi,\theta) = \mathbb E_{z\sim Q_i} \left[ \log p(x^{(i)}\mid z;\theta) \right] - D_{KL} \left( Q_i(z\mid x^{(i)}) \parallel p(z) \right)

The two terms play different roles.

The reconstruction term:

EzQi[logp(x(i)z;θ)] \mathbb E_{z\sim Q_i} \left[ \log p(x^{(i)}\mid z;\theta) \right]

rewards latent samples that allow the decoder to explain x(i)x^{(i)}.

The KL term:

DKL(Qi(zx(i))p(z)) D_{KL} \left( Q_i(z\mid x^{(i)}) \parallel p(z) \right)

keeps the approximate posterior close to the prior.

With the fixed-variance Gaussian decoder:

logp(xz;θ)=C12σ2xg(z;θ)22 \log p(x\mid z;\theta) = C - \frac{1}{2\sigma^2} \left\| x-g(z;\theta) \right\|_2^2

where CC is constant with respect to θ\theta.

Thus, maximizing the reconstruction term is equivalent to minimizing a scaled expected squared reconstruction error.

If the decoder variance is learned, its log-normalization term also matters, so the objective is not simply squared error.

The full-dataset objective is the sum of the per-example bounds:

L(D;ϕ,ψ,θ)=i=1nLi(ϕ,ψ,θ) \mathcal L \left( \mathcal D; \phi, \psi, \theta \right) = \sum_{i=1}^n \mathcal L_i (\phi,\psi,\theta)

Rather than computing separate variational parameters for every example, the encoder amortizes inference across the dataset.

We jointly optimize the encoder and decoder parameters using stochastic gradient ascent:

θθ+ηθL(D;ϕ,ψ,θ) \theta \leftarrow \theta + \eta \nabla_\theta \mathcal L \left( \mathcal D; \phi, \psi, \theta \right) ϕϕ+ηϕL(D;ϕ,ψ,θ) \phi \leftarrow \phi + \eta \nabla_\phi \mathcal L \left( \mathcal D; \phi, \psi, \theta \right) ψψ+ηψL(D;ϕ,ψ,θ) \psi \leftarrow \psi + \eta \nabla_\psi \mathcal L \left( \mathcal D; \phi, \psi, \theta \right)

The Reparameterization Trick

There is one remaining problem: the encoder parameters determine the distribution we sample zz from, so ordinary backpropagation cannot treat the sampled zz as a fixed input.

The reparameterization trick moves the randomness into a separate variable whose distribution does not depend on the encoder parameters.

Draw:

ξ(i)N(0,Ik) \xi^{(i)} \sim \mathcal N(0,I_k)

and define:

z^(i)=q(x(i);ϕ)+v(x(i);ψ)ξ(i) \hat z^{(i)} = q(x^{(i)};\phi) + \sqrt{ v(x^{(i)};\psi) } \odot \xi^{(i)}

where the square root is applied elementwise.

Now the randomness is entirely in ξ(i)\xi^{(i)}, whose distribution does not depend on the encoder parameters, so gradients can flow through the deterministic transformation that produces z^(i)\hat z^{(i)}.

A standard normal distribution shifted and scaled by mu and sigma into the latent distribution

Sampling zN(μ,diag(σ2))z\sim\mathcal N(\mu,\operatorname{diag}(\sigma^2)) is equivalent to drawing ξN(0,Ik)\xi\sim\mathcal N(0,I_k) and mapping z=μ+σξz=\mu+\sigma\odot\xi. Here σ=v\sigma=\sqrt v, so the randomness lives in ξ\xi and gradients can flow through the encoder outputs.

With this reparameterization, both the encoder and decoder can be trained using ordinary backpropagation.

The decoder gradient is:

θL(D)=i=1nEξ(i)[θlogp(x(i)z^(i);θ)] \nabla_\theta \mathcal L(\mathcal D) = \sum_{i=1}^n \mathbb E_{\xi^{(i)}} \left[ \nabla_\theta \log p \left( x^{(i)} \mid \hat z^{(i)}; \theta \right) \right]

For either encoder parameter block:

η{ϕ,ψ} \eta \in \{\phi,\psi\}

we have:

ηL(D)=i=1n{Eξ(i)[ηlogp(x(i)z^(i);θ)]ηDKL(Qi(zx(i))p(z))} \nabla_\eta \mathcal L(\mathcal D) = \sum_{i=1}^n \left\{ \mathbb E_{\xi^{(i)}} \left[ \nabla_\eta \log p \left( x^{(i)} \mid \hat z^{(i)}; \theta \right) \right] - \nabla_\eta D_{KL} \left( Q_i(z\mid x^{(i)}) \parallel p(z) \right) \right\}

These derivatives are computed by backpropagation.

We estimate each expectation using one or more Monte Carlo samples. A single latent sample per example is commonly used when training with minibatches.

See derivation
L(D)=i=1n{EzQi[logp(x(i)z;θ)]DKL(Qi(zx(i))p(z))} \mathcal L(\mathcal D) = \sum_{i=1}^n \left\{ \mathbb E_{z\sim Q_i} \left[ \log p(x^{(i)}\mid z;\theta) \right] - D_{KL} \left( Q_i(z\mid x^{(i)}) \parallel p(z) \right) \right\}

The reparameterization trick rewrites an expectation over the parameter-dependent distribution QiQ_i as an expectation over fixed standard-normal noise:

EzQi[f(z)]=EξN(0,Ik)[f(q(x(i);ϕ)+v(x(i);ψ)ξ)] \mathbb E_{z\sim Q_i} [f(z)] = \mathbb E_{ \xi\sim\mathcal N(0,I_k) } \left[ f \left( q(x^{(i)};\phi) + \sqrt{ v(x^{(i)};\psi) } \odot\xi \right) \right]

Under the usual differentiability and integrability conditions, for:

η{ϕ,ψ} \eta\in\{\phi,\psi\}

we can move the derivative inside this fixed-noise expectation:

ηEξ[f(z^(i))]=Eξ[ηf(z^(i))] \nabla_\eta \mathbb E_\xi \left[ f(\hat z^{(i)}) \right] = \mathbb E_\xi \left[ \nabla_\eta f(\hat z^{(i)}) \right]

The decoder parameters θ\theta do not affect QiQ_i or the prior, so differentiating the reconstruction term gives the decoder gradient above.

Differentiating the same reparameterized term with respect to ϕ\phi or ψ\psi, then subtracting the corresponding KL gradient, gives the encoder gradient.