Search

Search papers and lessons

Amortized Inference

Level 4 · 14 min read

Amortized inference trains one network to map each observation straight to its approximate hidden posterior, so no fresh optimisation is needed for each new data point.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Neural network basics
  • Calculus
  • Linear algebra
  • Inference
  • ReadReady to readNot yet

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 zz is drawn from a standard normal (this is its prior), and an observation xx is drawn from a normal centred on zz with variance 1. You see xx and want the hidden zz.

  1. You observe x=2x = 2. The true posterior of zz given this xx is a normal with mean 11 and variance 0.50.5. You know this because the model is simple enough to solve by hand. In a real model you would not know it.

  2. Per-point method. Pick a guess q(z)q(z) that is a normal with mean mm and variance s2s^2. Compute the ELBO, adjust mm and s2s^2 to raise it, and repeat. For x=2x = 2 this loop ends at m=1m = 1 and s2=0.5s^2 = 0.5, which is the true posterior. The plot below shows the score as a function of mm with s2s^2 fixed at 0.50.5. Its peak is the answer.

  3. The loop must be run again for every new observation. If the next reading is x=−1x = -1, you start the loop over, and it ends at m=−0.5m = -0.5. A thousand readings mean a thousand loops.

  4. Amortized method. Replace the per-point guess with a function of xx: m(x)=ax+bm(x) = a x + b, with one shared pair of numbers (a,b)(a, b) used for every observation. Train aa and bb so that the average ELBO over many observations is as high as possible. Training is the expensive step. It is done once.

  5. Use the trained function. For x=2x = 2 the function gives m=0.5×2+0=1m = 0.5 \times 2 + 0 = 1. For x=−1x = -1 it gives m=−0.5m = -0.5. Both match the per-point answers, and neither needed a loop.

  6. Check. The score of the function's guess can be compared with the best score possible for that xx. 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
−0.500.511.522.5−5−4−3−2−1mean m of the guessELBO
  • ELBO for x = 2
  • log p(2)
With the variance fixed at 0.5, the score for x = 2 peaks at m = 1. The peak touches log p(2), so the guess is exact there.

The math

The recognition model

Let p(x,z)p(x, z) be the model, the joint distribution of xx and zz, and qϕ(z∣x)q_\phi(z \mid x) a distribution over the hidden zz whose parameters ϕ\phi are the weights of a network that reads xx. This network is the recognition model, also called the encoder or inference network. Write the ELBO for one observation as

L(ϕ;x)=Eqϕ(z∣x)[log⁡p(x,z)−log⁡qϕ(z∣x)].\mathcal{L}(\phi; x) = \mathbb{E}_{q_\phi(z \mid x)}\left[\log p(x, z) - \log q_\phi(z \mid x)\right].

E\mathbb{E} is the expected value: the average over zz drawn from qϕq_\phi.

Step 2 in symbols: the per-point problem

For each observation xx separately, solve

νx⋆=arg⁡max⁡ν L(ν;x),\nu_x^\star = \arg\max_{\nu} \, \mathcal{L}(\nu; x),

where ν\nu is a free set of parameters for that one guess. Each xx gets its own optimisation problem and its own answer νx⋆\nu_x^\star.

Step 3 in symbols: the amortized problem and the gap

Amortized inference solves one problem shared across the data distribution pdata(x)p_{\text{data}}(x):

ϕ⋆=arg⁡max⁡ϕ Ex∼pdata[L(ϕ;x)].\phi^\star = \arg\max_{\phi} \, \mathbb{E}_{x \sim p_{\text{data}}}\big[\mathcal{L}(\phi; x)\big].

Once ϕ⋆\phi^\star is found, the approximate posterior for any new xx is qϕ⋆(z∣x)q_{\phi^\star}(z \mid x), computed with one forward pass.

The best possible score for a given xx is log⁡p(x)\log p(x), the log marginal likelihood, reached when qq equals the true posterior. The amortization gap is

