MathalyseResearch

Learn AI · Chapter 19 · Approximate Inference

Variational Inference and Learning

Restrict the approximate posterior to a factorial family, optimize its parameters with fixed point updates, and see how the same bound yields a Gaussian when latent variables are continuous.

Book §19.4, pages 638–650Equations 19.17–19.68About 50 minutes
Section 1

Mean field and the direction of KL

visible data latent variables guess q KL divergence
How to use this page

Click the coloured symbols. Clicking a symbol explains it and lights up the same symbol in every equation.

Move the controls before you read on. Each demo is built so that the sentence below it describes something you have seen.

Section 19.1 showed that inference can be viewed as maximizing over , and learning as maximizing it over . The core idea of variational learning is to maximize over a restricted family of distributions. The family should make the expectation easy to compute. A common choice is to assume that factorizes completely:

(19.17)
Click any coloured symbol to see what it means.

This is the mean field approach. Any graphical structure can be placed on instead, which the book calls structured variational inference. The factorial case is the simplest. The bound can be rewritten as a KL divergence, so maximizing over means minimizing in the reverse direction from the one used in maximum likelihood. The book chooses this direction for computational reasons: the bound takes its expectations under q, which we can design to be simple, while the other direction needs expectations under the true posterior.

The two directions treat errors differently. Minimizing punishes for putting mass where the posterior has little, so locks onto one peak. The other direction spreads over every peak. The demo fits a single Gaussian to a two-peaked posterior both ways and reports both divergences.

Two fits to one posterior

posterior pfit q
Challenge +8 XP

Choose the fit that minimizes , the one that locks onto a single peak.

Predict first

Which fit has the lower ? Press your answer.

Section 2

Binary sparse coding

The book applies mean field to a binary sparse coding model. Each hidden unit is on or off, with a bias that sets its prior. The visible vector is a linear combination of the active units, plus Gaussian noise with precision :

(19.19)
Click any coloured symbol to see what it means.
(19.20)
Click any coloured symbol to see what it means.

Maximum likelihood needs the derivative of with respect to a bias. The derivative then becomes a sum over every configuration of the hidden units. This expectation is taken under the true posterior, which has a complete graph over the hidden units (figure 19.2 of the book, which shows this graph), so no shortcut helps:

(19.21)
Click any coloured symbol to see what it means.
(19.22)
Click any coloured symbol to see what it means.
(19.23)
Click any coloured symbol to see what it means.
(19.24)
Click any coloured symbol to see what it means.
(19.25)
Click any coloured symbol to see what it means.
(19.26)
Click any coloured symbol to see what it means.
(19.27)
Click any coloured symbol to see what it means.

Equation 19.27 shows the derivative as an expectation under . That is the expectation that is too hard to compute, and variational inference replaces it with the factorial .

Section 3

Fixed point updates

With the factorial of equation 19.28, the bound becomes a sum of simple terms, so it can be evaluated in closed form. The mean field parameters are probabilities, , and the book keeps them away from 0 and 1 so that logarithms stay finite:

(19.28)
Click any coloured symbol to see what it means.
(19.29)
Click any coloured symbol to see what it means.
(19.30)
Click any coloured symbol to see what it means.
(19.31)
Click any coloured symbol to see what it means.
(19.32)
Click any coloured symbol to see what it means.
(19.33)
Click any coloured symbol to see what it means.
(19.34)
Click any coloured symbol to see what it means.
(19.35)
Click any coloured symbol to see what it means.
(19.36)
Click any coloured symbol to see what it means.

Equation 19.36 is the bound written only with arithmetic, so it can replace the intractable log likelihood. Gradient ascent on would work, but it stores one vector per example. Fixed point equations are faster, because each unit is set by solving in turn:

(19.37)
Click any coloured symbol to see what it means.

The derivative of 19.36 with respect to one unit gives the expressions below. The first line is the derivative of the Bernoulli part, the second the derivative of the Gaussian part:

(19.38)
Click any coloured symbol to see what it means.
(19.39)
Click any coloured symbol to see what it means.
(19.40)
Click any coloured symbol to see what it means.
(19.41)
Click any coloured symbol to see what it means.
(19.42)
Click any coloured symbol to see what it means.
(19.43)
Click any coloured symbol to see what it means.

The update follows from setting 19.43 to zero, and the book calls it a recurrent network. The input sends a fixed message to each unit, and the units send messages to each other:

(19.44)
Click any coloured symbol to see what it means.
(19.45)
Click any coloured symbol to see what it means.

In the form of 19.45 each unit sees the residual rather than the raw input. Two units whose weight vectors point in similar directions inhibit each other. Set the sliders to (0.75, -2) and the second unit falls to about 0.09 while the first stays near 0.55. This is the mean field version of explaining away, and the factorial family cannot capture both modes of the true posterior. The demo uses two unit-norm atoms 70 degrees apart, unit noise precision and zero biases.

Run the updates on two units

