Docsity

General EM algorithms, Lecture notes of Computer Science

Toggle sidebar
bg1
Chapter 11
EM algorithms
In this set of notes, we discuss the EM (Expectation-Maximization) algorithm
for density estimation.
11.1 EM for mixture of Gaussians
Suppose that we are given a training set {x(1), . . . , x(n)}as usual. Since we
are in the unsupervised learning setting, these points do not come with any
labels.
We wish to model the data by specifying a joint distribution p(x(i), z(i)) =
p(x(i)|z(i))p(z(i)). Here, z(i)∼Multinomial(φ) (where φj≥0, Pk
j=1 φj= 1,
and the parameter φjgives p(z(i)=j)), and x(i)|z(i)=j∼ N(µj,Σj). We
let kdenote the number of values that the z(i)’s can take on. Thus, our
model posits that each x(i)was generated by randomly choosing z(i)from
{1, . . . , k}, and then x(i)was drawn from one of kGaussians depending on
z(i). This is called the mixture of Gaussians model. Also, note that the
z(i)’s are latent random variables, meaning that they’re hidden/unobserved.
This is what will make our estimation problem difficult.
The parameters of our model are thus φ,µand Σ. To estimate them, we
can write down the likelihood of our data:
`(φ, µ, Σ) =
n
X
i=1
log p(x(i);φ, µ, Σ)
=
n
X
i=1
log
k
X
z(i)=1
p(x(i)|z(i);µ, Σ)p(z(i);φ).
However, if we set to zero the derivatives of this formula with respect to
150
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download General EM algorithms and more Lecture notes Computer Science in PDF only on Docsity!

Chapter 11

EM algorithms

In this set of notes, we discuss the EM (Expectation-Maximization) algorithm for density estimation.

11.1 EM for mixture of Gaussians

Suppose that we are given a training set {x(1),... , x(n)} as usual. Since we are in the unsupervised learning setting, these points do not come with any labels. We wish to model the data by specifying a joint distribution p(x(i), z(i)) = p(x(i)|z(i))p(z(i)). Here, z(i)^ ∼ Multinomial(φ) (where φj ≥ 0,

∑k j=1 φj^ = 1, and the parameter φj gives p(z(i)^ = j)), and x(i)|z(i)^ = j ∼ N (μj , Σj ). We let k denote the number of values that the z(i)’s can take on. Thus, our model posits that each x(i)^ was generated by randomly choosing z(i)^ from { 1 ,... , k}, and then x(i)^ was drawn from one of k Gaussians depending on z(i). This is called the mixture of Gaussians model. Also, note that the z(i)’s are latent random variables, meaning that they’re hidden/unobserved. This is what will make our estimation problem difficult. The parameters of our model are thus φ, μ and Σ. To estimate them, we can write down the likelihood of our data:

`(φ, μ, Σ) =

∑^ n

i=

log p(x(i); φ, μ, Σ)

∑^ n

i=

log

∑^ k

z(i)=

p(x(i)|z(i); μ, Σ)p(z(i); φ).

However, if we set to zero the derivatives of this formula with respect to

the parameters and try to solve, we’ll find that it is not possible to find the maximum likelihood estimates of the parameters in closed form. (Try this yourself at home.) The random variables z(i)^ indicate which of the k Gaussians each x(i) had come from. Note that if we knew what the z(i)’s were, the maximum likelihood problem would have been easy. Specifically, we could then write down the likelihood as

