TL;DR
Variational inference finds an approximate hidden posterior for each observation by optimising a score, the ELBO, for that observation. Amortized inference changes the bookkeeping: it trains one function, usually a neural network, that takes an observation and outputs the parameters of its approximate posterior. The cost of learning the function is paid once, and every later query costs only a forward pass.
Builds on: Variational Lower Bound (ELBO), Feedforward Neural Network · Leads to: Variational Autoencoder
Where it's used
Variational autoencoders. A variational autoencoder (a kind of autoencoder) has an encoder network that reads an image and outputs a mean and a spread for its hidden code. Training fits that encoder across the whole dataset, usually with stochastic gradient descent. Afterwards, encoding a new image is one pass through the network, with no search. Kingma and Welling's 2013 paper introduced this setup, and Rezende, Mohamed and Wierstra published the same idea in 2014.
Fast perception and judgement. Cognitive scientists have proposed that people answer some inference questions quickly because they have learned to map a situation directly to a likely explanation, instead of reasoning from scratch each time. Amortized inference is a formal model of that idea. It is a hypothesis about people, not an established result, so treat it as a model.
Systems that must answer many queries. A fraud-scoring or sensor-monitoring system may need to estimate a hidden state for millions of readings a day, each within a few milliseconds. Running a full optimisation for each reading would be far too slow. A trained encoder gives an answer in one forward pass, at the price of some accuracy (see the amortization gap below).
How it works
The walkthrough uses the smallest model that shows the whole idea. A hidden random variable is drawn from a standard normal (this is its prior), and an observation is drawn from a normal centred on with variance 1. You see and want the hidden .
-
You observe . The true posterior of given this is a normal with mean and variance . You know this because the model is simple enough to solve by hand. In a real model you would not know it.
-
Per-point method. Pick a guess that is a normal with mean and variance . Compute the ELBO, adjust and to raise it, and repeat. For this loop ends at and , which is the true posterior. The plot below shows the score as a function of with fixed at . Its peak is the answer.
-
The loop must be run again for every new observation. If the next reading is , you start the loop over, and it ends at . A thousand readings mean a thousand loops.
-
Amortized method. Replace the per-point guess with a function of : , with one shared pair of numbers used for every observation. Train and so that the average ELBO over many observations is as high as possible. Training is the expensive step. It is done once.
-
Use the trained function. For the function gives . For it gives . Both match the per-point answers, and neither needed a loop.
-
Check. The score of the function's guess can be compared with the best score possible for that . If the two match, nothing was lost by sharing. If they do not, the difference is the price of amortization.
flowchart LR
A[New observation x] --> B{Method}
B -->|Per point| C[Run an optimisation loop for x]
C --> D[Approximate posterior for x]
B -->|Amortized| E[One forward pass of the trained encoder]
E --> D
- ELBO for x = 2
- log p(2)
The math
The recognition model
Let be the model, the joint distribution of and , and a distribution over the hidden whose parameters are the weights of a network that reads . This network is the recognition model, also called the encoder or inference network. Write the ELBO for one observation as
is the expected value: the average over drawn from .
Step 2 in symbols: the per-point problem
For each observation separately, solve
where is a free set of parameters for that one guess. Each gets its own optimisation problem and its own answer .
Step 3 in symbols: the amortized problem and the gap
Amortized inference solves one problem shared across the data distribution :
Once is found, the approximate posterior for any new is , computed with one forward pass.
The best possible score for a given is , the log marginal likelihood, reached when equals the true posterior. The amortization gap is
The equality uses the identity from the ELBO lesson. The gap is never negative, because it is a KL divergence. It has two parts: the error from the family of distributions the encoder can produce, and the error from one shared function being unable to fit every exactly. Only the second part is caused by amortization.
Step 4 in symbols: the Gaussian example
Take and , so . The guess is . The ELBO has a closed form:
Set the partial derivatives to zero:
- , so .
- , so .
The best guess for every is with variance . The mean is a linear function of , so a linear encoder can represent it exactly with and . In this model the amortization gap is zero. Real models rarely have this property.
For numeric work it helps to fix . The constant terms then sum to , and the ELBO becomes
Worked examples
Take , , and use . The true value is .
- Guess . The squared term is . The other term is . So .
- Gap: . The KL divergence check agrees: .
- Guess . The squared term is . The other term is . So .
- Gap: .
Interpretation: the score rises from to when the guess moves from to the true mean . The gap closes exactly when the guess is right.
Compare two encoders with the same family , , on the three observations , , and .
- Encoder A has and , so . For the guess is , and .
- For the guess is , and .
- For the guess is , and .
- Average over the three: . Every gap is zero, so the average gap is zero too.
- Encoder B has and , so . For the guess is , and . The gap is .
- For the guess is , and .
- For the guess is , and .
- Average over the three: .
Interpretation: encoder A scores higher on average, and its gap is zero for every observation. Encoder B is worse at all three points, and the training objective is exactly the average shown here. Training searches over to raise that average.
Suppose a monitoring service receives 50,000 readings per hour. A per-point optimisation needs 200 gradient steps per reading. The amortized method needs 2,000,000 gradient steps once, for training, and then one forward pass per reading. Ignore the cost of a forward pass and assume training happens at the start, when . These numbers are invented for illustration.
- Per-point gradient steps per hour: .
- Per-point total after hours: .
- Amortized total after any time : the training cost, , and no further gradient steps.
- The two totals are equal when .
Try it: solve step 4 for , in hours and in minutes. Answer: hours, which is 12 minutes. From 12 minutes on, the amortized method has done less gradient work in total, and the difference grows every hour after that.
Common mistakes and tradeoffs
It is tempting to think that amortized inference gives the exact posterior faster. In fact it gives an approximate posterior, and the amortization gap measures the extra error from sharing one function across all inputs. The gap is zero in the Gaussian example only because the true posterior mean happens to be linear in .
It is tempting to think that the encoder skips the ELBO. In fact the encoder is trained by maximising the ELBO averaged over the data. The score is the same; only the search is different.
It is tempting to think that a fast encoder is cheap overall. In fact training costs time and data, and the encoder is reliable only for inputs like those it was trained on. A reading far outside the training range can get a poor posterior with no warning.
It is tempting to think that amortization replaces per-point optimisation. In fact the two can be combined. Start from the encoder's guess, then run a few gradient steps for that one point. This shrinks the gap at a small per-point cost.
When to use it: many queries over similar inputs, a need for speed at prediction time, and a model whose posterior is hard to compute for each point.
When not to use it: a few queries, inputs that change in kind over time, or work where the last bit of accuracy matters more than speed.
Alternatives: per-point optimisation (exact per query, slower), sampling methods such as Markov chain Monte Carlo (no bound, slower), and semi-amortized inference (encoder plus a few refinement steps).
History
In 1995 the Helmholtz machine, by Dayan, Hinton, Neal and Zemel, used a recognition network trained by a "wake-sleep" procedure. It was an early version of a network that maps data to hidden causes. Its training method was less principled than the ELBO, and it did not scale well.
Later work tied the same idea to the ELBO. Kingma and Welling (2013) and, independently, Rezende, Mohamed and Wierstra (2014) trained an encoder by maximising a bound on the likelihood. They used the reparameterization trick to get low-variance gradients through the sampling step. Their work made amortized training practical for deep networks and started the variational autoencoder line of models.
Recap
- Variational inference scores a guess for the hidden posterior with the ELBO, and it normally runs an optimisation loop for each observation.
- Amortized inference replaces the loop with one shared network that reads an observation and outputs the parameters of its guess.
- The network is trained by maximising the average ELBO over the data, so the training cost is paid once.
- The amortization gap is the extra error from sharing; it is zero when the best guess is a function the encoder can represent.
- Read next: Variational Autoencoder, which uses an encoder of this kind together with the reparameterization trick.
Check yourself
QuestionWhy does amortized inference need only one optimisation, while per-point inference needs one for each observation?Show answer
Amortized inference fits a single set of parameters that define a function from observation to guess. Training adjusts so the average ELBO over the data is high. After that, a new observation is handled by evaluating the function, with no new optimisation. Per-point inference has no shared function, so each observation needs its own search.
QuestionFor the model , , and the guess , compute the ELBO when and .Show answer
Use . Here , so the first term is . Also , so the second term is . The total is . This is below , and the gap is .
QuestionWhy can an amortized encoder have a positive gap even when training has converged?Show answer
The encoder is one function shared across all observations, so it may fit some observations less well than a separate optimisation would. The distribution family may also be too narrow to contain the true posterior. Either limit leaves a gap that training cannot remove. Only the first is caused by amortization itself.
QuestionA model has a fixed encoder that is correct for training data but is queried on an observation far outside the training range. What should you expect, and what can you do?Show answer
Expect a poor approximate posterior, since the encoder was never trained on that region and its output there is not checked by the training objective. You can run a few per-point gradient steps starting from the encoder's guess, which is the semi-amortized approach, or retrain on data that covers the new range.
Sources
- Kingma and Welling, "Auto-Encoding Variational Bayes" (2013): https://arxiv.org/abs/1312.6114
- Rezende, Mohamed and Wierstra, "Stochastic Backpropagation and Approximate Inference in Deep Generative Models" (2014): https://arxiv.org/abs/1401.4082
- Blei, Kucukelbir and McAuliffe, "Variational Inference: A Review for Statisticians" (2017): https://arxiv.org/abs/1601.00670
- Evidence lower bound, Wikipedia: https://en.wikipedia.org/wiki/Evidence_lower_bound