ĥ1ĥ2
Challenge +8 XP

Set the input so that both converged mean field parameters are above 0.6.

Challenge +8 XP

Find an input where the first parameter is above 0.55 and the second is below 0.45 after twelve sweeps. This is explaining away.

Predict first

With the default input, does the second unit switch off after one sweep and stay off? Press your answer.

Binary sparse coding does not admit block updates, so the book uses a heuristic called damping. It solves for the individually optimal value of every unit, then moves all of them a small step in that direction. This is not guaranteed to increase the bound at each step, but it works well in practice for many models. The demo above updates one unit at a time, so it does not use damping.

a mean field parameter, a probability that the unit is on the noise precision of the visible units
Section 4

Calculus of variations

When the latent variables are continuous, the family is a function, so maximizing over it needs calculus of variations. A functional takes a function as its argument, and its functional derivative is the rate of change at one point of that function. The book states the key identity:

(19.46)
Click any coloured symbol to see what it means.
(19.47)
Click any coloured symbol to see what it means.

As a worked example, consider the distribution of maximum entropy for a fixed mean and fixed variance . The entropy is defined as an expectation of the log density:

(19.48)
Click any coloured symbol to see what it means.
(19.49)
Click any coloured symbol to see what it means.

The entropy alone has no finite maximum, because it grows with the variance. The problem is made well posed by three constraints, handled with Lagrange multipliers: the density integrates to one, its mean is , and its variance is .

(19.50)
Click any coloured symbol to see what it means.
(19.51)
Click any coloured symbol to see what it means.

The functional derivative must vanish at every point, which gives an equation that determines the density:

(19.52)
Click any coloured symbol to see what it means.
(19.53)
Click any coloured symbol to see what it means.

The multipliers can be chosen so that the three constraints hold, and this gives a Gaussian. The book uses this result to justify the normal distribution when the true distribution is unknown: among all distributions with a given variance, the Gaussian imposes the least structure.

(19.54)
Click any coloured symbol to see what it means.

The demo compares three distributions with the same variance. The Gaussian always has the highest entropy. Its entropy is , measured in nats, and the other two are lower for every width.

Same variance, three shapes

Challenge +8 XP

Set σ so that the Gaussian entropy is between 1.95 and 2.05 nats.

Section 5

Continuous latent variables

With continuous latent variables the optimal can be found from the model itself. If every other factor is held fixed, the best is the normalized exponential of the expected log joint:

(19.55)
Click any coloured symbol to see what it means.
(19.56)
Click any coloured symbol to see what it means.

Equation 19.56 is a fixed point equation, and it also gives the functional form of the optimum. In most applications the form can be read off and its parameters optimized with any method. Calculus of variations is only needed to design a new family.

The book's example has two latent variables and one visible variable, with and . The true posterior does not factorize, because the cross term in its exponent couples and :

(19.57)
Click any coloured symbol to see what it means.
(19.58)
Click any coloured symbol to see what it means.
(19.59)
Click any coloured symbol to see what it means.
(19.60)
Click any coloured symbol to see what it means.
(19.61)
Click any coloured symbol to see what it means.

Equation 19.56 applied to averages out . Only two moments of are needed, its mean and its second moment :

(19.62)
Click any coloured symbol to see what it means.
(19.63)
Click any coloured symbol to see what it means.
(19.64)
Click any coloured symbol to see what it means.
(19.65)
Click any coloured symbol to see what it means.
(19.66)
Click any coloured symbol to see what it means.
(19.67)
Click any coloured symbol to see what it means.

The result is a Gaussian, so with variational parameters that can be optimized freely. Nothing about the Gaussian form was assumed. Calculus of variations produced it. The demo below uses the same model to show what the factorial family misses.

What the factorial family misses

true posteriorfactorial fit

Challenge +8 XP

Choose weights so that the true posterior covariance between h1 and h2 has absolute value of at least 0.4.

Section 6

Learning and inference interact

Approximate inference changes what the model learns. Variational learning increases the expectation over the parameters, which is equation 19.68:

(19.68)
Click any coloured symbol to see what it means.

For a fixed , this raises where is large and lowers it where is small. So the approximating assumptions become self fulfilling: a model trained with a unimodal tends to have a nearly unimodal true posterior.

The gap between the bound and can be small for the learned parameters and still hide a large loss. The gap only shows that the approximation is accurate at that . The real harm is measured against the best parameters , and the book notes that finding out is very hard: it needs a superior learning algorithm that can reach those parameters for comparison.

What to take away
  • Mean field restricts to a factorial family, and the bound is then optimized over that family only.
  • A fit that minimizes locks onto one mode, and the reverse direction spreads the fit over all modes.
  • For binary sparse coding the updates for each are a fixed point of a sigmoid, and together they form a recurrent network.
  • With continuous latent variables, equation 19.56 gives the optimal form directly, and a Gaussian posterior is the result for the example.
Final quiz

Check yourself

Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.

Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 19.4 (pages 638–650), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.