Computing With Latent Variables
Computing With Latent Variables
Rajesh Ranganath
1
Formal definition
2
DSTN @ CMU . EDU
al Laboratory DJSCHLEGEL @ LBL . GOV
ratoryAstrophysics PRABHAT @ LBL . GOV
del of op-
h a varia-
xel inten-
able, with
properties
erties are
r distribu-
data sets.
mages. We
y survey,
he current
stial bod-
Astrophysics
chlegel, Lawrence Berkeley National Laboratory
, Lawrence Berkeley National Laboratory
DJSCHLEGEL @ LBL . GOV
PRABHAT @ LBL . GOV
Abstract
present a new, fully generative model of op-
l telescope image sets, along with a varia-
nal procedure for inference. Each pixel inten-
is treated as a Poisson random variable, with
ate parameter dependent on latent properties
stars and galaxies. Key latent properties are
mselves random, with scientific prior distribu-
ns constructed from large ancillary data sets.
check our approach on synthetic images. We
o run it on images from a major sky survey,
ere it exceeds the performance of the current
e-of-the-art method for locating celestial bod-
and measuring their colors.
oduction
Figure 1. An image from the Sloan Digital Sky Survey (SDSS,
Why a latent variable
2015)model?
of a galaxy from the constellation Serpens, 100 million
er presents Celeste, a new, fully generative model
omical image sets—the first such model to be em- light years from Earth, along with several other galaxies and many
investigated, to our knowledge. The work we stars from our own galaxy.
an encouraging example of principled statistical
e applied successfully to a science domain under-
y the machine learning community. It is unfortu- from a particular celestial body or from background at-
astronomy and cosmology receive comparatively mospheric noise—that pass through a telescope’s lens dur-
our attention: the scientific questions are funda- ing an exposure. Multiple celestial bodies may contribute
there are petabytes of data available, and we as a photons to a single image (e.g. Figure 1), and even to a
lysis community have a lot to offer the domain sci- single pixel of an image. Locating and characterizing the
One goal in reporting this work is to raise the profile imaged celestial bodies is an inference problem central to
problems for the machine-learning audience and astronomy. To date, the algorithms proposed for this in-
at much interesting research remains to be done. ference problem have been primarily heuristic, based on
finding bright regions in the images (Lupton et al., 2001;
w to the science. Stars and galaxies radiate photons. Stoughton et al., 2002).
nomical image records photons—each originating
Generative models are well-suited to this problem—for
ngs of the 32nd International Conference on Machine three reasons. First, to a good approximation, photon
Lille, France, 2015. JMLR: W&CP volume 37. Copy- counts from celestial objects are independent Poisson pro-
5 by the author(s).
cesses: each star or galaxy has an intrinsic brightness that
4
dams, Harvard University RPA @ SEAS . HARVARD . EDU
offman, Adobe Research MDHOFFMA @ CS . PRINCETON . EDU
Lang, Carnegie Mellon University DSTN @ CMU . EDU
Astrophysics
chlegel, Lawrence Berkeley National Laboratory
, Lawrence Berkeley National Laboratory
DJSCHLEGEL @ LBL . GOV
PRABHAT @ LBL . GOV
Abstract
present a new, fully generative model of op-
l telescope image sets, along with a varia-
nal procedure for inference. Each pixel inten-
is treated as a Poisson random variable, with
ate parameter dependent on latent properties
stars and galaxies. Key latent properties are
mselves random, with scientific prior distribu-
ns constructed from large ancillary data sets.
check our approach on synthetic images. We
o run it on images from a major sky survey,
ere it exceeds the performance of the current
e-of-the-art method for locating celestial bod-
and measuring their colors.
oduction
Figure 1. An image from the Sloan Digital Sky Survey (SDSS,
Prior knowledge about
2015) of a galaxy from the constellation Serpens, 100 million
er presents Celeste, a new, fully generative model
omical image sets—the first such model to be em- light years from Earth, along with several other galaxies and many
investigated, to our knowledge. The work we stars from our own galaxy.
Astrophysics
chlegel, Lawrence Berkeley National Laboratory
, Lawrence Berkeley National Laboratory
DJSCHLEGEL @ LBL . GOV
PRABHAT @ LBL . GOV
Abstract
present a new, fully generative model of op-
l telescope image sets, along with a varia-
nal procedure for inference. Each pixel inten-
is treated as a Poisson random variable, with
ate parameter dependent on latent properties
stars and galaxies. Key latent properties are
mselves random, with scientific prior distribu-
ns constructed from large ancillary data sets.
check our approach on synthetic images. We
o run it on images from a major sky survey,
ere it exceeds the performance of the current
e-of-the-art method for locating celestial bod-
and measuring their colors.
oduction
Figure 1. An image from the Sloan Digital Sky Survey (SDSS,
Prior knowledge about
2015) of a galaxy from the constellation Serpens, 100 million
er presents Celeste, a new, fully generative model
omical image sets—the first such model to be em- light years from Earth, along with several other galaxies and many
investigated, to our knowledge. The work we stars from our own galaxy.
5
Combining Simple Distributions
Categorical(1...K)
Normal(µ, σ)
Combine
zi ∼ Categorical(1...K)
xi ∼ Normal(µzi , σzi )
6
Q + A from Last Year
■ Encoding prior knowledge
■ Combining simple distributions to create a more complex one
■ Uncovering hidden structure
7
Q + A from Last Year
■ Encoding prior knowledge
■ Combining simple distributions to create a more complex one
■ Uncovering hidden structure
Do Latent Variables Help Predict x
Predictions
7
Q + A from Last Year
■ Encoding prior knowledge
■ Combining simple distributions to create a more complex one
■ Uncovering hidden structure
How do I know my latent variable is correct? Can I used cross-
validation like when we predict Y?
7
Q + A from Last Year
■ Encoding prior knowledge
■ Combining simple distributions to create a more complex one
■ Uncovering hidden structure
How can we create graphs?
7
Q + A from Last Year
■ Encoding prior knowledge
■ Combining simple distributions to create a more complex one
■ Uncovering hidden structure
Where does one use latent variables? Can we have some more
real-world examples?
7
Q + A from Last Year
■ Encoding prior knowledge
■ Combining simple distributions to create a more complex one
■ Uncovering hidden structure
Are latent variables about the noise and getting an accurate data
generating distribution?
Yes/No
■ Noise variables could be latent variables
Data
■ M number of documents
■ V number of words
■ W: M × V matrix of words
Hidden Structure
■ A group of topics that describe the documents
■ Each document contains a distribution over topics
8
Data
■ M number of documents
■ V number of words
■ W: A M × V matrix of words
How do we describe the topics?
9
Data
■ M number of documents
■ V number of words
■ W: A M × V matrix of words
How do we describe the topics?
A distribution over the words called β k
9
■ M number of documents
■ V number of words
■ W: A M × V matrix of words
s How do we describe the document’s topic composition?
A distribution over the topics called θ i
10
Have two distributions
■ β k distributions over words for each topic
■ θ i distribution over topics for each document
Priors?
11
Have two distributions
■ β k distributions over words for each topic
■ θ i distribution over topics for each document
Priors? Dirichlet distribution
11
Still need a likelihood for data
■ M number of documents
■ V number of words
■ W: A M × V matrix of words
12
For word m in document l
1. Draw word’s topic from zm,l ∼ Categorical(θ i )
2. Draw topic from for wm,l ∼ Categorical(β zm,l )
13
Topic Model
For each topic:
1. Draw distribution over words from Dirichlet(α)
14
Topic Model
For each topic:
1. Draw distribution over words from Dirichlet(α)
p(β, θ , z, w)
p(β, θ , z | w) =
p(w)
14
L ATENT D IRICHLET A LLOCATION
α θ z w N
M
15
Figure 1: Posterior topics from the hierarchical Dirichlet process topic model on two large data sets.
[Hoffman+ 2013]These posteriors were approximated using stochastic variational inference with 1.8M ar- 16
Computing the posterior. Is it easy?
17
Computing the posterior is hard
p(x, z)
p(z | x) =
p(x)
18
Computing the posterior is hard
Bayesian Mixture of Gaussians
µk ∼ Normal(0, 1)
zi ∼ Categorical(1...K)
xi ∼ Normal(µzi , 1)
Marginal likelihood is
K
Z Y N X
Y K
p(x) = p(µk ) p(zi = j)p(xi | µj )dµ1 ...dµk
k=1 i=1 j=1
Swapping
K Z
XY N
Y
p(x) = p(µk ) p(zi = k)p(xi | µk )dµk
z k=1 i:z[i]=k
19
Show the equality
K
Z Y N X
Y K
p(x) = p(µk ) p(zi = j)p(xi | µj )dµ1 ...dµk
k=1 i=1 j=1
K
Z Y N
XY
= p(µk ) p(zi = z[i])p(xi | µz[i] )dµ1 ...dµk
k=1 z i=1
K
Z XY YN
= p(µk ) p(zi = z[i])p(xi | µz[i] )dµ1 ...dµk
z k=1 i=1
XZ YK YN
= p(µk ) p(zi = z[i])p(xi | µz[i] )dµ1 ...dµk
z k=1 i=1
K Z
XY YN
= p(µk ) p(zi = k)p(xi | µk )dµk
z k=1 i:z[i]=k
R R R
Uses a,b
f (a)g(b) = a
f (a) b
g(b)
20
Variational Inference
p.z j x/
⇤
[Link] / jj p.z j x//
[Link] / ⇤
init
21
Variational Inference
p.z j x/
⇤
[Link] / jj p.z j x//
[Link] / ⇤
init
21
Variational Inference
p.z j x/
⇤
[Link] / jj p.z j x//
[Link] / ⇤
init
21
KL(q(z; λ)||p(z | x)) = Eq [log q(z; λ) − log p(z | x)]
= Eq [log q(z; λ) − log p(z, x) + log p(x)]
= Eq [log q(z; λ) − log p(z, x)] + log p(x)
Equivalently,
log p(x) = Eq [log p(z, x) − log q(z; λ)] + KL(q(z; λ)||p(z | x))
≥ Eq [log p(z, x) − log q(z; λ)]
22
The Evidence Lower Bound
23
The Evidence Lower Bound
24
The Recipe
p(x, z) Z [Link] /
(· · · )q(z; )dz r
q(z; )
25
L ATENT D IRICHLET A LLOCATION
α θ z w N
M
26
VI for LDA B LEI , N G , AND J ORDAN B LEI , N G , AND J ORDAN
A.1 Computing E[log(θi | α)] Finally, we expand Eq. (14) in terms of the model parameters (α, β) and the variational parameters
(γ, φ). Each of the five lines below expands one of the five terms in the bound:
The need to compute the expected value of the log of a single probability component under the
Dirichlet arises repeatedly in deriving the inference and parameter estimation procedures for LDA.
This value can be easily computed from the natural parameterization of the exponential family k k
p(z, x)
28
The Variational Inference Recipe
Choose a variational approximation:
q(z; λ)
28
The Variational Inference Recipe
Write down the ELBO:
28
The Variational Inference Recipe
Compute the expectation(integral):
28
The Variational Inference Recipe
Take derivatives:
1
Example: ∇λ L (λ) = 2xλ +
λ
28
The Variational Inference Recipe
Optimize:
λt+1 = λt + ρt ∇λ L
28
The Variational Inference Recipe
p(x, z) Z [Link] /
(· · · )q(z; )dz r
q(z; )
28
Example: Bayesian Logistic Regression
■ Data pairs yi , xi
■ xi are covariates
■ yi are label
■ z is the regression coefficient
■ Generative process
p(z) ∼ N(0, 1)
p(yi | xi , z) ∼ Bernoulli(σ(zxi ))
29
VI for Bayesian Logistic Regression
Assume:
■ We have one data point (y, x)
■ The approximating family q is the normal; λ = (µ, σ2 )
The ELBO is
30
VI for Bayesian Logistic Regression
L (µ, σ2 )
= Eq [log p(z) − log q(z) + log p(y | x, z)]
31
VI for Bayesian Logistic Regression
L (µ, σ2 )
= Eq [log p(z) − log q(z) + log p(y | x, z)]
1 1
= − (µ2 + σ2 ) + log σ2 + Eq [log p(y | x, z)] + C
2 2
31
VI for Bayesian Logistic Regression
L (µ, σ2 )
= Eq [log p(z) − log q(z) + log p(y | x, z)]
1 1
= − (µ2 + σ2 ) + log σ2 + Eq [log p(y | x, z)] + C
2 2
1 2 1
= − (µ + σ2 ) + log σ2 + Eq [yxz − log(1 + exp(xz))]
2 2
31
VI for Bayesian Logistic Regression
L (µ, σ2 )
= Eq [log p(z) − log q(z) + log p(y | x, z)]
1 1
= − (µ2 + σ2 ) + log σ2 + Eq [log p(y | x, z)] + C
2 2
1 2 1
= − (µ + σ2 ) + log σ2 + Eq [yxz − log(1 + exp(xz))]
2 2
1 2 1
= − (µ + σ ) + log σ2 + yxµ − Eq [log(1 + exp(xz))]
2
2 2
31
VI for Bayesian Logistic Regression
L (µ, σ2 )
= Eq [log p(z) − log q(z) + log p(y | x, z)]
1 1
= − (µ2 + σ2 ) + log σ2 + Eq [log p(y | x, z)] + C
2 2
1 2 1
= − (µ + σ2 ) + log σ2 + Eq [yxz − log(1 + exp(xz))]
2 2
1 2 1
= − (µ + σ ) + log σ2 + yxµ − Eq [log(1 + exp(xz))]
2
2 2
We are stuck.
1. We cannot analytically take that expectation.
2. The expectation hides the objectives dependence on the variational
parameters. This makes it hard to directly optimize.
31
Options?
32
Nonconjugate Models
33
Black Box Variational Inference (BBVI)
Black box variational inference
REUSABLE
REUSABLE
REUSABLE MASSIVE
VARIATIONAL
VARIATIONAL
VARIATIONAL DATA
FAMILIES
FAMILIES
FAMILIES
ANY MODEL
BLACK BOX
p.ˇ; z j x/
VARIATIONAL
INFERENCE
I Sample from q. /
I Form noisy gradients without model-specific computation
p(x, z) Z [Link] /
(· · · )q(z; )dz r
q(z; )
35
The New VI Recipe
p(x, z) Z [Link] /
r (· · · )q(z; )dz
q(z; )
36
Computing Gradients of Expectations
■ Define
■ What is ∇λ L
Z
∇λ L = ∇λ q(z; λ)g(z, λ)dz
Z
= ∇λ q(z; λ)g(z, λ) + q(z; λ)∇λ g(z, λ)dz
Z
= q(z; λ)∇λ log q(z; λ)g(z, λ) + q(z; λ)∇λ g(z, λ)dz
∇λ q
Using ∇λ log q = q
37
Roadmap
■ Reparameterization Gradients
38
Score Function Gradients of the ELBO
39
Score Function Estimator
Recall
Simplify:
Gradient: Eq(z;λ) [∇λ log q(z; λ)(log p(x, z) − log q(z; λ))]
S
1X
∇λ log q(zs ; λ)(log p(x, zs ) − log q(zs ; λ)),
S s=1
where zs ∼ q(z; λ)
41
Basic BBVI
42
The requirements for inference
43
Black Box Variational Inference
Black box variational inference
REUSABLE
REUSABLE
REUSABLE MASSIVE
VARIATIONAL
VARIATIONAL
VARIATIONAL DATA
FAMILIES
FAMILIES
FAMILIES
ANY MODEL
BLACK BOX
p.ˇ; z j x/
VARIATIONAL
INFERENCE
I Sample from q. /
I Form noisy gradients without model-specific computation
45
Problem: Basic BBVI doesn’t work
Varq(z;ν) = Eq(z;ν) [(∇ν log q(z; ν)(log p(x, z) − log q(z; ν)) − ∇ν L )2 ].
2.0
PDF
1.5 Abs Mu Score
1.0
0.5
0.0
2.0 1.5 1.0 0.5 0.0 0.5 1.0 1.5 2.0
Intuition:
Sampling rare values can lead to large scores and thus high
variance
46
Solution: Control Variates
Replace with f with f̂ where E[f̂ (z)] = E[f (z)]. General such
class:
f̂ (z) ≜ f (z) − a(h(z) − E[h(z)])
6
PDF
5 f = x + x2
fˆ; h = x2
4
fˆ; h = f
3
−1
−2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0
47
Solution: Control Variates
Replace with f with f̂ where E[f̂ (z)] = E[f (z)]. General such
class:
f̂ (z) ≜ f (z) − a(h(z) − E[h(z)])
6
PDF
5 f = x + x2
fˆ; h = x2
4
fˆ; h = f
3
−1
−2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0
6
PDF
5 f = x + x2
fˆ; h = x2
4
fˆ; h = f
3
−1
−2.0 −1.5 −1.0 −0.5 0.0 0.5 1.0 1.5 2.0
47
Nonconjugate Models
48
More Assumptions?
49
Pathwise Gradients of the ELBO
50
Reparameterization Estimator
Assume
1. z = t(ε, λ) for ε ∼ s(ε) implies z ∼ q(z; λ)
Example:
ε ∼ Normal(0, 1)
z = εσ + µ
→ z ∼ Normal(µ, σ2 )
51
Reparameterization Estimator
Recall
To differentiate:
[R+ 2018]
53
What’s an example problem that might have gradients where one
is better than the other?
54
Score Function Estimator vs. Reparameterization
Estimator
Pathwise
Score Function
■ Differentiates the function
■ Differentiates the density
∇z [log p(x, z) − log q(z; λ)]
∇λ q(z; λ)
■ Requires differentiable
■ Works for discrete and
models
continuous models
■ Requires variational
■ Works for large class of
approximation to have form
variational approximations
z = t(ε, λ)
■ Variance can be a big
■ Generally better behaved
problem
variance
55
How do we use both estimators at the same time?
56
Parameter Learning with Variational Inference
pθ (x, z)
log pθ (x) = log
pθ (z | x)
57
Parameter Learning with Variational Inference
log pθ (x) = Eq [log p(z, x) − log q(z; λ)] + KL(q(z; λ)||p(z | x))
≥ Eq [log p(z, x) − log q(z; λ)] := L (λ, θ )
58
Parameter Learning with Variational Inference
log pθ (x) = Eq [log p(z, x) − log q(z; λ)] + KL(q(z; λ)||p(z | x))
≥ Eq [log p(z, x) − log q(z; λ)] := L (λ, θ )
58
Parameter Learning with Variational Inference
log pθ (x) = Eq [log p(z, x) − log q(z; λ)] + KL(q(z; λ)||p(z | x))
≥ Eq [log p(z, x) − log q(z; λ)] := L (λ, θ )
58
Parameter Learning with Variational Inference
log pθ (x) = Eq [log p(z, x) − log q(z; λ)] + KL(q(z; λ)||p(z | x))
≥ Eq [log p(z, x) − log q(z; λ)] := L (λ, θ )
58