G(x)=log⁡p(x)−L(ϕ⋆;x)=DKL(qϕ⋆(z∣x) ∥ p(z∣x))≥0.G(x) = \log p(x) - \mathcal{L}\big(\phi^\star; x\big) = D_{\mathrm{KL}}\big(q_{\phi^\star}(z \mid x) \,\|\, p(z \mid x)\big) \ge 0.

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 xx exactly. Only the second part is caused by amortization.

Step 4 in symbols: the Gaussian example

Take z∼N(0,1)z \sim \mathcal{N}(0, 1) and x∣z∼N(z,1)x \mid z \sim \mathcal{N}(z, 1), so log⁡p(x)=−12ln⁡(4π)−x24\log p(x) = -\tfrac12 \ln(4\pi) - \tfrac{x^2}{4}. The guess is q(z)=N(m,s2)q(z) = \mathcal{N}(m, s^2). The ELBO has a closed form:

L(m,s2;x)=−12ln⁡(2π)−12(x−m)2−12m2−s2+12ln⁡s2+12.\mathcal{L}(m, s^2; x) = -\tfrac12 \ln(2\pi) - \tfrac12 (x - m)^2 - \tfrac12 m^2 - s^2 + \tfrac12 \ln s^2 + \tfrac12.

Set the partial derivatives to zero:

  • ∂L/∂m=(x−m)−m=0\partial \mathcal{L} / \partial m = (x - m) - m = 0, so m=x/2m = x/2.
  • ∂L/∂s2=−1+12s2=0\partial \mathcal{L} / \partial s^2 = -1 + \dfrac{1}{2s^2} = 0, so s2=12s^2 = \tfrac12.

The best guess for every xx is m(x)=x/2m(x) = x/2 with variance 12\tfrac12. The mean is a linear function of xx, so a linear encoder m(x)=ax+bm(x) = a x + b can represent it exactly with a=12a = \tfrac12 and b=0b = 0. In this model the amortization gap is zero. Real models rarely have this property.

For numeric work it helps to fix s2=12s^2 = \tfrac12. The constant terms then sum to −1.2655-1.2655, and the ELBO becomes

L(m;x)≈−1.2655−12(x−m)2−12m2.\mathcal{L}(m; x) \approx -1.2655 - \tfrac12 (x - m)^2 - \tfrac12 m^2.

Worked examples

Example
One observation, two guesses

Take x=2x = 2, s2=12s^2 = \tfrac12, and use L(m;2)=−1.2655−12(2−m)2−12m2\mathcal{L}(m; 2) = -1.2655 - \tfrac12 (2 - m)^2 - \tfrac12 m^2. The true value is log⁡p(2)=−2.2655\log p(2) = -2.2655.

  1. Guess m=0m = 0. The squared term is 12(2−0)2=2\tfrac12 (2 - 0)^2 = 2. The other term is 12⋅02=0\tfrac12 \cdot 0^2 = 0. So L=−1.2655−2−0=−3.2655\mathcal{L} = -1.2655 - 2 - 0 = -3.2655.
  2. Gap: −2.2655−(−3.2655)=1.0-2.2655 - (-3.2655) = 1.0. The KL divergence check agrees: DKL(N(0,0.5) ∥ N(1,0.5))=12[1+10.5−1]=1D_{\mathrm{KL}}(\mathcal{N}(0, 0.5) \,\|\, \mathcal{N}(1, 0.5)) = \tfrac12\big[1 + \tfrac{1}{0.5} - 1\big] = 1.
  3. Guess m=1m = 1. The squared term is 12(2−1)2=0.5\tfrac12 (2 - 1)^2 = 0.5. The other term is 12⋅12=0.5\tfrac12 \cdot 1^2 = 0.5. So L=−1.2655−0.5−0.5=−2.2655\mathcal{L} = -1.2655 - 0.5 - 0.5 = -2.2655.
  4. Gap: −2.2655−(−2.2655)=0-2.2655 - (-2.2655) = 0.

Interpretation: the score rises from −3.27-3.27 to −2.27-2.27 when the guess moves from 00 to the true mean 11. The gap closes exactly when the guess is right.

Example
A shared encoder for three observations