`(φ, μ, Σ) =

∑^ n

i=

log p(x(i)|z(i); μ, Σ) + log p(z(i); φ).

Maximizing this with respect to φ, μ and Σ gives the parameters:

φj =

n

∑^ n

i=

1 {z(i)^ = j},

μj =

∑n i=1 1 {z (i) (^) = j}x(i) ∑n i=1 1 {z (i) (^) = j} ,

Σj =

∑n i=1 1 {z

(i) (^) = j}(x(i) (^) − μj )(x(i) (^) − μj )T ∑n i=1 1 {z (i) (^) = j}.

Indeed, we see that if the z(i)’s were known, then maximum likelihood estimation becomes nearly identical to what we had when estimating the parameters of the Gaussian discriminant analysis model, except that here the z(i)’s playing the role of the class labels.^1 However, in our density estimation problem, the z(i)’s are not known. What can we do? The EM algorithm is an iterative algorithm that has two main steps. Applied to our problem, in the E-step, it tries to “guess” the values of the z(i)’s. In the M-step, it updates the parameters of our model based on our guesses. Since in the M-step we are pretending that the guesses in the first part were correct, the maximization becomes easy. Here’s the algorithm:

Repeat until convergence: {

(E-step) For each i, j, set

w( j i):= p(z(i)^ = j|x(i); φ, μ, Σ) (^1) There are other minor differences in the formulas here from what we’d obtained in

PS1 with Gaussian discriminant analysis, first because we’ve generalized the z(i)’s to be multinomial rather than Bernoulli, and second because here we are using a different Σj for each Gaussian.

that will allow us to easily apply it to other estimation problems in which there are also latent variables, and which will allow us to give a convergence guarantee.

11.2 Jensen’s inequality

We begin our discussion with a very useful result called Jensen’s inequality Let f be a function whose domain is the set of real numbers. Recall that f is a convex function if f ′′(x) ≥ 0 (for all x ∈ R). In the case of f taking vector-valued inputs, this is generalized to the condition that its hessian H is positive semi-definite (H ≥ 0). If f ′′(x) > 0 for all x, then we say f is strictly convex (in the vector-valued case, the corresponding statement is that H must be positive definite, written H > 0). Jensen’s inequality can then be stated as follows:

Theorem. Let f be a convex function, and let X be a random variable. Then: E[f (X)] ≥ f (EX).

Moreover, if f is strictly convex, then E[f (X)] = f (EX) holds true if and only if X = E[X] with probability 1 (i.e., if X is a constant).

Recall our convention of occasionally dropping the parentheses when writ- ing expectations, so in the theorem above, f (EX) = f (E[X]). For an interpretation of the theorem, consider the figure below.

a E[X] (^) b

f(a)

f(b) f(EX)

E[f(X)]

f

Here, f is a convex function shown by the solid line. Also, X is a random variable that has a 0.5 chance of taking the value a, and a 0.5 chance of

taking the value b (indicated on the x-axis). Thus, the expected value of X is given by the midpoint between a and b. We also see the values f (a), f (b) and f (E[X]) indicated on the y-axis. Moreover, the value E[f (X)] is now the midpoint on the y-axis between f (a) and f (b). From our example, we see that because f is convex, it must be the case that E[f (X)] ≥ f (EX). Incidentally, quite a lot of people have trouble remembering which way the inequality goes, and remembering a picture like this is a good way to quickly figure out the answer. Remark. Recall that f is [strictly] concave if and only if −f is [strictly] convex (i.e., f ′′(x) ≤ 0 or H ≤ 0). Jensen’s inequality also holds for concave functions f , but with the direction of all the inequalities reversed (E[f (X)] ≤ f (EX), etc.).

11.3 General EM algorithms

Suppose we have an estimation problem in which we have a training set {x(1),... , x(n)} consisting of n independent examples. We have a latent vari- able model p(x, z; θ) with z being the latent variable (which for simplicity is assumed to take finite number of values). The density for x can be obtained by marginalized over the latent variable z:

p(x; θ) =

z

p(x, z; θ) (11.1)

We wish to fit the parameters θ by maximizing the log-likelihood of the data, defined by

`(θ) =

∑^ n

i=

log p(x(i); θ) (11.2)

We can rewrite the objective in terms of the joint density p(x, z; θ) by