Compare two encoders with the same family m(x)=ax+bm(x) = a x + b, s2=12s^2 = \tfrac12, on the three observations x=−1x = -1, x=0x = 0, and x=2x = 2.

  1. Encoder A has a=0.5a = 0.5 and b=0b = 0, so m(x)=x/2m(x) = x/2. For x=−1x = -1 the guess is m=−0.5m = -0.5, and L=−1.2655−12(0.5)2−12(0.5)2=−1.5155\mathcal{L} = -1.2655 - \tfrac12(0.5)^2 - \tfrac12(0.5)^2 = -1.5155.
  2. For x=0x = 0 the guess is m=0m = 0, and L=−1.2655\mathcal{L} = -1.2655.
  3. For x=2x = 2 the guess is m=1m = 1, and L=−2.2655\mathcal{L} = -2.2655.
  4. Average over the three: (−1.5155−1.2655−2.2655)/3=−1.6822(-1.5155 - 1.2655 - 2.2655)/3 = -1.6822. Every gap is zero, so the average gap is zero too.
  5. Encoder B has a=0.3a = 0.3 and b=0.2b = 0.2, so m(x)=0.3x+0.2m(x) = 0.3x + 0.2. For x=−1x = -1 the guess is m=−0.1m = -0.1, and L=−1.2655−12(0.9)2−12(0.1)2=−1.6755\mathcal{L} = -1.2655 - \tfrac12(0.9)^2 - \tfrac12(0.1)^2 = -1.6755. The gap is −1.5155−(−1.6755)=0.16-1.5155 - (-1.6755) = 0.16.
  6. For x=0x = 0 the guess is m=0.2m = 0.2, and L=−1.2655−12(0.2)2−12(0.2)2=−1.3055\mathcal{L} = -1.2655 - \tfrac12(0.2)^2 - \tfrac12(0.2)^2 = -1.3055.
  7. For x=2x = 2 the guess is m=0.8m = 0.8, and L=−1.2655−12(1.2)2−12(0.8)2=−2.3055\mathcal{L} = -1.2655 - \tfrac12(1.2)^2 - \tfrac12(0.8)^2 = -2.3055.
  8. Average over the three: (−1.6755−1.3055−2.3055)/3=−1.7622(-1.6755 - 1.3055 - 2.3055)/3 = -1.7622.

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 (a,b)(a, b) to raise that average.

Example
Counting the work for a sensor stream

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 t=0t = 0. These numbers are invented for illustration.

  1. Per-point gradient steps per hour: 50,000×200=10,000,00050{,}000 \times 200 = 10{,}000{,}000.
  2. Per-point total after tt hours: 10,000,000 t10{,}000{,}000 \, t.
  3. Amortized total after any time tt: the training cost, 2,000,0002{,}000{,}000, and no further gradient steps.
  4. The two totals are equal when 10,000,000 t=2,000,00010{,}000{,}000 \, t = 2{,}000{,}000.

Try it: solve step 4 for tt, in hours and in minutes. Answer: t=0.2t = 0.2 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 xx.

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 ϕ\phi that define a function from observation to guess. Training adjusts ϕ\phi 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 z∼N(0,1)z \sim \mathcal{N}(0,1), x∣z∼N(z,1)x \mid z \sim \mathcal{N}(z,1), and the guess q(z)=N(m,0.5)q(z) = \mathcal{N}(m, 0.5), compute the ELBO when x=2x = 2 and m=0.5m = 0.5.Show answer

Use L=−1.2655−12(x−m)2−12m2\mathcal{L} = -1.2655 - \tfrac12(x-m)^2 - \tfrac12 m^2. Here (x−m)2=1.52=2.25(x-m)^2 = 1.5^2 = 2.25, so the first term is −1.125-1.125. Also m2=0.25m^2 = 0.25, so the second term is −0.125-0.125. The total is −1.2655−1.125−0.125=−2.5155-1.2655 - 1.125 - 0.125 = -2.5155. This is below log⁡p(2)=−2.2655\log p(2) = -2.2655, and the gap is 0.250.25.

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