`(θ) =

∑^ n

i=

log p(x(i); θ) (11.3)

∑^ n

i=

log

z(i)

p(x(i), z(i); θ). (11.4)

But, explicitly finding the maximum likelihood estimates of the parameters θ may be hard since it will result in difficult non-convex optimization prob-

x ∈ R+. Also, the term

∑

z

Q(z)

[

p(x, z; θ) Q(z)

]

in the summation is just an expectation of the quantity [p(x, z; θ)/Q(z)] with respect to z drawn according to the distribution given by Q.^6 By Jensen’s inequality, we have

f

Ez∼Q

[

p(x, z; θ) Q(z)

])

≥ Ez∼Q

[

f

p(x, z; θ) Q(z)

)]

where the “z ∼ Q” subscripts above indicate that the expectations are with respect to z drawn from Q. This allowed us to go from Equation (11.6) to Equation (11.7). Now, for any distribution Q, the formula (11.7) gives a lower-bound on log p(x; θ). There are many possible choices for the Q’s. Which should we choose? Well, if we have some current guess θ of the parameters, it seems natural to try to make the lower-bound tight at that value of θ. I.e., we will make the inequality above hold with equality at our particular value of θ. To make the bound tight for a particular value of θ, we need for the step involving Jensen’s inequality in our derivation above to hold with equality. For this to be true, we know it is sufficient that the expectation be taken over a “constant”-valued random variable. I.e., we require that

p(x, z; θ) Q(z)

= c

for some constant c that does not depend on z. This is easily accomplished by choosing Q(z) ∝ p(x, z; θ).

Actually, since we know

z Q(z) = 1 (because it is a distribution), this further tells us that

Q(z) =

p(x, z; θ) ∑ z p(x, z;^ θ) =

p(x, z; θ) p(x; θ) = p(z|x; θ) (11.8) (^6) We note that the notion p(x,z;θ) Q(z) only makes sense if^ Q(z)^6 = 0 whenever^ p(x, z;^ θ)^6 = 0. Here we implicitly assume that we only consider those Q with such a property.

Thus, we simply set the Q’s to be the posterior distribution of the z’s given x and the setting of the parameters θ. Indeed, we can directly verify that when Q(z) = p(z|x; θ), then equa- tion (11.7) is an equality because

∑

z

Q(z) log

p(x, z; θ) Q(z)

z

p(z|x; θ) log

p(x, z; θ) p(z|x; θ)

=

z

p(z|x; θ) log

p(z|x; θ)p(x; θ) p(z|x; θ)

=

z

p(z|x; θ) log p(x; θ)

= log p(x; θ)

z

p(z|x; θ)

= log p(x; θ) (because

z p(z|x;^ θ) = 1) For convenience, we call the expression in Equation (11.7) the evidence lower bound (ELBO) and we denote it by

ELBO(x; Q, θ) =

z

Q(z) log

p(x, z; θ) Q(z)

With this equation, we can re-write equation (11.7) as

∀Q, θ, x, log p(x; θ) ≥ ELBO(x; Q, θ) (11.10)

Intuitively, the EM algorithm alternatively updates Q and θ by a) set- ting Q(z) = p(z|x; θ) following Equation (11.8) so that ELBO(x; Q, θ) = log p(x; θ) for x and the current θ, and b) maximizing ELBO(x; Q, θ) w.r.t θ while fixing the choice of Q. Recall that all the discussion above was under the assumption that we aim to optimize the log-likelihood log p(x; θ) for a single example x. It turns out that with multiple training examples, the basic idea is the same and we only need to take a sum over examples at relevant places. Next, we will build the evidence lower bound for multiple training examples and make the EM algorithm formal. Recall we have a training set {x(1),... , x(n)}. Note that the optimal choice of Q is p(z|x; θ), and it depends on the particular example x. Therefore here we will introduce n distributions Q 1 ,... , Qn, one for each example x(i). For each example x(i), we can build the evidence lower bound

log p(x(i); θ) ≥ ELBO(x(i); Qi, θ) =

z(i)

Qi(z(i)) log

p(x(i), z(i); θ) Qi(z(i))

the Qi’s. Specifically, on the iteration of EM in which the parameters had started out as θ(t), we would have chosen Q( it )(z(i)) := p(z(i)|x(i); θ(t)). We saw earlier that this choice ensures that Jensen’s inequality, as applied to get Equation (11.11), holds with equality, and hence

`(θ(t)) =

∑^ n

i=

ELBO(x(i); Q( it ), θ(t)) (11.13)

The parameters θ(t+1)^ are then obtained by maximizing the right hand side of the equation above. Thus,

`(θ(t+1)) ≥

∑^ n

i=

ELBO(x(i); Q( i t), θ(t+1))

(because ineqaulity (11.11) holds for all Q and θ)

≥

∑^ n

i=

ELBO(x(i); Q( i t), θ(t)) (see reason below)

= `(θ(t)) (by equation (11.13))

where the last inequality follows from that θ(t+1)^ is chosen explicitly to be

arg max θ

∑n

i=

ELBO(x(i); Q( it ), θ)

Hence, EM causes the likelihood to converge monotonically. In our de- scription of the EM algorithm, we said we’d run it until convergence. Given the result that we just showed, one reasonable convergence test would be to check if the increase in (θ) between successive iterations is smaller than some tolerance parameter, and to declare convergence if EM is improving(θ) too slowly.

Remark. If we define (by overloading ELBO(·))

ELBO(Q, θ) =

∑^ n

i=

ELBO(x(i); Qi, θ) =

i

z(i)

Qi(z(i)) log

p(x(i), z(i); θ) Qi(z(i)) (11.14)

then we know `(θ) ≥ ELBO(Q, θ) from our previous derivation. The EM can also be viewed an alternating maximization algorithm on ELBO(Q, θ), in which the E-step maximizes it with respect to Q (check this yourself), and the M-step maximizes it with respect to θ.

11.3.1 Other interpretation of ELBO

Let ELBO(x; Q, θ) =

z Q(z) log^

p(x,z;θ) Q(z) be defined as in equation (11.9). There are several other forms of ELBO. First, we can rewrite

ELBO(x; Q, θ) = Ez∼Q[log p(x, z; θ)] − Ez∼Q[log Q(z)] = Ez∼Q[log p(x|z; θ)] − DKL(Q‖pz ) (11.15)

where we use pz to denote the marginal distribution of z (under the distri- bution p(x, z; θ)), and DKL() denotes the KL divergence

DKL(Q‖pz ) =

z

Q(z) log

Q(z) p(z)

In many cases, the marginal distribution of z does not depend on the param- eter θ. In this case, we can see that maximizing ELBO over θ is equivalent to maximizing the first term in (11.15). This corresponds to maximizing the conditional likelihood of x conditioned on z, which is often a simpler question than the original question. Another form of ELBO(·) is (please verify yourself)

ELBO(x; Q, θ) = log p(x) − DKL(Q‖pz|x) (11.17)

where pz|x is the conditional distribution of z given x under the parameter θ. This forms shows that the maximizer of ELBO(Q, θ) over Q is obtained when Q = pz|x, which was shown in equation (11.8) before.

11.4 Mixture of Gaussians revisited

Armed with our general definition of the EM algorithm, let’s go back to our old example of fitting the parameters φ, μ and Σ in a mixture of Gaussians. For the sake of brevity, we carry out the derivations for the M-step updates only for φ and μj , and leave the updates for Σj as an exercise for the reader. The E-step is easy. Following our algorithm derivation above, we simply calculate w( ji )= Qi(z(i)^ = j) = P (z(i)^ = j|x(i); φ, μ, Σ).

Here, “Qi(z(i)^ = j)” denotes the probability of z(i)^ taking the value j under the distribution Qi.

that

∑k j=1 φj^ = 1, we construct the Lagrangian

L(φ) =

∑^ n

i=

∑^ k

j=

w( ji )log φj + β(

∑^ k

j=

φj − 1),

where β is the Lagrange multiplier.^7 Taking derivatives, we find

∂ ∂φj

L(φ) =

∑^ n

i=

w j(i) φj

  • β

Setting this to zero and solving, we get

φj =

∑n i=1 w

(i) j −β

I.e., φj ∝

∑n i=1 w

(i) j.^ Using the constraint that^

j φj^ = 1, we easily find that −β =

∑n i=

∑k j=1 w

(i) j =^

∑n i=1 1 =^ n.^ (This used the fact that^ w

(i) j = Qi(z(i)^ = j), and since probabilities sum to 1,

j w

(i) j = 1.)^ We therefore have our M-step updates for the parameters φj :

φj :=

n

∑^ n

i=

w( j i).

The derivation for the M-step updates to Σj are also entirely straightfor- ward.

11.5 Variational inference and variational

auto-encoder

Loosely speaking, variational auto-encoder Kingma and Welling [2013] gen- erally refers to a family of algorithms that extend the EM algorithms to more complex models parameterized by neural networks. It extends the technique of variational inference with the additional “re-parametrization trick” which will be introduced below. Variational auto-encoder may not give the best performance for many datasets, but it contains several central ideas about how to extend EM algorithms to high-dimensional continuous latent variables

(^7) We don’t need to worry about the constraint that φj ≥ 0, because as we’ll shortly see,

the solution we’ll find from this derivation will automatically satisfy that anyway.

with non-linear models. Understanding it will likely give you the language and backgrounds to understand various recent papers related to it. As a running example, we will consider the following parameterization of p(x, z; θ) by a neural network. Let θ be the collection of the weights of a neural network g(z; θ) that maps z ∈ Rk^ to Rd. Let

z ∼ N (0, Ik×k) (11.18) x|z ∼ N (g(z; θ), σ^2 Id×d) (11.19)

Here Ik×k denotes identity matrix of dimension k by k, and σ is a scalar that we assume to be known for simplicity. For the Gaussian mixture models in Section 11.4, the optimal choice of Q(z) = p(z|x; θ) for each fixed θ, that is the posterior distribution of z, can be analytically computed. In many more complex models such as the model (11.19), it’s intractable to compute the exact posterior distribution p(z|x; θ). Recall that from equation (11.10), ELBO is always a lower bound for any choice of Q, and therefore, we can also aim for finding an approximation of the true posterior distribution. Often, one has to use some particular form to approximate the true posterior distribution. Let Q be a family of Q’s that we are considering, and we will aim to find a Q within the family of Q that is closest to the true posterior distribution. To formalize, recall the definition of the ELBO lower bound as a function of Q and θ defined in equation (11.14)

ELBO(Q, θ) =

∑^ n

i=

ELBO(x(i); Qi, θ) =

i

z(i)

Qi(z(i)) log

p(x(i), z(i); θ) Qi(z(i))

Recall that EM can be viewed as alternating maximization of ELBO(Q, θ). Here instead, we optimize the ELBO over Q ∈ Q

max Q∈Q max θ ELBO(Q, θ) (11.20)

Now the next question is what form of Q (or what structural assumptions to make about Q) allows us to efficiently maximize the objective above. When the latent variable z are high-dimensional discrete variables, one popular as- sumption is the mean field assumption, which assumes that Qi(z) gives a distribution with independent coordinates, or in other words, Qi can be de- composed into Qi(z) = Q^1 i (z 1 ) · · · Qki (zk). There are tremendous applications of mean field assumptions to learning generative models with discrete latent variables, and we refer to Blei et al. [2017] for a survey of these models and

should be able to sample from the distribution Qi so that we can build an empirical estimator with samples. It happens that for a Gaussian distribution Qi = N (q(x(i); φ), diag(v(x(i); ψ))^2 ), we are able to do both efficiently. Now let’s optimize the ELBO. It turns out that we can run gradient ascent over φ, ψ, θ instead of alternating maximization. There is no strong need to compute the maximum over each variable at a much greater cost. (For Gaus- sian mixture model in Section 11.4, computing the maximum is analytically feasible and relatively cheap, and therefore we did alternating maximization.) Mathematically, let η be the learning rate, the gradient ascent step is

θ := θ + η∇θELBO(φ, ψ, θ) φ := φ + η∇φELBO(φ, ψ, θ) ψ := ψ + η∇ψELBO(φ, ψ, θ)

Computing the gradient over θ is simple because

∇θELBO(φ, ψ, θ) = ∇θ

∑n

i=

Ez(i)∼Qi

[

log

p(x(i), z(i); θ) Qi(z(i))

]

= ∇θ

∑n

i=

Ez(i)∼Qi

[

log p(x(i), z(i); θ)

]

∑^ n

i=

Ez(i)∼Qi

[

∇θ log p(x(i), z(i); θ)

]

But computing the gradient over φ and ψ is tricky because the sam- pling distribution Qi depends on φ and ψ. (Abstractly speaking, the is- sue we face can be simplified as the problem of computing the gradi- ent Ez∼Qφ [f (φ)] with respect to variable φ. We know that in general, ∇Ez∼Qφ [f (φ)] 6 = Ez∼Qφ [∇f (φ)] because the dependency of Qφ on φ has to be taken into account as well. ) The idea that comes to rescue is the so-called re-parameterization trick: we rewrite z(i)^ ∼ Qi = N (q(x(i); φ), diag(v(x(i); ψ))^2 ) in an equivalent way:

z(i)^ = q(x(i); φ) + v(x(i); ψ) ξ(i)^ where ξ(i)^ ∼ N (0, Ik×k) (11.24)

Here x y denotes the entry-wise product of two vectors of the same dimension. Here we used the fact that x ∼ N (μ, σ^2 ) is equivalent to that x = μ+ξσ with ξ ∼ N (0, 1). We mostly just used this fact in every dimension simultaneously for the random variable z(i)^ ∼ Qi.

With this re-parameterization, we have that

Ez(i)∼Qi

[

log

p(x(i), z(i); θ) Qi(z(i))

]

= Eξ(i)∼N (0,Ik×k )

[

log

p(x(i), q(x(i); φ) + v(x(i); ψ) ξ(i); θ) Qi(q(x(i); φ) + v(x(i); ψ) ξ(i))

]

It follows that

∇φEz(i)∼Qi

[

log

p(x(i), z(i); θ) Qi(z(i))

]

= ∇φEξ(i)∼N (0,Ik×k )

[

log

p(x(i), q(x(i); φ) + v(x(i); ψ) ξ(i); θ) Qi(q(x(i); φ) + v(x(i); ψ) ξ(i))

]

= Eξ(i)∼N (0,Ik×k )

[

∇φ log

p(x(i), q(x(i); φ) + v(x(i); ψ) ξ(i); θ) Qi(q(x(i); φ) + v(x(i); ψ) ξ(i))

]

We can now sample multiple copies of ξ(i)’s to estimate the expectation in the RHS of the equation above.^9 We can estimate the gradient with respect to ψ similarly, and with these, we can implement the gradient ascent algorithm to optimize the ELBO over φ, ψ, θ. Not many high-dimensional distributions with analytically computable density functions are known to be re-parameterizable. We refer to Kingma and Welling [2013] for a few other choices that can replace Gaussian distri- butions.

(^9) Empirically people sometimes just use one sample to estimate it for maximum com-

putational efficiency.