Cs229 ML Notes
Cs229 ML Notes
Stanford, California
Contents
Acknowledgments viii
1 Linear Regression 3
1.1 Least mean squares (LMS) algorithm 4
1.2 The normal equations 8
1.2.1 Matrix derivatives 9
1.2.2 Least squares revisited 9
1.3 Probabilistic interpretation 11
1.4 Locally weighted linear regression 13
5 Naive Bayes 38
5.1 Laplace smoothing 41
5.2 Event models for text classification 43
6 Kernel methods 46
6.1 Feature maps 46
6.2 LMS (least mean squares) with features 47
6.3 LMS with the kernel trick 47
6.4 Properties of kernels 51
7.2 Notation 58
7.3 Functional and geometric margins 59
7.4 The optimal margin classifier 61
7.5 Lagrange duality (optional reading) 62
7.6 Optimal margin classifiers 65
7.7 Regularization and the non-separable case (optional reading) 69
7.8 The SMO algorithm (optional reading) 70
7.8.1 Coordinate ascent 71
7.9 SMO 71
9 Neural Networks 78
10 Backpropagation 87
10.1 Preliminary: chain rule 88
10.2 Backpropagation for two-layer neural networks 88
∂J
10.2.1 Computing ∂W [2]
89
∂J
10.2.2 Computing ∂W [1]
89
∂J
10.2.3 Computing ∂z 90
∂J
10.2.4 Computing ∂a 91
10.2.5 Summary for two-layer neural networks 92
10.3 Multi-layer neural networks 92
12 Cross validation 98
23 Restrictions of Σ 134
B Boosting 175
B.1 Boosting 175
B.1.1 The boosting algorithm 176
B.2 The convergence of Boosting 178
B.3 Implementing weak-learners 180
B.3.1 Decision stumps 180
B.3.2 Other strategies 181
B.4 Proof of lemma B.1 183
References 184
This work is taken from the lecture notes for the course Machine Learning at Stan-
ford University, CS 229 (cs229.stanford.edu). The contributors to the content
of this work are Andrew Ng, Christopher Ré, Moses Charikar, Tengyu Ma, Anand
Avati, Kian Katanforoosh, Yoann Le Calonnec, and John Duchi—this collection
is simply a typesetting of existing lecture notes with minor modifications. We
would like to thank the original authors for their contribution. In addition, we
wish to thank Mykel Kochenderfer and Tim Wheeler for their contribution to the
Tufte-Algorithms LATEX template, based off of Algorithms for Optimization.1 1
M. J. Kochenderfer and T. A.
Wheeler, Algorithms for Optimiza-
tion. MIT Press, 2019.
Ro b e rt J. Moss
Stanford, Calif.
May 23, 2021
600
price (in $1000)
400
200
Given data like this, how can we learn to predict the prices of other houses in
Portland, as a function of the size of their living areas?
To establish notation for future use, we’ll use x (i) to denote the ‘‘input’’ variables
(living area in this example), also called input features, and y(i) to denote the
‘‘output’’ or target variable that we are trying to predict (price). A pair ( x (i) , y(i) )
is called a training example, and the dataset that we’ll be using to learn—a list
of n training examples {( x (i) , y(i) ); i = 1, . . . , n}—is called a training set. Note
that the superscript ‘‘(i )’’ in the notation is simply an index into the training set,
and has nothing to do with exponentiation. We will also use X denote the space
of input values, and Y the space of output values. In this example, X = Y = R.
To describe the supervised learning problem slightly more formally, our goal
is, given a training set, to learn a function h : X 7→ Y so that h( x ) is a ‘‘good’’
predictor for the corresponding value of y. For historical reasons, this function h
is called a hypothesis. Seen pictorially, the process is therefore like this:
learning
algorithm
x h predicted y
(living area (predicted price
of house) of house)
When the target variable that we’re trying to predict is continuous, such as in
our housing example, we call the learning problem a regression2 problem. When 2
The term regression was originally
y can take on only a small number of discrete values (such as if, given the living coined due to ‘‘regressing’’ to the
mean (Francis Galton, 1886).
area, we wanted to predict if a dwelling is a house or an apartment, say), we call
it a classification problem.
To make our housing example more interesting, let’s consider a slightly richer
dataset in which we also know the number of bedrooms in each house:
where on the right-hand side above we are viewing θ and x both as vectors, and
here d is the number of input variables (not counting x0 ).
4 c h apter 1. line ar regression
Now, given a training set, how do we pick, or learn, the parameters θ? One
reasonable method seems to be to make h( x ) close to y, at least for the training
examples we have. To formalize this, we will define a function that measures, for
each value of the θ’s, how close the h( x (i) )’s are to the corresponding y(i) ’s. We
define the cost function:
1 n 2
J (θ ) = ∑
2 i =1
h θ ( x (i ) ) − y (i ) . (1.3)
If you’ve seen linear regression before, you may recognize this as the familiar
least-squares cost function that gives rise to the ordinary least squares regression
model. Whether or not you have seen it previously, let’s keep going, and we’ll
eventually show this to be a special case of a much broader family of algorithms.
Here, α is called the learning rate. This is a very natural algorithm that repeatedly
takes a step in the direction of steepest decrease of J.
In order to implement this algorithm, we have to work out what is the partial
derivative term on the right hand side. Let’s first work it out for the case of if
we have only one training example ( x, y), so that we can neglect the sum in the
definition of J. We have:
∂ ∂ 1
J (θ ) = ( h ( x ) − y )2
∂θ j ∂θ j 2 θ
1 ∂
= 2 · ( hθ ( x ) − y) · (h ( x ) − y)
2 ∂θ j θ
!
d
∂
∂θ j i∑
= ( hθ ( x ) − y) · θi xi − y
=0
= ( hθ ( x ) − y) x j
value of y(i) , then we find that there is little need to change the parameters; in
contrast, a larger change to the parameters will be made if our prediction hθ ( x (i) )
has a large error (i.e., if it is very far from y(i) ).
We’ve derived the LMS rule for when there was only a single training example.
There are two ways to modify this method for a training set of more than one
example. The first is replace it with the following algorithm:
for every j do
n
θ j ← θ j + α ∑ y (i ) − h θ ( x (i ) ) x j
(i )
i =1
end for
until convergence
gradient descent on the original cost function J. This method looks at every
example in the entire training set on every step, and is called batch gradient
descent. Note that, while gradient descent can be susceptible to local minima
in general, the optimization problem we have posed here for linear regression
has only one global, and no other local, optima; thus gradient descent always
converges (assuming the learning rate α is not too large) to the global minimum.
Indeed, J is a convex quadratic function.
Here is an example of gradient descent as it is run to minimize a quadratic Example 1.1. Gradient descent on
a quadratic function.
function.
40
20
−20
−40
−40 −20 0 20 40
The ellipses shown above are the contours of a quadratic function. Also
shown is the trajectory taken by gradient descent, which was initialized at
(48,30). The arrows in the figure (joined by straight lines) mark the successive
values of θ that gradient descent went through.
When we run batch gradient descent to fit θ on our previous dataset, to learn Example 1.2. Best fit line using
batch gradient descent on Portland,
to predict housing price as a function of living area. We obtain: Oregon housing prices.
θ0 = 71.27 (intercept)
θ1 = 0.1345 (slope)
housing prices
800
600
price (in $1000)
400
200
The results in example 1.2 were obtained with batch gradient descent. There is
an alternative to batch gradient descent that also works very well. Consider the
following algorithm:
i =1
end for
end for
until convergence
do this without having to write reams of algebra and pages full of matrices of
derivatives, let’s introduce some notation for doing calculus with matrices.
3
f ( A) = A + 5A212 + A21 A22 .
2 11
Here, Aij denotes the (i, j) entry of the matrix A. We then have:
" #
3
2 10A12
∇ A f ( A) =
A22 A21
Given a training set, define the design matrix X to be the n-by-d matrix (actually
n-by-(d + 1), if we include the intercept term) that contains the training examples’
input values in its rows:
— ( x (1) ) > —
— ( x (2) ) > —
X= .. (1.8)
.
— ( x (n) )> —
Also, let y be the n-dimensional vector containing all the target values from the
training set:
y (1)
(2)
y
y= ..
(1.9)
.
y(n)
Thus, using the fact that for a vector z, we have that z> z = ∑i z2i :
1 1 n 2
(Xθ − y)> (Xθ − y) = ∑ hθ ( x (i) ) − y(i)
2 2 i =1
= J (θ )
The notation ‘‘p(y(i) | x (i) ; θ )’’ indicates that this is the distribution of y(i) given
x (i) and parameterized by θ. Note that we should not condition on θ (i.e. ‘‘p(y(i) |
x (i) , θ )’’), since θ is not a random variable. We can also write the distribution of
y(i) as (y(i) | x (i) ; θ ) ∼ N (θ > x (i) , σ2 ).
Given X (the design matrix, which contains all the x (i) ’s) and θ, what is the
distribution of the y(i) ’s? The probability of the data is given by p(y | X; θ ). This
quantity is typically viewed a function of y (and perhaps X), for a fixed value of
θ. When we wish to explicitly view this as a function of θ, we will instead call it
the likelihood function:
Note that by the independence assumption on the e(i) ’s (and hence also the y(i) ’s
given the x (i) ’s), this can also be written as
n
L(θ ) = ∏ p ( y (i ) | x (i ) ; θ ) (1.16)
i =1
!
n
1 ( y (i ) − θ > x (i ) )2
=∏√ exp − . (1.17)
i =1 2πσ 2σ2
Now, given this probabilistic model relating the y(i) ’s and the x (i) ’s, what is a
reasonable way of choosing our best guess of the parameters θ? The principal
of maximum likelihood says that we should choose θ so as to make the data as
high probability as possible—i.e. we should choose θ to maximize L(θ ).
Instead of maximizing L(θ ), we can also maximize any strictly increasing
function of L(θ ). In particular, the derivations will be a bit simpler if we instead
1 n (i ) 2
2 i∑
> (i )
y − θ x ,
=1
4 4 4
y
y
2 2 2
0 0 0
0 2 4 6 0 2 4 6 0 2 4 6
x x x
2. Output θ > x.
In contrast, the locally weighted linear regression algorithm does the following:
2
1. Fit θ to minimize ∑i w(i) y(i) − θ > x (i) .
2. Output θ > x.
Here, the w(i) ’s are non-negative valued weights. Intuitively, if w(i) is large for
a particular value of i, then in picking θ we’ll try hard to make (y(i) − θ > x (i) )2
small. If w(i) is small, then the (y(i) − θ > x (i) )2 error term will be pretty much
ignored in the fit.
A fairly standard choice for the weights is:8 8
If x is vector-valued, the weights
w(i) can be generalized to
!
(i ) ( x (i ) − x )2 ( x (i ) − x ) > ( x (i ) − x )
!
w = exp − (1.18) exp −
2τ 2 2τ 2
or
Note that the weights depend on the particular point x at which we’re trying to !
( x ( i ) − x ) > Σ −1 ( x ( i ) − x )
evaluate x. Moreover, if | x (i) − x | is small, then w(i) is close to 1; and if | x (i) − x | exp −
2τ 2
is large, then w(i) is small. Hence, θ is chosen giving a much higher ‘‘weight’’ to
for appropriate choices of τ or Σ.
the (errors on) training examples close to the query point x.9 The parameter τ 9
Note also that while the formula
controls how quickly the weight of a training example falls off with distance of for the weights takes a form that
its x (i) from the query point x; τ is called the bandwidth parameter, and is also is cosmetically similar to the den-
sity of a Gaussian distribution, the
something that you’ll get to experiment with in your homework. w(i) ’s do not directly have anything
Locally weighted linear regression is the first example we’re seeing of a non- to do with Gaussians, and in partic-
parametric algorithm. The (unweighted) linear regression algorithm that we saw ular the w(i) are not random vari-
ables, normally distributed or oth-
earlier is known as a parametric learning algorithm, because it has a fixed, finite erwise.
number of parameters (the θi ’s), which are fit to the data. Once we’ve fit the θi ’s
and stored them away, we no longer need to keep the training data around to
make future predictions. In contrast, to make predictions using locally weighted
linear regression, we need to keep the entire training set around. The term ‘‘non-
parametric’’ (roughly) refers to the fact that the amount of stuff we need to keep
in order to represent the hypothesis h grows linearly with the size of the training
set.
Let’s now talk about the classification problem. This is just like the regression
problem, except that the values y we now want to predict take on only a small
number of discrete values. For now, we will focus on the binary classification
problem in which y can take on only two values, 0 and 1. (Most of what we say
here will also generalize to the multiple-class case.) For instance, if we are trying
to build a spam classifier for email, then x (i) may be some features of a piece of
email, and y may be 1 if it is a piece of spam mail, and 0 otherwise. The class 0 is
also called the negative class, and 1 the positive class, and they are sometimes
also denoted by the symbols ‘‘−’’ and ‘‘+’’. Given x (i) , the corresponding y(i) is
also called the label for the training example.
We could approach the classification problem ignoring the fact that y is discrete-
valued, and use our old linear regression algorithm to try to predict y given x.
However, it is easy to construct examples where this method performs very poorly.
Intuitively, it also doesn’t make sense for hθ ( x ) to take values larger than 1 or
smaller than 0 when we know that y ∈ {0, 1}.
To fix this, let’s change the form for our hypotheses hθ ( x ). We will choose
1
hθ ( x ) = g(θ > x ) = >x
1 + e−θ
where
1
g(z) =
1 + e−z
is called the logistic function or the sigmoid function. Here is a plot showing
g ( z ):
Notice that g(z) tends towards 1 as z → ∞, and g(z) tends towards 0 as
z → −∞. Moreover, g(z), and hence also h( x ), is always bounded between
0 and 1. As before, we are keeping the convention of letting x0 = 1, so that
θ > x = θ0 + ∑dj=1 θ j x j .
2.1. logistic regression 17
0.8
0.6
0.4
0.2
−6 −4 −2 0 2 4 6
For now, let’s take the choice of g as given. Other functions that smoothly
increase from 0 to 1 can also be used, but for a couple of reasons that we’ll see
later (when we talk about GLMs, and when we talk about generative learning
algorithms), the choice of the logistic function is a fairly natural one. Before
moving on, here’s a useful property of the derivative of the sigmoid function,
which we write as g0 :
d 1
g0 (z) = (2.1)
dz 1 + e−z
1
= (e−z ) (2.2)
(1 + e − z )2
1 1
= · 1− (2.3)
(1 + e − z ) (1 + e − z )
= g(z)(1 − g(z)) (2.4)
So, given the logistic regression model, how do we fit θ for it? Following how
we saw least squares regression could be derived as the maximum likelihood
estimator under a set of assumptions, let’s endow our classification model with
a set of probabilistic assumptions, and then fit the parameters via maximum
likelihood.
P(y = 1 | x; θ ) = hθ ( x )
P(y = 0 | x; θ ) = 1 − hθ ( x )
= (y − hθ ( x )) x j (2.14)
Above, we used the fact that g0 (z) = g(z)(1 − g(z)). This therefore gives us the
stochastic gradient ascent rule
(i )
θ j : = θ j + α y (i ) − h θ ( x (i ) ) x j (2.15)
If we compare this to the LMS update rule, we see that it looks identical; but
this is not the same algorithm, because hθ ( x (i) ) is now defined as a non-linear
function of θ > x (i) . Nonetheless, it’s a little surprising that we end up with the
same update rule for a rather different algorithm and learning problem. Is this
coincidence, or is there a deeper reason behind this? We’ll answer this when we
get to GLM models.
We now digress to talk briefly about an algorithm that’s of some historical interest,
and that we will also return to later when we talk about learning theory. Consider
modifying the logistic regression method to ‘‘force’’ it to output values that are
either 0 or 1 or exactly. To do so, it seems natural to change the definition of g to
be the threshold function:
1 if z ≥ 0
g(z) = (2.16)
0 if z < 0
If we then let hθ ( x ) = g(θ > x ) as before but using this modified definition of g,
and if we use the update rule
(i )
θ j : = θ j + α y (i ) − h θ ( x (i ) ) x j (2.17)
Returning to logistic regression with g(z) being the sigmoid function, let’s now
talk about a different algorithm for maximizing `(θ ).
To get us started, let’s consider Newton’s method for finding a zero of a function.
Specifically, suppose we have some function f : R 7→ R, and we wish to find
a value of θ so that f (θ ) = 0. Here, θ ∈ R is a real number. Newton’s method
performs the following update:
f (θ )
θ := θ − (2.18)
f 0 (θ )
60 60 60
40 40 40
f (x)
20 20 20
0 0 0
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
x x x
`0 (θ )
θ := θ − . (2.19)
`00 (θ )
(Something to think about: How would this change if we wanted to use Newton’s
method to minimize rather than maximize a function?)
Lastly, in our logistic regression setting, θ is vector-valued, so we need to gen-
eralize Newton’s method to this setting. The generalization of Newton’s method
to this multidimensional setting (also called the Newton-Raphson method) is
given by:
θ := θ − H −1 ∇θ `(θ ). (2.20)
Here, ∇θ `(θ ) is, as usual, the vector of partial derivatives of `(θ ) with respect
to the θi ’s; and H is an d-by-d matrix (actually, d + 1-by-d + 1, assuming that we
include the intercept term) called the Hessian, whose entries are given by
∂2 `(θ )
Hij = . (2.21)
∂θi ∂θ j
Here, η is called the natural parameter (also called the canonical parameter) of
the distribution; T (y) is the sufficient statistic (for the distributions we consider,
it will often be the case that T (y) = y); and a(η ) is the log partition function.
The quantity e− a(η ) essentially plays the role of a normalization constant, that
makes sure the distribution p(y; η ) sums/integrates over y to 1. MLE w.r.t. η is concave → (neg. log-
likelihood is convex)
A fixed choice of T, a and b defines a family (or set) of distributions that is
parameterized by η; as we vary η, we then get different distributions within this
family.
We now show that the Bernoulli and the Gaussian distributions are examples of
exponential family distributions. The Bernoulli distribution with mean φ, written
Bernoulli(φ), specifies a distribution over y ∈ {0, 1}, so that p(y = 1; φ) =
3.1. the exponential family 23
T (y) = y
a(η ) = − log(1 − φ)
= log(1 + eη )
b(y) = 1
This shows that the Bernoulli distribution can be written in the form of 3.1, using
an appropriate choice of T, a and b.
Let’s now move on to consider the Gaussian distribution. Recall that, when
deriving linear regression, the value of σ2 had no effect on our final choice of
θ and hθ ( x ). Thus, we can choose an arbitrary value for σ2 without changing
anything. To simplify the derivation below, let’s set σ2 = 1.1 We then have: If we leave σ2 as a variable,
1
η=µ
T (y) = y
a(η ) = µ2 /2
= η 2 /2
√
b(y) = (1/ 2π ) exp(−y2 /2).
There’re many other distributions that are members of the exponential family: The
multinomial (which we’ll see later), the Poisson (for modelling count-data; also
see the problem set); the gamma and the exponential (for modelling continuous,
non-negative random variables, such as time-intervals); the beta and the Dirichlet
(for distributions over probabilities); and many more. In the next section, we will
describe a general ‘‘recipe’’ for constructing models in which y (given x and θ)
comes from any of these distributions.
2. Given x, our goal is to predict the expected value of T (y) given x. In most
of our examples, we will have T (y) = y, so this means we would like the
prediction h( x ) output by our learned hypothesis h to satisfy h( x ) = E[y | x ].
(Note that this assumption is satisfied in the choices for hθ ( x ) for both logistic
regression and linear regression. For instance, in logistic regression, we had
hθ ( x ) = p(y = 1 | x; θ ) = 0 · p(y = 0 | x; θ ) + 1 · p(y = 1 | x; θ ) = E[y | x; θ ].)
3. The natural parameter η and the inputs x are related linearly: η = θ > x. (Or, if
η is vector-valued, then ηi = θi> x.)
The third of these assumptions might seem the least well justified of the above,
and it might be better thought of as a ‘‘design choice’’ in our recipe for designing
GLMs, rather than as an assumption per se. These three assumptions/design
choices will allow us to derive a very elegant class of learning algorithms, namely
GLMs, that have many desirable properties such as ease of learning. Furthermore,
the resulting models are often very effective for modelling different types of
distributions over y; for example, we will shortly show that both logistic regression
and ordinary least squares can both be derived as GLMs.
hθ ( x ) = E[y | x; θ ]
=µ
=η
= θ > x.
The first equality follows from Assumption 2, above; the second equality follows
from the fact that y | x; θ ∼ N (µ, σ2 ), and so its expected value is given by µ; the
third equality follows from Assumption 1 (and our earlier derivation showing that
µ = η in the formulation of the Gaussian as an exponential family distribution);
and the last equality follows from Assumption 3.
hθ ( x ) = E[y | x; θ ]
=φ
= 1/(1 + e−η )
>x
= 1/(1 + e−θ )
>
So, this gives us hypothesis functions of the form hθ ( x ) = 1/(1 + e−θ x ). If you
are previously wondering how we came up with the form of the logistic function
1/(1 + e−z ), this gives one answer: Once we assume that y conditioned on x is
Bernoulli, it arises as a consequence of the definition of GLMs and exponential
family distributions.
To introduce a little more terminology, the function g giving the distribution’s
mean as a function of the natural parameter (g(η ) = E[ T (y); η ]) is called the
canonical response function. Its inverse, g−1 , is called the canonical link func-
tion. Thus, the canonical response function for the Gaussian family is just the
identity function; and the canonical response function for the Bernoulli is the
logistic function.2 2
Many texts use g to denote the
link function, and g−1 to denote the
response function; but the notation
3.2.3 Softmax Regression we’re using here, inherited from
the early machine learning litera-
Let’s look at one more example of a GLM. Consider a classification problem in ture, will be more consistent with
the notation used in the rest of the
which the response variable y can take on any one of k values, so y ∈ {1, 2, . . . , k}.
class.
For example, rather than classifying email into the two classes spam or not-
spam—which would have been a binary classification problem— we might want
to classify it into three classes, such as spam, personal mail, and work-related mail.
The response variable is still discrete, but can now take on more than two values.
We will thus model it as distributed according to a multinomial distribution.
Let’s derive a GLM for modelling this type of multinomial data. To do so, we
will begin by expressing the multinomial as an exponential family distribution.
To parameterize a multinomial over k possible outcomes, one could use k
parameters φ1 , . . . , φk specifying the probability of each of the outcomes. However,
these parameters would be redundant, or more formally, they would not be
independent (since knowing any k − 1 of the φi ’s uniquely determines the last
one, as they must satisfy ∑ik=1 φi = 1). So, we will instead parameterize the
multinomial with only k − 1 parameters, φ1 , . . . , φk−1 , where φi = p(y = i; φ),
and p(y = k; φ) = 1 − ∑ik=−11 φi . For notational convenience, we will also let
φk = 1 − ∑ik=−11 φi , but we should keep in mind that this is not a parameter, and
that it is fully specified by φ1 , . . . , φk−1 .
To express the multinomial as an exponential family distribution, we will define
T (y) ∈ Rk−1 as follows:
1 0 0 0
0 1 0 0
0 , T (2) = 0 , · · · , T ( k − 1) = 0 , T ( k ) = 0 ,
T (1) =
.. .. .. ..
. . . .
0 0 1 0
Unlike our previous examples, here we do not have T (y) = y; also, T (y) is now
a k − 1 dimensional vector, rather than a real number. We will write ( T (y))i to
denote the i-th element of the vector T (y). We introduce one more very useful
piece of notation. An indicator function 1{·} takes on a value of 1 if its argument is
true, and 0 otherwise (1{True} = 1, 1{False} = 0). For example, 1{2 = 3} = 0,
and 1{3 = 5 − 2} = 1. So, we can also write the relationship between T (y) and
y as ( T (y))i = 1{y = i }. (Before you continue reading, please make sure you
understand why this is true!) Further, we have that E[( T (y))i ] = P(y = i ) = φi .
We are now ready to show that the multinomial is a member of the exponential
family. We have:
1{ y =1} 1{ y =2} 1{ y = k }
p(y; φ) = φ1 φ2 · · · φk
−1
1{ y =1} 1{ y =2} 1−∑ik= 1 1{ y = i }
= φ1 φ2 · · · φk
−1
( T (y))1 ( T (y))2 1−∑ik= 1 ( T ( y ))i
= φ1 φ2 · · · φk
! !
k −1
= exp ( T (y))i log(φ1 ) + ( T (y))2 log(φ2 ) + · · · + 1 − ∑ (T (y))i log(φk )
i =1
= exp (( T (y))i log(φ1 /φk ) + ( T (y))2 log(φ2 /φk ) + · · · + ( T (k))k−1 log(φk−1 /φk ) + log(φk ))
= b(y) exp(η > T (y) − a(η ))
where
log(φ1 /φk )
log(φ2 /φk )
η= .. ,
.
log(φk−1 /φk )
a(η ) = − log(φk )
b(y) = 1.
φi
ηi = log
φk
For convenience, we have also defined ηk = log(φk /φk ) = 0. To invert the link
function and derive the response function, we therefore have that
φi
e ηi = (3.6)
φk
φk eηi = φi (3.7)
k k
φk ∑ eηi = ∑ φi = 1 (3.8)
i =1 i =1
This implies that φk = 1/ ∑ik=1 eηi , which can be substituted back into equa-
tion (3.7) to give the response function
e ηi
φi =
∑kj=1 e
ηj
This function mapping from the η’s to the φ’s is called the softmax function.
To complete our model, we use Assumption 3, given earlier, that the ηi ’s
are linearly related to the x’s. So, have ηi = θi> x (for i = 1, . . . , k − 1), where
θ1 , . . . , θk−1 ∈ Rd+1 are the parameters of our model. For notational convenience,
we can also define θk = 0, so that ηk = θk> x = 0, as given previously. Hence, our
model assumes that the conditional distribution of y given x is given by:
p(y = 1 | x; θ ) = φi (3.9)
e ηi
= (3.10)
∑kj=1 e
ηj
>x
e θi
= (3.11)
θ j> x
∑kj=1 e
This model, which applies to classification problems where y ∈ {1, ..., k}, is called
softmax regression. It is a generalization of logistic regression.
hθ ( x ) = E [ T (y) | x; θ ] (3.12)
1{ y = 1}
1{ y = 2}
= E
.. | x; θ (3.13)
.
1{ y = k − 1}
φ1
φ2
= ..
(3.14)
.
φk−1
exp(θ1> x )
k
∑ j=1 exp(θ j> x )
exp(θ2> x )
k
∑ j=1 exp(θ j> x )
= (3.15)
..
.
exp(θk>−1 x )
∑kj=1 exp(θ j> x )
In other words, our hypothesis will output the estimated probability that p(y =
i | x; θ ), for every value of i = 1, . . . , k. (Even though hθ ( x ) as defined above is
only k − 1 dimensional, clearly p(y = k | x; θ ) can be obtained as 1 − ∑ik=−11 φi .)
Lastly, let’s discuss parameter fitting. Similar to our original derivation of
ordinary least squares and logistic regression, if we have a training set of n
examples {( x (i) , y(i) ); i = 1, . . . , n} and would like to learn the parameters θi of
this model, we would begin by writing down the log-likelihood
n
`(θ ) = ∑ log p(y(i) | x(i) ; θ ) (3.16)
i =1
1{ y (i ) = l }
n k > x (i )
eθl
= ∑ log ∏ θ > x (i )
(3.17)
i =1 l =1 ∑kj=1 e j
To obtain the second line above, we used the definition for p(y | x; θ ) given in
3.11. We can now obtain the maximum likelihood estimate of the parameters
by maximizing `(θ ) in terms of θ, using a method such as gradient ascent or
Newton’s method.
p( x | y) p(y)
p(y | x ) = (3.18)
p( x )
The first generative learning algorithm that we’ll look at is Gaussian discrim-
inant analysis (GDA). In this model, we’ll assume that p( x | y) is distributed
according to a multivariate normal distribution. Let’s talk briefly about the prop-
erties of multivariate normal distributions before moving on to the GDA model
itself.
The multivariate normal distribution in d-dimensions, also called the multi-
variate Gaussian distribution, is parameterized by a mean vector µ ∈ Rd and a
covariance matrix Σ ∈ Rd×d , where Σ ≥ 0 is symmetric and positive semi-definite.
Also written ‘‘N (µ, Σ)’’, its density is given by:
1 1 > −1
p( x; µ, Σ) = exp − ( x − µ) Σ ( x − µ) (4.1)
(2π )d/2 |Σ|1/2 2
Cov( X ) = Σ. (4.3)
33
Here are some examples of what the density of a Gaussian distribution looks
like:
The left-most figure shows a Gaussian with mean zero (that is, the 2 × 1 zero-
vector) and covariance matrix Σ = I (the 2 × 2 identity matrix). A Gaussian with
zero mean and identity covariance is also called the standard normal distribution.
The middle figure shows the density of a Gaussian with zero mean and Σ = 0.6I;
and in the rightmost figure shows one with, Σ = 2I. We see that as Σ becomes
larger, the Gaussian becomes more ‘‘spread-out,’’ and as it becomes smaller, the
distribution becomes more ‘‘compressed.’’
Let’s look at some more examples.
The figures above show Gaussians with mean 0, and with covariance matrices
respectively:
" # " # " #
1 0 1 0.5 1 0.8
Σ= ; Σ= ; Σ= . (4.4)
0 1 0.5 1 0.8 1
The leftmost figure shows the familiar standard normal distribution, and we
see that as we increase the off-diagonal entry in Σ, the density becomes more
‘‘compressed’’ towards the 45◦ line (given by x1 = x2 ). We can see this more
clearly when we look at the contours of the same three densities:
−2
−4
−4 −2 0 2 4 −4 −2 0 2 4 −4 −2 0 2 4
−2
−4
−4 −2 0 2 4 −4 −2 0 2 4 −4 −2 0 2 4
From the leftmost and middle figures, we see that by decreasing the off- diagonal
elements of the covariance matrix, the density now becomes ‘‘compressed’’ again,
but in the opposite direction. Lastly, as we vary the parameters, more generally
the contours will form ellipses (the rightmost figure showing an example).
As our last set of examples, fixing Σ = I, by varying µ, we can also move the
mean of the density around.
The figures above were generated using Σ = I, and respectively
" # " # " #
1 −0.5 −1
µ= ; µ= ; µ= . (4.6)
0 0 −1.5
When we have a classification problem in which the input features x are continuous-
valued random variables, we can then use the Gaussian Discriminant Analysis
(GDA) model, which models p( x | y) using a multivariate normal distribution.
The model is:
y ∼ Bernoulli(φ) (4.7)
x | y = 0 ∼ N ( µ0 , Σ ) (4.8)
x | y = 1 ∼ N ( µ1 , Σ ) (4.9)
p ( y ) = φ y (1 − φ )1− y (4.10)
1 1
p ( x | y = 0) = exp − ( x − µ0 )> Σ−1 ( x − µ0 ) (4.11)
(2π )d/2 |Σ|1/2 2
1 1
p ( x | y = 1) = exp − ( x − µ1 )> Σ−1 ( x − µ1 ) (4.12)
(2π )d/2 |Σ|1/2 2
Here, the parameters of our model are φ, Σ, µ0 and µ1 . (Note that while there’re
two different mean vectors µ0 and µ1 , this model is usually applied using only
one covariance matrix Σ.) The log-likelihood of the data is given by
n
`(φ, µ0 , µ1 , Σ) = log ∏ p( x (i) , y(i) ; φ, µo , µ1 , Σ) (4.13)
i =1
n
= log ∏ p( x (i) | y(i) ; φ, µo , µ1 , Σ) p(y(i) ; φ). (4.14)
i =1
1
p(y = 1 | x; φ, Σ, µ0 , µ1 ) = , (4.19)
1 + exp(−θ > x )
In GDA, the feature vectors x were continuous, real-valued vectors. Let’s now
talk about a different learning algorithm in which the x j ’s are discrete-valued.
For our motivating example, consider building an email spam filter using
machine learning. Here, we wish to classify messages according to whether they
are unsolicited commercial (spam) email, or non-spam email. After learning
to do this, we can then have our mail reader automatically filter out the spam
messages and perhaps place them in a separate mail folder. Classifying emails is
one example of a broader set of problems called text classification.
Let’s say we have a training set (a set of emails labeled as spam or non- spam).
We’ll begin our construction of our spam filter by specifying the features x j used
to represent an email.
We will represent an email via a feature vector whose length is equal to the
number of words in the dictionary. Specifically, if an email contains the j-th word
of the dictionary, then we will set x j = 1; otherwise, we let x j = 0. For instance,
the vector
1 a
0 aardvark
0 aardwolf
.. ..
x= .
.
1
buy
. ..
.
. .
0 zygmurgy
is used to represent an email that contains the words ‘‘a’’ and ‘‘buy,’’ but not
‘‘aardvark,’’ ‘‘aardwolf’’ or ‘‘zygmurgy.’’1 The set of words encoded into the 1
Actually, rather than looking
feature vector is called the vocabulary, so the dimension of x is equal to the size through an English dictionary
for the list of all English words,
of the vocabulary. in practice it is more common to
Having chosen our feature vector, we now want to build a generative model. look through our training set and
encode in our feature vector only
So, we have to model p( x | y). But if we have, say, a vocabulary of 50000 words, the words that occur at least once
then x ∈ {0, 1}50000 (x is a 50000-dimensional vector of 0’s and 1’s), and if we there. Apart from reducing the
were to model x explicitly with a multinomial distribution over the 250000 possible number of words modeled and
hence reducing our computational
and space requirements, this also
has the advantage of allowing
us to model/include as a feature
many words that may appear
in your email (such as ‘‘cs229’’)
but that you won’t find in a
dictionary. Sometimes (as in the
39
p( x1 , . . . , x50000 | y) (5.1)
= p( x1 | y) p( x2 | y, x1 ) p( x3 | y, x1 , x2 ) · · · p( x50000 | y, x1 , . . . , x49999 ) (5.2)
= p( x1 | y) p( x2 | y) p( x3 | y) · · · p( x50000 | y) (5.3)
d
= ∏ p( x j | y) (5.4)
j =1
The first equality simply follows from the usual properties of probabilities, and
the second equality used the NB assumption. We note that even though the Naive
Bayes assumption is an extremely strong assumptions, the resulting algorithm
works well on many problems.
Our model is parameterized by φj|y=1 = p( x j = 1 | y = 1), φj|y=0 = p( x j = 1 |
y = 0), and φy = p(y = 1). As usual, given a training set {( x (i) , y(i) ); i = 1, . . . , n},
we can write down the joint likelihood of the data:
n
L(φy , φj|y=0 , φj|y=1 ) = ∏ p( x (i) , y(i) ) (5.5)
i =1
Maximizing this with respect to φy , φj|y=0 and φj|y=1 gives the maximum likeli-
hood estimates:
(i )
∑in=1 1{ x j = 1 ∧ y(i) = 1}
φ j | y =1 = (5.6)
∑in=1 1{y(i) = 1}
(i )
∑in=1 1{ x j = 1 ∧ y(i) = 0}
φ j | y =0 = (5.7)
∑in=1 1{y(i) = 0}
∑in=1 1{y(i) = 1}
φy = (5.8)
n
In the equations above, the ‘‘∧’’ symbol means ‘‘and.’’ The parameters have a
very natural interpretation. For instance, φj|y=1 is just the fraction of the spam
(y = 1) emails in which word j does appear.
Having fit all these parameters, to make a prediction on a new example with
features x, we then simply calculate
p ( x | y = 1) p ( y = 1)
p(y = 1 | x ) = (5.9)
p( x )
∏dj=1 p( x j | y = 1) p(y = 1)
= ,
∏dj=1 p( x j | y = 1) p(y = 1) + ∏dj=1 p( x j | y = 0) p(y = 0)
(5.10)
Thus, for a house with living area 890 square feet, we would set the value of the
corresponding feature x j to 3. We can then apply the Naive Bayes algorithm, and
model p( x j | y) with a multinomial distribution, as described previously. When
the original, continuous-valued attributes are not well- modeled by a multivariate
normal distribution, discretizing the features and using Naive Bayes (instead of
GDA) will often result in a better classifier.
The Naive Bayes algorithm as we have described it will work fairly well for many
problems, but there is a simple change that makes it work much better, especially
for text classification. Let’s briefly discuss a problem with the algorithm in its
current form, and then talk about how we can fix it.
Consider spam/email classification, and let’s suppose that, we are in the year
of 20xx, after completing CS229 and having done excellent work on the project,
you decide around May 20xx to submit work you did to the NeurIPS conference
for publication.2 Because you end up discussing the conference in your emails, 2
NeurIPS is one of the top machine
you also start getting messages with the word ‘‘neurips’’ in it. But this is your learning conferences. The deadline
for submitting a paper is typically
first NeurIPS paper, and until this time, you had not previously seen any emails in May-June.
containing the word ‘‘neurips’’; in particular ‘‘neurips’’ did not ever appear in
your training set of spam/non-spam emails. Assuming that ‘‘neurips’’ was the
35000th word in the dictionary, your Naive Bayes spam filter therefore had picked
its maximum likelihood estimates of the parameters φ35000|y to be
(i )
∑in=1 1{ x35000 = 1 ∧ y(i) = 1}
φ35000|y=1 = =0 (5.11)
∑in=1 1{y(i) = 1}
(i )
∑in=1 1{ x35000 = 1 ∧ y(i) = 0}
φ35000|y=0 = = 0, (5.12)
∑in=1 1{y(i) = 0}
i.e., because it has never seen ‘‘neurips’’ before in either spam or non-spam
training examples, it thinks the probability of seeing it in either type of email is
zero. Hence, when trying to decide if one of these messages containing ‘‘neurips’’
∏dj=1 p( x j | y = 1) p(y = 1)
p(y = 1 | x ) =
∏dj=1 p( x j | y = 1) p(y = 1) + ∏dj=1 p( x j | y = 0) p(y = 0)
(5.13)
0
= (5.14)
0
This is because each of the terms ‘‘∏dj=1 p( x j | y)’’ includes a term p( x35000 | y) = 0
that is multiplied into it. Hence, our algorithm obtains 0/0, and doesn’t know
how to make a prediction.
Stating the problem more broadly, it is statistically a bad idea to estimate the
probability of some event to be zero just because you haven’t seen it before in your
finite training set. Take the problem of estimating the mean of a multinomial ran-
dom variable z taking values in {1, . . . , k}. We can parameterize our multinomial
with φj = p(z = j). Given a set of n independent observations {z(1) , . . . , z(n) },
the maximum likelihood estimates are given by
∑in=1 1{z(i) = j}
φj = . (5.15)
n
As we saw previously, if we were to use these maximum likelihood estimates,
then some of the φj ’s might end up as zero, which was a problem. To avoid this,
we can use Laplace smoothing, which replaces the above estimate with
1 + ∑in=1 1{z(i) = j}
φj = . (5.16)
k+n
Here, we’ve added 1 to the numerator, and k to the denominator. Note that
∑kj=1 φj = 1 still holds (check this yourself!), which is a desirable property since
the φj ’s are estimates for probabilities that we know must sum to 1. Also, φj 6= 0
for all values of j, solving our problem of probabilities being estimated as zero.
Under certain (arguably quite strong) conditions, it can be shown that the Laplace
smoothing actually gives the optimal estimator of the φj ’s.
(In practice, it usually doesn’t matter much whether we apply Laplace smoothing
to φy or not, since we will typically have a fair fraction each of spam and non-spam
messages, so φy will be a reasonable estimate of p(y = 1) and will be quite far
from 0 anyway.)
To close off our discussion of generative learning algorithms, let’s talk about one
more model that is specifically for text classification. While Naive Bayes as we’ve
presented it will work well for many classification problems, for text classification,
there is a related model that does even better.
In the specific context of text classification, Naive Bayes as presented uses the
what’s called the Bernoulli event model (or sometimes multi-variate Bernoulli
event model). In this model, we assumed that the way an email is generated is
that first it is randomly determined (according to the class priors p(y)) whether
a spammer or non-spammer will send you your next message. Then, the person
sending the email runs through the dictionary, deciding whether to include each
word j in that email independently and according to the probabilities p( x j = 1 |
y) = φj|y . Thus, the probability of a message was given by p(y) ∏dj=1 p( x j | y)
Here’s a different model, called the Multinomial event model. To describe
this model, we will use a different notation and set of features for representing
emails. We let x j denote the identity of the j-th word in the email. Thus, x j is now
an integer taking values in {1, . . . , |V |}, where |V | is the size of our vocabulary
(dictionary). An email of d words is now represented by a vector ( x1 , x2 , . . . , xd )
of length d; note that d can vary for different documents. For instance, if an email
starts with ‘‘A NeurIPS …,’’ then x1 = 1 (‘‘a’’ is the first word in the dictionary),
and x2 = 35000 (if ‘‘neurips’’ is the 35000th word in the dictionary).
In the multinomial event model, we assume that the way an email is generated
is via a random process in which spam/non-spam is first determined (according
to p(y)) as before. Then, the sender of the email writes the email by first generating
x1 from some multinomial distribution over words (p( x1 | y)). Next, the second
word x2 is chosen independently of x1 but from the same multinomial distribution,
and similarly for x3 , x4 , and so on, until all d words of the email have been
generated. Thus, the overall probability of a message is given by p(y) ∏dj=1 p( x j |
y). Note that this formula looks like the one we had earlier for the probability of a
message under the Bernoulli event model, but that the terms in the formula now
mean very different things. In particular x j | y is now a multinomial, rather than
a Bernoulli distribution.
The parameters for our new model are φy = p(y) as before, φk|y=1 = p( x j =
k | y = 1) (for any j) and φk|y=0 = p( x j = k | y = 0). Note that we have assumed
that p( x j | y) is the same for all values of j (i.e., that the distribution according to
which a word is generated does not depend on its position j within the email).
(i ) (i ) (i )
If we are given a training set {( x (i) , y(i) ); i = 1, . . . , n} where x (i) = ( x1 , x2 , . . . , xd )
i
(here, di is the number of words in the i-training example), the likelihood of the
data is given by
n
L(φy , φk|y=0 , φk|y=1 ) = ∏ p( x (i) , y(i) ) (5.19)
i =1
!
n di
=∏ ∏
(i )
p( x j | y; φk|y=0 , φk|y=1 ) p(y(i) ; φy ). (5.20)
i =1 j =1
d (i )
∑in=1 ∑ j=i 1 1{ x j = k ∧ y(i) = 1}
φk|y=1 = (5.21)
∑in=1 1{y(i) = 1}di
d (i )
∑in=1 ∑ j=i 1 1{ x j = k ∧ y(i) = 0}
φk|y=0 = (5.22)
∑in=1 1{y(i) = 0}di
∑in=1 1{y(i) = 1}
φy = . (5.23)
n
If we were to apply Laplace smoothing (which is needed in practice for good
performance) when estimating φk|y=0 and φk|y=1 , we add 1 to the numerators and
While not necessarily the very best classification algorithm, the Naive Bayes
classifier often works surprisingly well. It is often also a very good ‘‘first thing to
try,’’ given its simplicity and ease of implementation.
Recall that in our discussion about linear regression, we considered the problem
of predicting the price of a house (denoted by y) from the living area of the house
(denoted by x), and we fit a linear function of x to the training data. What if the
price y can be more accurately represented as a non-linear function of x? In this
case, we need a more expressive family of models than linear models.
We start by considering fitting cubic functions y = θ3 x3 + θ2 x2 + θ1 x + θ0 . It
turns out that we can view the cubic function as a linear function over a different
set of feature variables (defined below). Concretely, let the function φ : R 7→ R4
be defined as
1
x
φ( x ) = 2 ∈ R4 . (6.1)
x
x3
θ3 x 3 + θ2 x 2 + θ1 x + θ0 = θ > φ ( x )
Thus, a cubic function of the variable x can be viewed as a linear function over the
variables φ( x ). To distinguish between these two sets of variables, in the context
of kernel methods, we will call the ‘‘original’’ input value the input attributes of
a problem (in this case, x, the living area). When the original input is mapped to
some new set of quantities φ( x ), we will call those new quantities the features
variables. (Unfortunately, different authors use different terms to describe these
two things in different contexts.) We will call φ a feature map, which maps the
attributes to the features.
6.2. lms (least mean squares) with features 47
We will derive the gradient descent algorithm for fitting the model θ > φ( x ). First
recall that for ordinary least square problem where we were to fit θ > x, the batch
gradient descent update is (see the first lecture note for its derivation):
n
θ : = θ + α ∑ y (i ) − h θ ( x (i ) ) x (i ) (6.2)
i =1
n
: = θ + α ∑ y (i ) − θ > x (i ) x (i ) . (6.3)
i =1
The gradient descent update, or stochastic gradient update above becomes compu-
tationally expensive when the features φ( x ) is high-dimensional. For example, con-
sider the direct extension of the feature map in equation 6.1 to high-dimensional
input x: suppose x ∈ Rd , and let φ( x ) be the vector that contains all the monomials
of x with degree ≤ 3
1
x1
x2
.
..
2
x1
x1 x2
x x
φ( x ) = 1 3 (6.6)
.
..
x2 x1
..
.
x3
1
2
x1 x2
..
.
for some β 1 , . . . , β n ∈ R. Then we claim that in the next round, θ is still a linear
combination of φ( x (1) ), . . . , φ( x (n) ) because
n
θ : = θ + α ∑ y (i ) − θ > φ ( x (i ) ) φ ( x (i ) ) (6.8)
i =1
n n
= ∑ i
β φ ( x (i )
) + α ∑ y (i )
− θ >
φ ( x (i )
) φ ( x (i ) ) (6.9)
i =1 i =1
n
= ∑ β i + α y (i ) − θ > φ ( x (i ) ) φ ( x (i ) ) (6.10)
i =1 | {z }
new β i
You may realize that our general strategy is to implicitly represent the p-dimensional
vector θ by a set of coefficients β 1 , . . . , β n . Towards doing this, we derive the up-
date rule of the coefficients β 1 , . . . , β n . Using the equation above, we see that the
new β i depends on the old one via:
β i : = β i + α y (i ) − θ > φ ( x (i ) ) (6.11)
Here we still have the old θ on the RHS of the equation. Replacing θ by θ =
∑nj=1 β j φ( x ( j) ) gives:
!
n
∀i ∈ {1, . . . , n}, β i := β i + α y(i) − ∑ β j φ( x ( j) )> φ( x (i) )
j =1
We often rewrite φ( x ( j) )> φ( x (i) ) as hφ( x ( j) ), φ( x (i) )i to emphasize that it’s the
inner product of the two feature vectors. Viewing β i ’s as the new representation
of θ, we have successfully translated the batch gradient descent algorithm into
an algorithm that updates the value of β iteratively. It may appear that at every
iteration, we still need to compute the values of hφ( x ( j) ), φ( x (i) )i for all pairs of
i, j, each of which may take roughly O( p) operation. However, two important
properties come to rescue:
1. We can pre-compute the pairwise inner products hφ( x ( j) ), φ( x (i) )i for all pairs
of i, j before the loop starts.
2. For the feature map φ defined in 6.6 (or many other interesting feature maps),
computing hφ( x ( j) ), φ( x (i) )i can be efficient and does not necessarily require
computing φ( x (i) ) explicitly. This is because:
d
hφ( x ), φ(z)i = 1 + ∑ xi zi + ∑ xi x j zi z j + ∑ xi x j x k zi z j z k
i =1 i,j∈{1,...,d} i,j,k ∈{1,...,d}
(6.12)
!2 !3
d d d
= 1 + ∑ xi zi + ∑ xi zi + ∑ xi zi (6.13)
i =1 i =1 i =1
= 1 + h x, zi + h x, zi + h x, zi3
2
(6.14)
Therefore, to compute hφ( x ), φ(z)i, we can first compute h x, zi with O(d) time
and then take another constant number of operations to compute 1 + h x, zi +
h x, zi2 + h x, zi3 .
As you will see, the inner products between the features hφ( x ), φ(z)i are essen-
tial here. We define the Kernel corresponding to the feature map φ as a function
that maps X × X 7→ R satisfying:2 2
Recall that X is the space of the
input x. In our running example,
K ( x, z) , hφ( x ), φ(z)i (6.15) X = Rd
To wrap up the discussion, we write the down the final algorithm as follows:
1. Compute all the values K ( x (i) , x ( j) ) , hφ( x (i) ), φ( x ( j) )i using equation 6.14 for
all i, j ∈ {1, . . . , n}. Set β := 0.
2. Loop:
!
n
∀i ∈ {1, . . . , n}, β i := β i + α y(i) − ∑ β j K ( x (i) , x ( j) ) (6.16)
j =1
With the algorithm above, we can update the representation β of the vector θ
efficiently with O(n2 ) time per update. Finally, we need to show that the knowl-
edge of the representation β suffices to compute the prediction θ > φ( x ). Indeed,
we have:
n n
θ > φ( x ) = ∑ β i φ ( x (i ) ) > φ ( x ) = ∑ β i K ( x (i ) , x ) (6.17)
i =1 i =1
You may realize that fundamentally all we need to know about the feature map
φ(·) is encapsulated in the corresponding kernel function K (·, ·). We will expand
on this in the next section.
In the last subsection, we started with an explicitly defined feature map φ, which
induces the kernel function K ( x, z) , hφ( x ), φ(z)i. Then we saw that the kernel
function is so intrinsic so that as long as the kernel function is defined, the whole
training algorithm can be written entirely in the language of the kernel without
referring to the feature map φ, so can the prediction of a test example x (equation
6.17.)
Therefore, it would be tempting to define other kernel functions K (·, ·) and
run the algorithm 6.16. Note that the algorithm 6.16 does not need to explicitly
access the feature map φ, and therefore we only need to ensure the existence of
the feature map φ, but do not necessarily need to be able to explicitly write φ
down.
What kinds of functions K (·, ·) can correspond to some feature map φ? In other
words, can we tell if there is some feature mapping φ so that K ( x, z) = φ( x )> φ(z)
for all x, z?
If we can answer this question by giving a precise characterization of valid
kernel functions, then we can completely change the interface of selecting feature
maps φ to the interface of selecting kernel function K. Concretely, we can pick a
function K, verify that it satisfies the characterization (so that there exists a feature
map φ that K corresponds to), and then we can run update rule 6.16. The benefit
here is that we don’t have to be able to compute φ or write it down analytically,
and we only need to know its existence. We will answer this question at the end
of this subsection after we go through several concrete examples of kernels.
Suppose x, z ∈ Rd , and let’s first consider the function K (·, ·) defined as:
K ( x, z) = ( x > z)2
Thus, we see that K ( x, z) = hφ( x ), φ(z)i is the kernel function that corresponds
to the the feature mapping φ given (shown here for the case of d = 3) by
x1 x1
x1 x2
x1 x3
x x
2 1
φ ( x ) = x2 x2 .
x2 x3
x x
3 1
x3 x2
x3 x3
K ( x, z) = ( x > z + c)2
d d √ √
= ∑ ( xi x j )(zi z j ) + ∑ 2cxi 2czi + c2 .
i,j=1 i =1
and the parameter c controls the relative weighting between the xi (first order)
and the xi x j (second order) terms.
More broadly, the kernel K ( x, z) = ( x > z + c)k corresponds to a feature map-
ping to an (d+k k) feature space, corresponding of all monomials of the form
xi1 xi2 · · · xik that are up to order k. However, despite working in this O(dk )-
dimensional space, computing K ( x, z) still takes only O(d) time, and hence we
never need to explicitly represent feature vectors in this very high dimensional
feature space.
Kernels as similarity metrics. Now, let’s talk about a slightly different view
of kernels. Intuitively, (and there are things wrong with this intuition, but nev-
ermind), if φ( x ) and φ(z) are close together, then we might expect K ( x, z) =
φ( x )> φ(z) to be large. Conversely, if φ( x ) and φ(z) are far apart— say nearly
orthogonal to each other—then K ( x, z) = φ( x )> φ(z) will be small. So, we can
think of K ( x, z) as some measurement of how similar are φ( x ) and φ(z), or of
how similar are x and z.
Given this intuition, suppose that for some learning problem that you’re work-
ing on, you’ve come up with some function K ( x, z) that you think might be a
reasonable measure of how similar x and z are. For instance, perhaps you chose
k x − z k2
K ( x, z) = exp − .
2σ2
This is a reasonable measure of x and z’s similarity, and is close to 1 when x and z
are close, and near 0 when x and z are far apart. Does there exist a feature map
φ such that the kernel K defined above satisfies K ( x, z) = φ( x )> φ(z)? In this
particular example, the answer is yes. This kernel is called the Gaussian kernel,
and corresponds to an infinite dimensional feature mapping φ. We will give a
precise characterization about what properties a function K needs to satisfy so
that it can be a valid kernel function that corresponds to some feature map φ.
Necessary conditions for valid kernels. Suppose for now that K is indeed a
valid kernel corresponding to some feature mapping φ, and we will first see what
properties it satisfies. Now, consider some finite set of n points (not necessarily
the training set) { x (1) , . . . , x (n) }, and let a square, n-by-n matrix K be defined
so that its (i, j)-entry is given by Kij = K ( x (i) , x ( j) ). This matrix is called the
kernel matrix. Note that we’ve overloaded the notation and used K to denote
both the kernel function K ( x, z) and the kernel matrix K, due to their obvious
close relationship.
Now, if K is a valid kernel, then Kij = K ( x (i) , x ( j) ) = φ( x (i) )> φ( x ( j) ) =
φ( x ( j) )> φ( x (i) ) = K ( x ( j) , x (i) ) = K ji , and hence K must be symmetric. More-
over, letting φk ( x ) denote the k-th coordinate of the vector φ( x ), we find that for
any vector z, we have
z> Kz = ∑ ∑ zi Kij z j
i j
= ∑ ∑ z i φ ( x (i ) ) > φ ( x ( j ) ) z j
i j
= ∑ ∑ zi ∑ φk ( x (i) )φk ( x ( j) )z j
i j k
= ∑ ∑ ∑ zi φk ( x (i) )φk ( x ( j) )z j
k i j
!2
=∑ ∑ zi φk (x (i )
)
k i
≥ 0.
The second-to-last step uses the fact that ∑i,j ai a j = (∑i ai )2 for ai = zi φk ( x (i) ).
Since z was arbitrary, this shows that K is positive semi-definite (K ≥ 0).
Hence, we’ve shown that if K is a valid kernel (i.e., if it corresponds to some
feature mapping φ), then the corresponding kernel matrix K ∈ Rn×n is symmetric
positive semidefinite.
Sufficient conditions for valid kernels. More generally, the condition above
turns out to be not only a necessary, but also a sufficient, condition for K to
be a valid kernel (also called a Mercer kernel). The following result is due to
Mercer.3 3
Many texts present Mercer’s theo-
rem in a slightly more complicated
form involving L2 functions, but
Theorem (Mercer). Let K : Rd × Rd 7→ R be given. Then for K to be a valid when the input attributes take val-
(Mercer) kernel, it is necessary and sufficient that for any { x (1) , . . . , x (n) }, (n < ues in Rd , the version given here is
equivalent.
∞), the corresponding kernel matrix is symmetric positive semi-definite.
Given a function K, apart from trying to find a feature mapping φ that corre-
sponds to it, this theorem therefore gives another way of testing if it is a valid
kernel. You’ll also have a chance to play with these ideas more in problem set 2.
In class, we also briefly talked about a couple of other examples of kernels.
For instance, consider the digit recognition problem, in which given an image
(16 × 16 pixels) of a handwritten digit (0-9), we have to figure out which digit it
was. Using either a simple polynomial kernel K ( x, z) = ( x > z)k or the Gaussian
kernel, support vector machines (SVMs) were able to obtain extremely good
performance on this problem. This was particularly surprising since the input
attributes x were just 256-dimensional vectors of the image pixel intensity values,
and the system had no prior knowledge about vision, or even about which pixels
are adjacent to which other ones. Another example that we briefly talked about
in lecture was that if the objects x that we are trying to classify are strings (say, x
is a list of amino acids, which strung together form a protein), then it seems hard
to construct a reasonable, ‘‘small’’ set of features for most learning algorithms,
especially if different strings have different lengths. However, consider letting
φ( x ) be a feature vector that counts the number of occurrences of each length-k
substring in x. If we’re considering strings of English letters, then there are 26 k
such strings. Hence, φ( x ) is a 26k -dimensional vector; even for moderate values
of k, this is probably too big for us to efficiently work with. (e.g., 264 ≈ 460000.)
However, using (dynamic programming-ish) string matching algorithms, it is
This set of notes presents the Support Vector Machine (SVM) learning al- gorithm.
SVMs are among the best (and many believe are indeed the best) ‘‘off-the-shelf’’
supervised learning algorithms. To tell the SVM story, we’ll need to first talk
about margins and the idea of separating data with a large ‘‘gap.’’ Next, we’ll
talk about the optimal margin classifier, which will lead us into a digression
on Lagrange duality. We’ll also see kernels, which give a way to apply SVMs
efficiently in very high dimensional (such as infinite-dimensional) feature spaces,
and finally, we’ll close off the story with the SMO algorithm, which gives an
efficient implementation of SVMs.
We’ll start our story on SVMs by talking about margins. This section will give the
intuitions about margins and about the ‘‘confidence’’ of our predictions; these
ideas will be made formal in Section 7.3.
Consider logistic regression, where the probability p(y = 1 | x; θ ) is modeled
by hθ ( x ) = g(θ > x ). We then predict ‘‘1’’ on an input x if and only if hθ ( x ) ≥ 0.5,
or equivalently, if and only if θ > x ≥ 0. Consider a positive training example
(y = 1). The larger θ > x is, the larger also is hθ ( x ) = p(y = 1 | x; θ ), and thus also
the higher our degree of ‘‘confidence’’ that the label is 1. Thus, informally we can
think of our prediction as being very confident that y = 1 if θ > x 0. Similarly,
we think of logistic regression as confidently predicting y = 0, if θ > x 0. Given
a training set, again informally it seems that we’d have found a good fit to the
training data if we can find θ so that θ > x (i) 0 whenever y(i) = 1, and θ > x (i) 0
whenever y(i) = 0, since this would reflect a very confident (and correct) set of
classifications for all the training examples. This seems to be a nice goal to aim
for, and we’ll soon formalize this idea using the notion of functional margins.
58 c hapter 7. support vector machines
For a different type of intuition, consider the following figure, in which x’s
represent positive training examples, o’s denote negative training examples, a
decision boundary (this is the line given by the equation θ > x = 0, and is also
called the separating hyperplane) is also shown, and three points have also been
labeled A, B and C.
Notice that the point A is very far from the decision boundary. If we are asked
to make a prediction for the value of y at A, it seems we should be quite confident
that y = 1 there. Conversely, the point C is very close to the decision boundary,
and while it’s on the side of the decision boundary on which we would predict
y = 1, it seems likely that just a small change to the decision boundary could
easily have caused out prediction to be y = 0. Hence, we’re much more confident
about our prediction at A than at C. The point B lies in-between these two cases,
and more broadly, we see that if a point is far from the separating hyperplane,
then we may be significantly more confident in our predictions. Again, informally
we think it would be nice if, given a training set, we manage to find a decision
boundary that allows us to make all correct and confident (meaning far from the
decision boundary) predictions on the training examples. We’ll formalize this
later using the notion of geometric margins.
7.2 Notation
To make our discussion of SVMs easier, we’ll first need to introduce a new no-
tation for talking about classification. We will be considering a linear classifier
for a binary classification problem with labels y and features x. From now, we’ll
use y ∈ {−1, 1} (instead of {0, 1}) to denote the class labels. Also, rather than
parameterizing our linear classifier with the vector θ, we will use parameters w, b,
and write our classifier as
Here, g(z) = 1 if z ≥ 0, and g(z) = −1 otherwise. This ‘‘w, b’’ notation allows us
to explicitly treat the intercept term b separately from the other parameters. (We
also drop the convention we had previously of letting x0 = 1 be an extra coordinate
in the input feature vector.) Thus, b takes the role of what was previously θ0 , and
w takes the role of [θ1 . . . θd ]> .
Note also that, from our definition of g above, our classifier will directly predict
either 1 or −1 (cf. the perceptron algorithm), without first going through the
intermediate step of estimating p(y = 1) (which is what logistic regression does).
Let’s formalize the notions of the functional and geometric margins. Given a train-
ing example ( x (i) , y(i) ), we define the functional margin of (w, b) with respect to
the training example as
γ̂(i) = y(i) (w> x (i) + b).
Note that if y(i) = 1, then for the functional margin to be large (i.e., for our
prediction to be confident and correct), we need w> x (i) + b to be a large positive
number. Conversely, if y(i) = −1, then for the functional margin to be large, we
need w> x (i) + b to be a large negative number. Moreover, if y(i) (w> x (i) + b) > 0,
then our prediction on this example is correct. (Check this yourself.) Hence, a
large functional margin represents a confident and a correct prediction.
For a linear classifier with the choice of g given above (taking values in {−1, 1}),
there’s one property of the functional margin that makes it not a very good
measure of confidence, however. Given our choice of g, we note that if we replace
w with 2w and b with 2b, then since g(w> x + b) = g(2w> x + 2b), this would not
change hw,b ( x ) at all. I.e., g, and hence also hw,b ( x ), depends only on the sign,
but not on the magnitude, of w> x + b. However, replacing (w, b) with (2w, 2b)
also results in multiplying our functional margin by a factor of 2. Thus, it seems
that by exploiting our freedom to scale w and b, we can make the functional
margin arbitrarily large without really changing anything meaningful. Intuitively,
it might therefore make sense to impose some sort of normalization condition
such as that kwk2 = 1; i.e., we might replace (w, b) with (w/kwk2 , b/kwk2 ), and
instead consider the functional margin of (w/kwk2 , b/kwk2 ). We’ll come back to
this later.
Given a training set S = {( x (i) , y(i) ); i = 1, . . . , n}, we also define the function
margin of (w, b) with respect to S as the smallest of the functional margins of the
individual training examples. Denoted by γ̂, this can therefore be written:
γ̂ = min γ̂(i)
i =1,...,n
Next, let’s talk about geometric margins. Consider the picture below:
The decision boundary corresponding to (w, b) is shown, along with the vector
w. Note that w is orthogonal (at 90◦ ) to the separating hyperplane. (You should
convince yourself that this must be the case.) Consider the point at A, which
represents the input x (i) of some training example with label y(i) = 1. Its distance
to the decision boundary, γ(i) , is given by the line segment AB.
How can we find the value of γ(i) ? Well, w/kwk is a unit-length vector pointing
in the same direction as w. Since A represents x (i) , we therefore find that the point
B is given by x (i) − γ(i) · w/kwk. But this point lies on the decision boundary, and
all points x on the decision boundary satisfy the equation w> x + b = 0. Hence,
w
w > x (i ) − γ (i ) + b = 0.
kwk
Solving for γ(i) yields
>
w > x (i ) + b
(i ) w b
γ = = x (i ) + .
kwk kwk kwk
This was worked out for the case of a positive training example at A in the figure,
where being on the ‘‘positive’’ side of the decision boundary is good. More
generally, we define the geometric margin of (w, b) with respect to a training
example ( x (i) , y(i) ) to be
w > (i )
!
(i ) (i ) b
γ =y x + .
kwk kwk
Note that if kwk = 1, then the functional margin equals the geometric margin—
this thus gives us a way of relating these two different notions of margin. Also,
the geometric margin is invariant to rescaling of the parameters; i.e., if we replace
w with 2w and b with 2b, then the geometric margin does not change. This will
in fact come in handy later. Specifically, because of this invariance to the scaling
of the parameters, when trying to fit w and b to training data, we can impose
an arbitrary scaling constraint on w without changing anything important; for
instance, we can demand that kwk = 1, or |w1 | = 5, or |w1 + b| + |w2 | = 2, and
any of these can be satisfied simply by rescaling w and b.
Finally, given a training set S = {( x (i) , y(i) ); i = 1, . . . , n}, we also define the
geometric margin of (w, b) with respect to S to be the smallest of the geometric
margins on the individual training examples:
γ = min γ(i) .
i =1,...,n
Given a training set, it seems from our previous discussion that a natural desider-
atum is to try to find a decision boundary that maximizes the (geometric) margin,
since this would reflect a very confident set of predictions on the training set and
a good ‘‘fit’’ to the training data. Specifically, this will result in a classifier that
separates the positive and the negative training examples with a ‘‘gap’’ (geometric
margin).
For now, we will assume that we are given a training set that is linearly sep-
arable; i.e., that it is possible to separate the positive and negative examples
using some separating hyperplane. How will we find the one that achieves the
maximum geometric margin? We can pose the following optimizationproblem:
max γ
γ,w,b
Here, we’re going to maximize γ̂/kwk, subject to the functional margins all being
at least γ̂. Since the geometric and functional margins are related by γ = γ̂/kwk,
this will give us the answer we want. Moreover, we’ve gotten rid of the constraint
kwk = 1 that we didn’t like. The downside is that we now have a nasty (again,
γ̂
non-convex) objective kw k
function; and, we still don’t have any off-the-shelf
software that can solve this form of an optimization problem.
Let’s keep going. Recall our earlier discussion that we can add an arbitrary
scaling constraint on w and b without changing anything. This is the key idea
we’ll use now. We will introduce the scaling constraint that the functional margin
of w, b with respect to the training set must be 1:
γ̂ = 1
Since multiplying w and b by some constant results in the functional margin being
multiplied by that same constant, this is indeed a scaling constraint, and can be
satisfied by rescaling w, b. Plugging this into our problem above, and noting that
maximizing γ̂/kwk = 1/kwk is the same thing as minimizing kwk2 , we now
have the following optimization problem:
1
min k w k2
w,b 2
s. t. y(i) (w> x (i) + b) ≥ 1, i = 1, . . . , n
We’ve now transformed the problem into a form that can be efficiently solved.
The above is an optimization problem with a convex quadratic objective and
only linear constraints. Its solution gives us the optimal margin classifier. This
optimization problem can be solved using commercial quadratic programming
(QP) code.1 1
You may be familiar with lin-
While we could call the problem solved here, what we will instead do is make ear programming, which solves
optimization problems that have
a digression to talk about Lagrange duality. This will lead us to our optimization linear objectives and linear con-
problem’s dual form, which will play a key role in allowing us to use kernels to straints. QP software is also widely
available, which allows convex
get optimal margin classifiers to work efficiently in very high dimensional spaces. quadratic objectives and linear con-
The dual form will also allow us to derive an efficient algorithm for solving the straints.
above optimization problem that will typically do much better than generic QP
software.
Let’s temporarily put aside SVMs and maximum margin classifiers, and talk about
solving constrained optimization problems. Consider a problem of the following
form:
min f (w)
w
s. t. hi (w) = 0, i = 1, . . . , l.
Some of you may recall how the method of Lagrange multipliers can be used to
solve it. (Don’t worry if you haven’t seen it before.) In this method, we define the
Lagrangian to be
l
L(w, β) = f (w) + ∑ β i hi (w)
i =1
Here, the β i ’s are called the Lagrange multipliers. We would then find and set
L’s partial derivatives to zero:
∂L ∂L
= 0; = 0,
∂wi ∂β i
and solve for w and β.
In this section, we will generalize this to constrained optimization problems
in which we may have inequality as well as equality constraints. Due to time
constraints, we won’t really be able to do the theory of Lagrange duality justice in
this class,2 but we will give the main ideas and results, which we will then apply 2
Readers interested in learning
to our optimal margin classifier’s optimization problem. more about this topic are encour-
aged to read, e.g., R. T. Rockarfeller
Consider the following, which we’ll call the primal optimization problem: (1970), Convex Analysis, Princeton
University Press.
min f (w)
w
s. t. gi (w) ≤ 0, i = 1, . . . , k
hi (w) = 0, i = 1, . . . , l.
Here, the αi ’s and β i ’s are the Lagrange multipliers. Consider the quantity
Here, the ‘‘P ’’ subscript stands for ‘‘primal.’’ Let some w be given. If w violates
any of the primal constraints (i.e., if either gi (w) > 0 or hi (w) 6= 0 for some i),
then you should be able to verify that
k l
θP (w) = max f (w) + ∑ αi gi (w) + ∑ β i hi (w)
α,β:αi ≥0 i =1 i =1
= ∞.
Conversely, if the constraints are indeed satisfied for a particular value of w, then
θP (w) = f (w). Hence,
f (w) if w satisfies primal constraints
θP (w) =
∞ otherwise.
Thus, θP takes the same value as the objective in our problem for all values of w
that satisfies the primal constraints, and is positive infinity if the constraints are
violated. Hence, if we consider the minimization problem
we see that it is the same problem (i.e., and has the same solutions as) our original,
primal problem. For later use, we also define the optimal value of the objective to
be p∗ = minw θP (w); we call this the value of the primal problem.
Now, let’s look at a slightly different problem. We define
Here, the ‘‘D ’’ subscript stands for ‘‘dual.’’ Note also that whereas in the defi-
nition of θP we were optimizing (maximizing) with respect to α, β, here we are
minimizing with respect to w.
We can now pose the dual optimization problem:
This is exactly the same as our primal problem shown above, except that the order
of the ‘‘max’’ and the ‘‘min’’ are now exchanged. We also define the optimal value
of the dual problem’s objective to be d∗ = maxα,β:αi ≥0 θD (w).
How are the primal and the dual problems related? It can easily be shown that
(You should convince yourself of this; this follows from the ‘‘max min’’ of a
function always being less than or equal to the ‘‘min max.’’) However, under
certain conditions, we will have
d∗ = p∗ ,
so that we can solve the dual problem in lieu of the primal problem. Let’s see
what these conditions are.
Suppose f and the gi ’s are convex,3 and the hi ’s are affine.4 Suppose further 3
When f has a Hessian, then it is
that the constraints gi are (strictly) feasible; this means that there exists some w convex if and only if the Hessian is
positive semi-definite. For instance,
so that gi (w) < 0 for all i. f (w) = w> w is convex; similarly,
Under our above assumptions, there must exist w∗ , α∗ , β∗ so that w∗ is the all linear (and affine) functions are
also convex. (A function f can also
solution to the primal problem, α∗ ,β∗ are the solution to the dual problem, and be convex without being differen-
moreover p∗ = d∗ = L(w∗ , α∗ , β∗ ). Moreover, w∗ ,α∗ and β∗ satisfy the Karush- tiable, but we won’t need those
Kuhn-Tucker (KKT) conditions, which are as follows: more general definitions of convex-
ity here.)
4
∂ I.e., there exists ai , bi , so that
L(w∗ , α∗ , β∗ ) = 0, i = 1, . . . , d (7.1) hi (w) = ai> w + bi . ‘‘Affine’’ means
∂wi the same thing as linear, except that
∂ we also allow the extra intercept
L(w∗ , α∗ , β∗ ) = 0, i = 1, . . . , l (7.2) term bi .
∂β i
αi∗ gi (w∗ ) = 0, i = 1, . . . , k (7.3)
∗
gi (w ) ≤ 0, i = 1, . . . , k (7.4)
∗
α ≥ 0, i = 1, . . . , k (7.5)
Note: The equivalence of optimization problem 7.6 and the optimization problem 7.11,
and the relationship between the primary and dual variables in equation 7.8 are the most
important take home messages of this section.
1
min k w k2 (7.6)
w,b 2
s. t. y(i) (w> x (i) + b) ≥ 1, i = 1, . . . , n (7.7)
We have one such constraint for each training example. Note that from the KKT
dual complementarity condition, we will have αi > 0 only for the training exam-
ples that have functional margin exactly equal to one (i.e., the ones corresponding
to constraints that hold with equality, gi (w) = 0). Consider the figure below, in
which a maximum margin separating hyperplane is shown by the solid line.
The points with the smallest margins are exactly the ones closest to the deci-
sion boundary; here, these are the three points (one negative and two positive
examples) that lie on the dashed lines parallel to the decision boundary. Thus,
only three of the αi ’s—namely, the ones corresponding to these three training
examples—will be non-zero at the optimal solution to our optimization problem.
These three points are called the support vectors in this problem. The fact that
the number of support vectors can be much smaller than the size the training set
will be useful later.
Let’s move on. Looking ahead, as we develop the dual form of the problem,
one key idea to watch out for is that we’ll try to write our algorithm in terms of
only the inner product h x (i) , x ( j) i (think of this as ( x (i) )> x ( j) ) between points in
the input feature space. The fact that we can express our algorithm in terms of
these inner products will be key when we apply the kernel trick.
When we construct the Lagrangian for our optimization problem we have:
n
1 h i
L(w, b, α) = k w k2 − ∑ α i y (i ) ( w > x (i ) + b ) − 1 .
2 i =1
Note that there’re only ‘‘αi ’’ but no ‘‘β i ’’ Lagrange multipliers, since the problem
has only inequality constraints.
Let’s find the dual form of the problem. To do so, we need to first minimize
L(w, b, α) with respect to w and b (for fixed α), to get θD , which we’ll do by setting
the derivatives of L with respect to w and b to zero. We have:
n
∇w L(w, b, α) = w − ∑ αi y(i) x (i) = 0
i =1
If we take the definition of w in Equation (7.8) and plug that back into the
Lagrangian (Section 7.6), and simplify, we get
n n n
1
L(w, b, α) = ∑ αi − 2 ∑ y (i ) y ( j ) α i α j ( x (i ) ) > x ( j ) − b ∑ α i y (i ) . (7.10)
i =1 i,j=1 i =1
But from Equation (7.9), the last term must be zero, so we obtain
n n
1
L(w, b, α) = ∑ αi − 2 ∑ y (i ) y ( j ) α i α j ( x (i ) ) > x ( j ) .
i =1 i,j=1
Recall that we got to the equation above by minimizing L with respect to w and
b. Putting this together with the constraints αi ≥ 0 (that we always had) and
the constraint from equation (7.9), we obtain the following dual optimization
problem:
n
1 n (i ) ( j )
max W (α) =
α
∑ αi − 2 i,j∑
y y α i α j h x (i ) , x ( j ) i. (7.11)
i =1 =1
s. t. αi ≥ 0, i = 1, . . . , n (7.12)
n
∑ αi y(i) = 0. (7.13)
i =1
You should also be able to verify that the conditions required for p∗ = d∗ and
the KKT conditions (Equations (7.1) to (7.5)) to hold are indeed satisfied in our
optimization problem. Hence, we can solve the dual in lieu of solving the primal
problem. Specifically, in the dual problem above, we have a maximization problem
in which the parameters are the αi ’s. We’ll talk later about the specific algorithm
that we’re going to use to solve the dual problem, but if we are indeed able to
solve it (i.e., find the α’s that maximize W (α) subject to the constraints), then we
can use Equation (7.8) to go back and find the optimal w’s as a function of the α’s.
Having found w∗ , by considering the primal problem, it is also straightforward
to find the optimal value for the intercept term b as
Hence, if we’ve found the αi ’s, in order to make a prediction, we have to calculate
a quantity that depends only on the inner product between x and the points in
the training set. Moreover, we saw earlier that the αi ’s will all be zero except for
the support vectors. Thus, many of the terms in the sum above will be zero, and
we really need to find only the inner products between x and the support vectors
(of which there is often only a small number) in order calculate equation (7.16)
and make our prediction.
By examining the dual form of the optimization problem, we gained significant
insight into the structure of the problem, and were also able to write the entire
algorithm in terms of only inner products between input feature vectors. In the
next section, we will exploit this property to apply the kernels to our classifica-
tion problem. The resulting algorithm, support vector machines, will be able to
efficiently learn in very high dimensional spaces.
The derivation of the SVM as presented so far assumed that the data is linearly
separable. While mapping data to a high dimensional feature space via φ does
generally increase the likelihood that the data is separable, we can’t guarantee
that it always will be so. Also, in some cases it is not clear that finding a separating
hyperplane is exactly what we’d want to do, since that might be susceptible to
outliers. For instance, the left figure below shows an optimal margin classifier,
and when a single outlier is added in the upper-left region (right figure), it causes
the decision boundary to make a dramatic swing, and the resulting classifier has
a much smaller margin.
To make the algorithm work for non-linearly separable datasets as well as be less
sensitive to outliers, we reformulate our optimization (using `1 regularization)
as follows:
n
1
min k w k2 + C ∑ ξ i
γ,w,b 2 i =1
s. t. y (i ) ( w > x (i ) + b ) ≥ 1 − ξ i , i = 1, . . . , n
ξ i ≥ 0, i = 1, . . . , n.
Thus, examples are now permitted to have (functional) margin less than 1, and
if an example has functional margin 1 − ξ i (with ξ > 0), we would pay a cost
of the objective function being increased by Cξ i . The parameter C controls the
relative weighting between the twin goals of making the kwk2 small (which we
saw earlier makes the margin large) and of ensuring that most examples have
functional margin at least 1.
As before, we can form the Lagrangian:
n n n
1 > h i
L(w, b, ξ, α, r ) = w w + C ∑ ξ i − ∑ α i y (i ) ( x > w + b ) − 1 + ξ i − ∑ r i ξ i .
2 i =1 i =1 i =1
s. t. 0 ≤ αi ≤ C, i = 1, . . . , n
n
∑ αi y(i) = 0.
i =1
Now, all that remains is to give an algorithm for actually solving the dual
problem, which we will do in the next section.
The SMO (sequential minimal optimization) algorithm, due to John Platt, gives
an efficient way of solving the dual problem arising from the derivation of the
SVM. Partly to motivate the SMO algorithm, and partly because it’s interesting in
its own right, let’s first take another digression to talk about the coordinate ascent
algorithm.
max W (α1 , α2 , . . . , αn ).
α
Here, we think of W as just some function of the parameters αi ’s, and for now
ignore any relationship between this problem and SVMs. We’ve already seen
two optimization algorithms, gradient ascent and Newton’s method. The new
algorithm we’re going to consider here is called coordinate ascent:
for i = 1, . . . , n do
αi := arg maxα̂i W (α1 , . . . , αi−1 , α̂i , αi+1 , . . . , αn ).
end for
until convergence
Thus, in the innermost loop of this algorithm, we will hold all the variables
except for some αi fixed, and reoptimize W with respect to just the parameter
αi . In the version of this method presented here, the inner-loop reoptimizes the
variables in order α1 , α2 , . . . , αn , α1 , α2 , . . . (A more sophisticated version might
choose other orderings; for instance, we may choose the next variable to update
according to which one we expect to allow us to make the largest increase in
W (α).)
When the function W happens to be of such a form that the ‘‘arg max’’ in the
inner loop can be performed efficiently, then coordinate ascent can be a fairly
efficient algorithm. Here’s a picture of coordinate ascent in action:
The ellipses in the figure are the contours of a quadratic function that we want
to optimize. Coordinate ascent was initialized at (2, −2), and also plotted in the
figure is the path that it took on its way to the global maximum. Notice that on
each step, coordinate ascent takes a step that’s parallel to one of the axes, since
only one variable is being optimized at a time.
7.9 SMO
We close off the discussion of SVMs by sketching the derivation of the SMO
algorithm.
s. t. 0 ≤ αi ≤ C, i = 1, . . . , n (7.21)
n
∑ αi y(i) = 0. (7.22)
i =1
Let’s say we have set of αi ’s that satisfy the constraints in equations (7.21)
and (7.22). Now, suppose we want to hold α2 , . . . , αn fixed, and take a coordinate
ascent step and reoptimize the objective with respect to α1 . Can we make any
progress? The answer is no, because the constraint 7.22 ensures that
n
α 1 y (1) = − ∑ α i y ( i ) .
i =2
(This step used the fact that y(1) ∈ {−1, 1}, and hence (y(1) )2 = 1.) Hence, α1
is exactly determined by the other αi ’s, and if we were to hold α2 , . . . , αn fixed,
then we can’t make any change to α1 without violating the constraint 7.22 in the
optimization problem.
Thus, if we want to update some subject of the αi ’s, we must update at least two
of them simultaneously in order to keep satisfying the constraints. This motivates
the SMO algorithm, which simply does the following:
To test for convergence of this algorithm, we can check whether the KKT
conditions (equations (7.17) to (7.19)) are satisfied to within some tol. Here, tol is
the convergence tolerance parameter, and is typically set to around 0.01 to 0.001.
(See the paper and pseudocode for details.)
The key reason that SMO is an efficient algorithm is that the update to αi , α j can
be computed very efficiently. Let’s now briefly sketch the main ideas for deriving
the efficient update.
1. Select some pair αi and α j to update next (using a heuristic that tries to
pick the two that will allow us to make the biggest progress towards
the global maximum).
2. Reoptimize W (α) with respect to αi and α j , while holding all the other
αk ’s (k 6= i, j) fixed.
until convergence
Let’s say we currently have some setting of the αi ’s that satisfy the constraints
7.21–7.22, and suppose we’ve decided to hold α3 , . . . , αn fixed, and want to re-
optimize W (α1 , α2 , . . . , αn ) with respect to α1 and α2 (subject to the constraints).
From equation (7.22), we require that
n
α 1 y (1) + α 2 y (2) = − ∑ α i y ( i ) .
i =3
Since the right hand side is fixed (as we’ve fixed α3 , . . . αn ), we can just let it be
denoted by some constant ζ:
α1 y(1) + α2 y(2) = ζ.
α 1 = ( ζ − α 2 y (2) ) y (1) .
(Check this derivation yourself; we again used the fact that y(1) ∈ {−1, 1} so that
(y(1) )2 = 1.) Hence, the objective W (α) can be written
Treating α3 , . . . , αn as constants, you should be able to verify that this is just some
quadratic function in α2 . I.e., this can also be expressed in the form aα22 + bα2 + c
for some appropriate a, b, and c. If we ignore the ‘‘box’’ constraints 7.21 (or,
equivalently, that L ≤ α2 ≤ H), then we can easily maximize this quadratic
new,unclipped
function by setting its derivative to zero and solving. We’ll let α2 denote
the resulting value of α2 . You should also be able to convince yourself that if we had
instead wanted to maximize W with respect to α2 but subject to the box constraint,
new,unclipped
then we can find the resulting value optimal simply by taking α2 and
‘‘clipping’’ it to lie in the [ L, H ] interval, to get
new,unclipped
H
if α2 >H
new,unclipped new,unclipped
αnew
2 = α if L ≤ α2 ≤H
2
new,unclipped
L if <L
α2
Finally, having found the αnew 2 , we can use section 7.9 to go back and find the
optimal value of α1 . new
There’re a couple more details that are quite easy but that we’ll leave you to
read about yourself in Platt’s paper: One is the choice of the heuristics used to
select the next αi , α j to update; the other is how to update b as the SMO algorithm
is run.
In the supervised learning setting (predicting y from the input x), suppose our
model/hypothesis is hθ ( x ). In the past lectures, we have considered the cases
when hθ ( x ) = θ > x (in linear regression or logistic regression) or hθ ( x ) = θ > φ( x )
(where φ( x ) is the feature map). A commonality of these two models is that they
are linear in the parameters θ. Next we will consider learning general family of
models that are non-linear in both the parameters θ and the inputs x. The most
common non-linear models are neural networks, which we will define staring from
the next section. For this section, it suffices to think hθ ( x ) as an abstract non-linear
model.1 1
If a concrete example is helpful,
Suppose {( x (i) , y(i) )}in=1 are the training examples. For simplicity, we start with perhaps think about the model
hθ ( x ) = θ12 x12 + θ22 x22 + · · · + θd2 xd2
the case where y(i) ∈ R and hθ ( x ) ∈ R. in this subsection, even though it’s
Cost/loss function. We define the least square cost function for the i-th example not a neural network.
( x (i) , y(i) ) as
1 2
J (i ) ( θ ) = h θ ( x (i ) ) − y (i ) (8.1)
2
and define the mean-square cost function for the dataset as
n
1
J (θ ) =
n ∑ J (i ) ( θ ) (8.2)
i =1
hθ ( x ) is different from the case of linear regression, even though the form of the
cost function is the same mean-squared loss. Throughout the notes, we use the
words ‘‘loss’’ and ‘‘cost’’ interchangeably. 2
Recall that, as defined in the pre-
vious lecture notes, we use the no-
tation ‘‘a := b’’ to denote an oper-
Optimizers (SGD). Commonly, people use gradient descent (GD), stochastic ation (in a computer program) in
gradient (SGD), or their variants to optimize the loss function J (θ ). GD’s update which we set the value of a variable
a to be equal to the value of b. In
rule can be written as2 other words, this operation over-
θ := θ − α∇θ J (θ ) (8.3) writes a with the value of b. In con-
trast, we will write ‘‘a = b’’ when
where α > 0 is often referred to as the learning rate or step size. Next, we introduce we are asserting a statement of fact,
that the value of a is equal to the
a version of the SGD (algorithm 8.1), which is slightly different from that in the
value of b.
first lecture notes. Oftentimes computing the gradient of B examples simultane-
Hyperparameter: learning rate α, number of total iteration niter . Algorithm 8.1. Stochastic gradient
descent.
Initialize θ randomly.
for i = 1 to niter do
Sample j uniformly from 1, . . . , n, and update θ by
θ : = θ − α ∇θ J ( j) (θ )
end for
ously for the parameter θ can be faster than computing B gradients separately
due to hardware parallelization. Therefore, a mini-batch version of SGD is most
commonly used in deep learning, as shown in algorithm 8.2. There are also other
variants of the SGD or mini-batch SGD with slightly different sampling schemes.
With these generic algorithms, a typical deep learning model is learned with
the following steps:
3. Run SGD or mini-batch SGD (or other gradient-based optimizers) with the
loss function J (θ ).
Hyperparameter: learning rate α, batch size B, # iteration niter . Algorithm 8.2. Mini-batch stochas-
tic gradient descent
Initialize θ randomly.
for i = 1 to niter do
Sample j uniformly from 1, . . . , n, and update θ by
Sample B examples j1 , . . . , jB (without replacement) uniformly from
{1, . . . , n}, and update θ by
B
α
θ := θ −
B ∑ ∇θ J ( jk ) (θ )
k =1
end for
A neural network with a single neuron. Recall the housing price prediction
problem from before: given the size of the house, we want to predict the price.
We will use it as a running example in this subsection.
Previously, we fit a straight line to the graph of size vs. housing price. Now,
instead of fitting a straight line, we wish to prevent negative housing prices by
setting the absolute minimum price as zero. This produces a ‘‘kink’’ in the graph
as shown in figure 9.1. How do we represent such a function with a single kink as
hθ ( x ) with unknown parameter? (After doing so, we can invoke the machinery
in part V.)
We define a parameterized function hθ ( x ) with input x, parameterized by θ,
which outputs the price of the house y. Formally, hθ : x 7→ y. Perhaps one of the
simplest parametrization would be
1,500
price (in $1000)
1,000
500
0
0 1,000 2,000 3,000 4,000 5,000
square feet
The term b is often referred to as the ‘‘bias’’, and the vector w is referred to
as the weight vector. Such a neural network has 1 layer. (We will define what
multiple layers mean in the sequel.)
Stacking neurons. A more complex neural network may take the single neuron
described above and ‘‘stack’’ them together such that one neuron passes its output
as input into the next neuron, resulting in a more complex function.
Let us now deepen the housing prediction example. In addition to the size
of the house, suppose that you know the number of bedrooms, the zip code
and the wealth of the neighborhood. Building neural networks is analogous to
Lego bricks: you take individual bricks and stack them together to build complex
structures. The same applies to neural networks: we take individual neurons
and stack them together to create complex neural networks. Given these features
(size, number of bedrooms, zip code, and wealth), we might then decide that
the price of the house depends on the maximum family size it can accommodate.
Suppose the family size is a function of the size of the house and number of
bedrooms (see figure 9.2). The zip code may provide additional information such
as how walkable the neighborhood is (i.e., can you walk to the grocery store or
do you need to drive everywhere). Combining the zip code with the wealth of the
neighborhood may predict the quality of the local elementary school. Given these
three derived features (family size, walkable, school quality), we may conclude
that the price of the home ultimately depends on these three features.
price y
walkable
zip code
u ality
ol q
scho
wealth
a1 = ReLU(θ1 x1 + θ2 x2 + θ3 )
a2 = ReLU(θ4 x3 + θ5 )
a3 = ReLU(θ6 x3 + θ7 x4 + θ8 )
parameters θi ’s correspond to the synapses. However, it’s unclear how similar the
modern deep artificial neural networks are to the biological ones. For example,
perhaps not many neuroscientists think biological neural networks could have
1000 layers, while some modern artificial neural networks do (we will elaborate
more on the notion of layers.) Moreover, it’s an open question whether human
brains update their neural networks in a way similar to the way that computer
scientists learn artificial neural networks (using backpropagation, which we will
introduce in the next section.).
Note that by default the vectors in Rd are viewed as column vectors, and in
particular a is a column vector with components a1 , a2 , . . . , am . The indices [1] and
[2] [1]
are used to distinguish two sets of parameters: the w j ’s (each of which is a
vector in Rd ) and w[2] (which is a vector in Rm ). We will have more of these later.
Vectorization. Before we introduce neural networks with more layers and more
complex structures, we will simplify the expressions for neural networks with
more matrix and vector notations. Another important motivation of vectorization
is the speed perspective in the implementation. In order to implement a neural
network efficiently, one must be careful when using for loops. The most natural
way to implement equation (9.4) in code is perhaps to use a for loop. In practice,
the dimensionalities of the inputs and hidden units are high. As a result, code
will run very slowly if you use for loops. Leveraging the parallelism in GPUs
is/was crucial for the progress of deep learning.
This gave rise to vectorization. Instead of using for loops, vectorization takes
advantage of matrix algebra and highly optimized numerical linear algebra pack-
ages (e.g., BLAS) to make neural network computations run quickly. Before the
deep learning era, a for loop may have been sufficient on smaller datasets, but
modern deep networks and state-of-the-art datasets will be infeasible to run with
for loops.
We vectorize the two-layer fully-connected neural network as below. We define
[1]
a weight matrix W [1] in Rm×d as the concatenation of all the vectors w j ’s in the
following way:
[1] >
w1
[1] >
w2
W [1] = ∈ Rm × d
..
.
[1] >
wm
Now by the definition of matrix vector multiplication, we can write z =
[z1 , . . . , zm ]> ∈ Rm as:
[1] >
z1 [1]
w1 x1 b1
.
.. w [1] > x [1]
x
2
2 2
. = .. +
.. ..
.
. . . .
[1] > zd [1]
bm
zm wm
| {z } | {z } | {z }
d ×1 x ∈R
| {z }
z ∈Rm ×1 W [1] ∈Rm × d b [1] ∈Rm ×1
Or succinctly,
z = W [1] x + b [1] (9.8)
We remark again that a vector in Rd
in these notes, following the conventions
previously established, is automatically viewed as a column vector, and can also
be viewed as a d × 1 dimensional matrix. (Note that this is different from numpy
where a vector is viewed as a row vector in broadcasting.)
Computing the activations a ∈ Rm from z ∈ Rm involves an element-wise
non-linear application of the ReLU function, which can be computed in parallel
efficiently. Overloading ReLU for element-wise application of ReLU (meaning, for
a vector t ∈ Rd , ReLU(t) is a vector such that ReLU(t)i = ReLU(ti )), we have:
a = ReLU(z) (9.9)
>
Define W [2] = [w[2] ] ∈ R1×m similarly. Then, the model in equation (9.7) can
be summarized as:
a = ReLU(W [1] x + b[1] ) (9.10)
[2] [2]
hθ ( x ) = W a + b (9.11)
Here θ consists of W [1] , W [2] (often referred to as the weight matrices) and b[1] , b[2]
(referred to as the biases). The collection of W [1] , b[1] is referred to as the first layer,
and W [2] , b[2] the second layer. The activation a is referred to as the hidden layer.
A two-layer neural network is also called one-hidden-layer neural network.
We note that the weight matrices and biases need to have compatible dimen-
sions for the equations above to make sense. If a[k] has dimension mk , then the
weight matrix W [k] should be of dimension mk × mk−1 , and the bias b[k] ∈ Rmk .
Moreover, W [1] ∈ Rm1 ×d and W [r] ∈ R1×mr−1 .
The total number of neurons in the network is m1 + · · · + mr , and the total
number of parameters in this network is (d + 1)m1 + (m1 + 1)m2 + · · · + (mr−1 +
1) mr .
Sometimes for notational consistency we also write a[0] = x, and a[r] = hθ ( x ).
Then we have simple recursion that
Note that this would have be true for k = r if there were an additional ReLU in
equation (9.16), but often people like to make the last layer linear (aka without a
ReLU) so that negative outputs are possible and it’s easier to interpret the last
layer as a linear model. (More on the interpretability at the ‘‘connection to kernel
method’’ paragraph of this section.)
1
σ(z) = (sigmoid)
1 + e−z
ez − e−z
σ(z) = z (tanh)
e + e−z
Why do we not use the identity function for σ(z)? That is, why not use σ (z) = z?
Assume for sake of argument that b[1] and b[2] are zeros. Suppose σ (z) = z, then
for two-layer neural network, we have that
h θ ( x ) = W [2] a [1]
= W [2] σ ( z [1] ) (by definition)
[2] [1]
=W z (since σ (z) = z)
= W [2] W [1] x (from chapter 9)
= W̃x (where W̃ = W [2] W [1] )
When β is fixed, then φβ (·) can viewed as a feature map, and therefore hθ ( x ) is just
a linear model over the features φβ ( x ). However, we will train the neural networks,
both the parameters in β and the parameters W [r] , b[r] are optimized, and therefore
we are not learning a linear model in the feature space, but also learning a good
feature map φβ (·) itself so that it’s possible to predict accurately with a linear
model on top of the feature map. Therefore, deep learning tends to depend less
on the domain knowledge of the particular applications and requires often less
feature engineering. The penultimate layer a[r−1] is often (informally) referred to
as the learned features or representations in the context of deep learning.
In the example of house price prediction, a fully-connected neural network
does not need us to specify the intermediate quantity such ‘‘family size’’, and
may automatically discover some useful features in the last penultimate layer
(the activation a[r−1] ), and use them to linearly predict the housing price. Often
the feature map / representation obtained from one datasets (that is, the function
φβ (·) can be also useful for other datasets, which indicates they contain essential
information about the data. However, oftentimes, the neural network will discover
complex features which are very useful for predicting the output but may be
difficult for a human to understand or interpret. This is why some people refer to
neural networks as a black box, as it can be difficult to understand the features it
has discovered.
We first recall the chain rule in calculus. Suppose the variable J depends on the
variables θ1 , . . . , θ p via the intermediate variables g1 , . . . , gk :
Here we overload the meaning of g j ’s: they denote both the intermediate variables
but also the functions used to compute the intermediate variables. Then, by the
chain rule, we have that ∀i:
k
∂J ∂J ∂gj
∂θi
= ∑ ∂gj ∂θi (10.2)
j =1
For the ease of invoking the chain rule in the following subsections in various
ways, we will call J the output variable, g1 , . . . , gk intermediate variables, and
θ1 , . . . , θ p the input variables in the chain rule.
Now we consider the two-layer neural network defined in equation (9.11). Our
general approach is to first unpack the vectorized notation to scalar form to apply
the chain rule, but as soon as we finish the derivation, we will pack the scalar
equations back to a vectorized form to keep the notations succinct.
Recall the following equations are used for the computation of the loss J:
Recall that W [1] ∈ Rm×d , W [2] ∈ R1×m , and b[1] , z, a ∈ Rm , and o, y, b[2] ∈ R.
Recall that a vector in Rd is automatically interpreted as a column vector (like a
matrix in Rd×1 ) if need be.2 2
We also note that even though
this is the convention in math, it’s
different from the convention in
numpy where an one dimensional
array will be automatically inter-
preted as a row vector.
2021-05-23 00:18:27-07:00, draft: send comments to [email protected] toc
10.2. backpropagation for two-layer neural networks 89
∂J
10.2.1 Computing ∂W [2]
[2] [2]
Suppose W [2] = [W1 , . . . , Wm ]. We start by computing ∂J
[2] using the chain rule
∂Wi
(equation (10.2)) with o as the intermediate variable.
∂J ∂J ∂o
= ·
∂W [2]i ∂o ∂W [2]i
∂o
= (o − y) ·
∂W [2]i
= ( o − y ) · ai (because o = ∑im=1 W [2]i ai + b[2] )
∂J ∂J >
= ·x (10.9)
∂W [1] ∂z
∂J ∂J
Abstraction. For future usage, the computations for and above can
∂W [1] ∂W [2]
be abstractified into the following claim:
z = Wu + b
J = J (z)
∂J ∂J
Then ∂W and ∂b satisfy:
∂J ∂J >
= ·u
∂W ∂z
∂J ∂J
=
∂b ∂z
∂J
10.2.3 Computing ∂z
∂J ∂J ∂ai
=
∂zi ∂ai ∂zi
∂J
= · 1{ z i ≥ 0}
∂ai
∂J
10.2.4 Computing ∂a
∂J
Now it suffices to compute ∂a . We invoke the chain rule with J as the output
variable, o as the intermediate variable, and ai as the input variable:
∂J ∂J ∂o
=
∂ai ∂o ∂ai
[2] [2]
= (o − y) · Wi (because o = ∑im=1 Wi ai + b[2] )
v = Wu + b
J = J (v)
Then,
∂J ∂J
= W> . (10.11)
∂u ∂v
∂J
δ [2] , = (o − y) ∈ R
∂o
∂J >
δ [1] , = (W [2] (o − y)) 1{z ≥ 0} ∈ Rm×1 (by claim 2 and 10.10)
∂z
Compute:
∂J
= δ [ 2 ] a > ∈ R1 × m (by equation (10.7))
∂W [2]
∂J
= δ [2] ∈ R (by equation (10.8))
∂b[2]
∂J
= δ [1] x > ∈ Rm × d (by equation (10.9))
∂W [1]
∂J
= δ [1] ∈ Rm (as an exercise)
∂b[1]
In this section, we will derive the backpropagation algorithms for the model
defined in equation (9.16). With the notation a[0] = x, recall that we have:
z [ k ] = W [ k ] a [ k −1] + b [ k ]
J = J (z[k] )
∂J ∂J >
[ k ]
= [ k ] · a [ k −1] (10.12)
∂W ∂z
∂J ∂J
= [k] (10.13)
∂b[k] ∂z
∂J
δ [r ] , = ( z [r ] − y ) (10.14)
∂z[r]
Next for k ≤ r − 1, suppose we have computed the value of δ[k+1] , then we will
compute δ[k] . First, using claim 2, we have that:
∂J ∂J
δ[k] , [ k ]
= [k] ReLU0 (z[k] ) (10.15)
∂z ∂a
Then we note that the relationship between a[k] and z[k+1] can be abstractly written
as:
∂J > ∂J
[ k ]
= W [ k +1] (10.18)
∂a ∂z[k+1]
It follows that:
[ k +1] > ∂J
δ [k]
= W ReLU0 (z[k] )
∂z[k+1]
>
= W [ k +1] δ [ k +1] ReLU0 (z[k] )
∂J
[ k +1] > [ k +1]
δ[k] , = W δ ReLU0 (z[k] )
∂z[k]
Compute:
∂J >
[ k ]
= δ [ k ] a [ k −1]
∂W
∂J
= δ[k]
∂b[k]
end for
The basic idea. The basic idea is simple. Suppose you have a training set with
three examples x (1) , x (2) , x (3) . The first-layer activations for each example are as
follows:
Note the difference between square brackets [·], which refer to the layer number,
and parenthesis (·), which refer to the training example number. Intuitively,
one would implement this using a for loop. It turns out, we can vectorize these
operations as well. First, define:
Note that we are stacking training examples in columns and not rows. We can
then combine this into a single unified formulation:
You may notice that we are attempting to add b[1] ∈ R4×1 to W [1] X ∈ R4×3 . Strictly
following the rules of linear algebra, this is not allowed. In practice however, this
addition is performed using broadcasting. We create an intermediate b̃[1] ∈ R4×3 :
We can then perform the computation: Z [1] = W [1] X + b̃[1] . Often times, it is
not necessary to explicitly construct b̃[1] . By inspecting the dimensions in equa-
tion (11.1), you can assume b[1] ∈ R4×1 is correctly broadcast to W [1] X ∈ R4×3 .
The matricization approach as above can easily generalize to multiple layers,
with one subtlety though, as discussed below.
1. Randomly split S into Strain (say, 70% of the data) and Scv (the remaining 30%).
Here, Scv is called the hold-out cross validation set.
3. Select and output the hypothesis hi that had the smallest error ε̂ Scv (hi ) on the
hold out cross validation set. (Recall, ε̂ Scv (h) denotes the empirical error of h
on the set of examples in Scv .)
By testing on a set of examples Scv that the models were not trained on, we
obtain a better estimate of each hypothesis hi ’s true generalization error, and
can then pick the one with the smallest estimated generalization error. Usually,
somewhere between 1/4 − 1/3 of the data is used in the hold out cross validation
set, and 30% is a typical choice.
Optionally, step 3 in the algorithm may also be replaced with selecting the
model Mi according to arg mini ε̂ Scv (hi ), and then retraining Mi on the entire
training set S. (This is often a good idea, with one exception being learning
algorithms that are be very sensitive to perturbations of the initial conditions
and/or data. For these methods, Mi doing well on Strain does not necessarily mean
it will also do well on Scv , and it might be better to forgo this retraining step.)
The disadvantage of using hold out cross validation is that it ‘‘wastes’’ about
30% of the data. Even if we were to take the optional step of retraining the model
on the entire training set, it’s still as if we’re trying to find a good model for a
learning problem in which we had 0.7m training examples, rather than n training
examples, since we’re testing models that were trained on only 0.7m examples
each time. While this is fine if data is abundant and/or cheap, in learning problems
in which data is scarce (consider a problem with m = 20, say), we’d like to do
something better.
Here is a method, called k-fold cross validation, that holds out less data each
time:
1. Randomly split S into k disjoint subsets of m/k training examples each. Lets
call these subsets S1 , . . . , Sk .
• For j = 1, . . . , k:
– Train the model Mi on S1 ∪ · · · ∪ S j−1 ∪ S j+1 ∪ · · · Sk (i.e., train on all the
data except S j ) to get some hypothesis hij .
3. Pick the model Mi with the lowest estimated generalization error, and retrain
that model on the entire training set S. The resulting hypothesis is then output
as our final answer.
A typical choice for the number of folds to use here would be k = 10. While
the fraction of data held out each time is now 1/k—much smaller than before—
this procedure may also be more computationally expensive than hold-out cross
validation, since we now need train to each model k times.
While k = 10 is a commonly used choice, in problems in which data is really
scarce, sometimes we will use the extreme choice of k = m in order to leave out
as little data as possible each time. In this setting, we would repeatedly train
on all but one of the training examples in S, and test on that held-out example.
The resulting m = k errors are then averaged together to obtain our estimate of
the generalization error of a model. This method has its own name; since we’re
holding out one training example at a time, this method is called leave-one-out
cross validation.
Finally, even though we have described the different versions of cross validation
as methods for selecting a model, they can also be used more simply to evaluate a
single model or algorithm. For example, if you have implemented some learning
algorithm and want to estimate how well it performs for your application (or if
you have invented a novel learning algorithm and want to report in a technical
paper how well it performs on various test sets), cross validation would give a
reasonable way of doing so.
13 Feature Selection
One special and important case of model selection is called feature selection.
To motivate this, imagine that you have a supervised learning problem where the
number of features d is very large (perhaps d n), but you suspect that there is
only a small number of features that are ‘‘relevant’’ to the learning task. Even if
101
you use the a simple linear classifier (such as the perceptron) over the d input
features, the VC dimension of your hypothesis class would still be O(n), and thus
overfitting would be a potential problem unless the training set is fairly large.
In such a setting, you can apply a feature selection algorithm to reduce the
number of features. Given d features, there are 2d possible feature subsets (since
each of the d features can either be included or excluded from the subset), and
thus feature selection can be posed as a model selection problem over 2d possible
models. For large values of d, it’s usually too expensive to explicitly enumerate
over and compare all 2d models, and so typically some heuristic search procedure
is used to find a good feature subset. The following search procedure is called
forward search:
repeat
for i = 1, . . . , d do
if i 6∈ F then
Fi = F ∪ {i }
Use some version of cross validation to evaluate features Fi .
(i.e., train your learning algorithm using only the features in Fi ,
and estimate its generalization error.)
end for
Set F to be the best feature subset found in the previous step.
until convergence
Select and output the best feature subset that was evaluated during the
entire search procedure.
The outer loop of the algorithm can be terminated either when F = {1, . . . , d}
is the set of all features, or when |F | exceeds some pre-set threshold (correspond-
ing to the maximum number of features that you want the algorithm to consider
using).
This algorithm described above one instantiation of wrapper model feature
selection, since it is a procedure that ‘‘wraps’’ around your learning algorithm,
and repeatedly makes calls to the learning algorithm to evaluate how well it
does using different feature subsets. Aside from forward search, other search
procedures can also be used. For example, backward search starts off with F =
{1, . . . , d} as the set of all features, and repeatedly deletes features one at a time
(evaluating single-feature deletions in a similar manner to how forward search
evaluates single-feature additions) until F = ∅.
Wrapper feature selection algorithms often work quite well, but can be compu-
tationally expensive given how that they need to make many calls to the learning
algorithm. Indeed, complete forward search (terminating when F = {1, . . . , d})
would take about O(n2 ) calls to the learning algorithm.
Filter feature selection methods give heuristic, but computationally much
cheaper, ways of choosing a feature subset. The idea here is to compute some
simple score S(i ) that measures how informative each feature xi is about the class
labels y. Then, we simply pick the k features with the largest scores S(i ).
One possible choice of the score would be define S(i ) to be (the absolute value
of) the correlation between xi and y, as measured on the training data. This would
result in our choosing the features that are the most strongly correlated with
the class labels. In practice, it is more common (particularly for discrete-valued
features xi ) to choose S(i ) to be the mutual information MI( xi , y) between xi and
y:
p( x , y)
MI( xi , y) = ∑ ∑ p(xi , y) log p(xi )i p(y) (13.1)
i x ∈{0,1} y∈{0,1}
(The equation above assumes that xi and y are binary-valued; more generally
the summations would be over the domains of the variables.) The probabilities
above p( xi , y), p( xi ) and p(y) can all be estimated according to their empirical
distributions on the training set.
To gain intuition about what this score does, note that the mutual information
can also be expressed as a Kullback-Leibler (KL) divergence:
You’ll get to play more with KL-divergence in the problem sets, but informally,
this gives a measure of how different the probability distributions p( xi , y) and
p( xi ) p(y) are. If xi and y are independent random variables, then we would have
p( xi , y) = p( xi ) p(y), and the KL-divergence between the two distributions will
be zero. This is consistent with the idea if xi and y are independent, then xi is
clearly very ‘‘non-informative’’ about y, and thus the score S(i ) should be small.
Conversely, if xi is very ‘‘informative’’ about y, then their mutual information
MI( xi , y) would be large.
In this section, we will talk about one more tool in our arsenal for our battle
against overfitting.
At the beginning of the quarter, we talked about parameter fitting using maxi-
mum likelihood estimation (MLE), and chose our parameters according to
n
θMLE = arg max ∏ p(y(i) | x (i) ; θ ). (14.1)
θ i =1
p(S | θ ) p(θ )
p(θ | S) = (14.2)
p(S)
∏in=1 p(y(i) | x (i) , θ ) p(θ )
= R n
(14.3)
θ ∏ i =1 p ( y
(i ) | x (i ) , θ ) p ( θ ) dθ
In the equation above, p(y(i) | x (i) , θ ) comes from whatever model you’re using for
your learning problem. For example, if you are using Bayesian logistic regression,
(i ) (i )
then you might choose p(y(i) | x (i) , θ ) = hθ ( x (i) )y (1 − hθ ( x (i) ))(1−y ), where
hθ ( x (i) ) = 1/(1 + exp(−θ > x (i) )).1 1
Since we are now viewing θ as
When we are given a new test example x and asked to make a prediction on it, a random variable, it is okay to
condition on its value, and write
we can compute our posterior distribution on the class label using the posterior ‘‘p(y| x, θ )’’ instead of ‘‘p(y| x; θ ).’’
distribution on θ:
Z
p(y | x, S) = p(y | x, θ ) p(θ | S)dθ (14.4)
θ
In the equation above, p(θ | S) comes from equation (14.2). Thus, for example, if
the goal is to the predict the expected value of y given x, then we would output:2 2
The integral below would be re-
Z placed by a summation if y is
discrete-valued.
E[y | x, S] = yp(y | x, S)dy (14.5)
y
The procedure that we’ve outlined here can be thought of as doing ‘‘fully
Bayesian’’ prediction, where our prediction is computed by taking an average
with respect to the posterior p(θ | S) over θ. Unfortunately, in general it is com-
putationally very difficult to compute this posterior distribution. This is because
it requires taking integrals over the (usually high-dimensional) θ as in equa-
tion (14.2), and this typically cannot be done in closed-form.
Thus, in practice we will instead approximate the posterior distribution for θ.
One common approximation is to replace our posterior distribution for θ (as in
equation (14.4)) with a single point estimate. The MAP (maximum a posteriori)
estimate for θ is given by:
n
θMAP = arg max ∏ p(y(i) | x (i) , θ ) p(θ ) (14.6)
θ i =1
Note that this is the same formulas as for the MLE (maximum likelihood) estimate
for θ, except for the prior p(θ ) term at the end.
In practical applications, a common choice for the prior p(θ ) is to assume
that θ ∼ N (0, τ 2 I ). Using this choice of prior, the fitted parameters θ MAP will
have smaller norm than that selected by maximum likelihood. In practice, this
causes the Bayesian MAP estimate to be less susceptible to overfitting than the ML
estimate of the parameters. For example, Bayesian logistic regression turns out to
be an effective algorithm for text classification, even though in text classification
we usually have d n.
105
By setting ∇θ `(θ, λ) = 0 we can solve for the θ̂ that minimizes the above problem.
Explicitly, we have:
θ̂ = ( X > X + λI )−1 X > y (15.3)
To see that the inverse in equation (15.3) exists, we observe that X > X is a sym-
metric, real d × d matrix so it has d eigenvalues (some may be 0). Moreover, it is
positive semidefinite, and we capture this by writing eig( X > X ) = {σ12 , . . . , σd2 }.
Now, inspired by the regularized problem, we examine:
n o
eig( X > X + λI ) = σ12 + λ, . . . , σd2 + λ (15.4)
Since σi2 ≥ 0 for all i ∈ [d], if we set λ > 0 then X > X + λI is full rank, and the
inverse of ( X > X + λI ) exists. In turn, this means there is a unique such θ̂.
Now, condition on the points we draw, namely X. Then, recall that randomness
is in the label noise (recall the linear regression model y ∼ Xθ ∗ + N (0, τ 2 I ) =
N ( Xθ ∗ , τ 2 I )).
Recall a fact about the multivariate normal distribution:
The last line above suggests that the more regularization we add (larger the λ),
the more the estimated θ̂ will be shrunk towards 0. In other words, regularization
adds bias (towards zero in this case). Though we paid the cost of higher bias, we
gain by reducing the variance of θ̂. To see this bias-variance tradeoff concretely,
observe the covariance matrix of θ̂:
C := Cov[θ̂ ] (15.10)
= ( X > X + λI )−1 X > (τ 2 I ) X ( X > X + λI )−1 (15.11)
and
( )
τ 2 σ12 τ 2 σd2
eig(C ) = 2
, . . . , 2
(15.12)
(σ1 + λ)2 (σd + λ)2
Gradient descent. We show that you can initialize gradient descent in a way that
effectively regularizes undetermined least squares—even with no regularization
penalty (λ = 0). Our first observation is that any point x ∈ Rd can be decomposed
into two orthogonal components x0 , x1 such that:
Recall that Null( X ) and Range( X > ) are orthogonal subspaces by the fundamental
theory of linear algebra. We write P0 for the projection on the null and P1 for the
projection on the range, then x0 = P0 ( x ) and x1 = P1 ( x ).
If one initializes at a point θ then, we observe that the gradient is orthogonal
to the null space. That is, if g(θ ) = X > ( Xθ − y) then g> P0 (v) = 0 for any v ∈ Rd .
But, then:
That is, no learning happens in the null. Whatever portion is in the null that we
initialize stays there throughout execution.
A key property of the Moore-Penrose pseudoinverse, is that if θ̂ = ( X > X ) +
X > y then P0 (θ̂ ) = 0. Hence, the gradient descent solution initialized at θ0 can be
written θ̂ + P0 (θ0 ). Two immediate observations:
We’ve argued that there are many ways to find equivalent solutions, and
that this allows us to understand the effect on the model fitting procedure as
regularization. Thus, there are many ways to find that equivalent solution. Many
modern methods of machine learning including dropout and data augmentation
are not penalty, but their effect is understood as regularization. One contrast with
the above methods is that they often depend on some property of the data or for
108 chapter 16. bias-variance and error analysis
how much they effectively regularization. In some sense, they adapt to the data.
A final comment is that in the same sense above, adding more data regularizes
the model as well!
Assume you are given a well fitted machine learning model fˆ that you want to
apply on some test dataset. For instance, the model could be a linear regression
whose parameters were computed using some training set different from your
test set. For each point x in your test set, you want to predict the associated target
y ∈ R, and compute the mean squared error (MSE):
h i
E( x,y)∼test set | fˆ( x ) − y|2 (16.1)
You now realize that this MSE is too high, and try to find an explanation to this
result:
• Overfitting: the model is too closely related to the examples in the training set
and doesn’t generalize well to other examples.
• Underfitting: the model didn’t gather enough information from the training set,
and doesn’t capture the link between the features x and the target y.
• The data is simply noisy, that is the model is neither overfitting or underfitting,
and the high MSE is simply due to the amount of noise in the dataset.
and your goal is to compute f . By looking at your training set, you obtain an
estimate fˆ. Now use this estimate with your test set, meaning that for each example
j in the test set, your prediction for y j = f ( x j ) + e j is fˆ( x j ). Here, x j is a fixed
real number (or vector if the feature space is multi-dimensional) thus f ( x j ) is
fixed, and e j is a real random variable with mean 0 and variance σ2 . The crucial
observation is that fˆ( x j ) is random since it depends on the values ei from the
training set. That’s why talking about the bias E[ fˆ( x ) − f ( x )] and the variance of
fˆ makes sense.
We can now compute our MSE on the test set by computing the following
expectation with respect to the possible training sets (since fˆ is a random variable
function of the choice of the traning set):
h i
test MSE = E (y − fˆ( x ))2 (16.3)
h i
= E ((e + f ( x ) − fˆ( x ))2 (16.4)
h i
= E[e2 ] + E ( f ( x ) − fˆ( x ))2 (16.5)
2
= σ2 + E[ f ( x ) − fˆ( x )] + Var f ( x ) − fˆ( x ) (16.6)
2
= σ2 + Bias fˆ( x ) + Var fˆ( x ) (16.7)
There is nothing we can do about the first term σ2 as we can not predict the
noise e by definition. The bias term is due to underfitting, meaning that on average,
fˆ does not predict f . The last term is closely related to overfitting, the prediction fˆ
is too close from the values y train and varies a lot with the choice of our training
set.
To sum up, we can understand our MSE as follows:
Even though understanding whether our poor test error is due to high bias or high
variance is important, knowing which parts of the machine learning algorithm
lead to this error or score is crucial. Consider the machine learning pipeline on ??.
The algorithms is divided into several steps:
If you biuld a complicated system like this one, you might want to figure out
how much error is attributable to each of the components, how good is each of
these green boxes. Indeed, if one of these boxes is really problematic, you might
want to spend more time trying to improve the performance of that one green
box. How do you decide what part to focus on?
One thing we can do is plug in the ground-truth for each component, and
see how accuracy changes. Let’s say the overall accuracy of the system is 85%
(pretty bad). You can now take your development set and manually give it the
perfect background removal, that is, instead of using your background removal
algorithm, manually specify the perfect background removal yourself (using
photoshop for instance), and look at how much that affect the performance of
the overall system.
Now let’s say the accuracy only improves by 0.1%. This gives us an upperbound,
that is even if we worked for years on background removal, it wouldn’t help our
system by more than 0.1%.
Now let’s give the pipeline the perfect face detection by specifying the position
of the face manually, see how much we improve the performance, and so on. The
results are specified in the table 16.1.
Looking at the table, we know that working on the background removal won’t
help much. It also tells us where the biggest jumps are. We notice that having an
accurate face detection mechanism really improves the performance, and similarly,
the eyes really help making the prediction more accurate.
Error analysis is also useful when publishing a paper, since it’s a convenient
way to analyze the error of an algorithm and explain which parts should be
improved.
While error analysis tries to explain the difference between current performance
and perfect performance, ablative analysis tries to explain the difference between
some baseline (much poorer) performance and current performance.
For instance, suppose you have built a good anti-spam classifier by adding lots
of clever features to logistic regression
• Spelling correction
• Javascript parser
and your question is: How much did each of these components really help?
In this example, let’s say that simple logistic regression without any clever
features gets 94% performance, but when adding these clever features, we get
99.9% performance. In abaltive analysis, what we do is start from the current level
of performance 99.9%, and slowly take away all of these features to see how it
affects performance. The results are provided in table 16.2.
By analyzing your mistakes, you can focus on what’s really important. If you
notice that 80 out of your 100 mistakes are blurry images, then work hard on
classifying correctly these blurry images. If you notice that 70 out of the 100 errors
are great cats, then focus on this specific task of identifying great cats.
In brief, do not waste your time improving parts of your algorithm that won’t
really help decreasing your error rate, and focus on what really matters.
In the clustering problem, we are given a training set { x (1) , . . . , x (n) }, and want
to group the data into a few cohesive ‘‘clusters.’’ Here, x (i) ∈ Rd as usual; but no
labels y(i) are given. So, this is an unsupervised learning problem. The k-means
clustering algorithm is as follows:
Thus, J measures the sum of squared distances between each training example
x (i) and the cluster centroid µc(i) to which it has been assigned. It can be shown
that k-means is exactly coordinate descent on J. Specifically, the inner-loop of
k-means repeatedly minimizes J with respect to c while holding µ fixed, and then
minimizes J with respect to µ while holding c fixed. Thus, J must monotonically
decrease, and the value of J must converge. (Usually, this implies that c and µ
will converge too. In theory, it is possible for k-means to oscillate between a few
different clusterings—i.e., a few different values for c and/or µ—that have exactly
the same value of J, but this almost never happens in practice.)
The distortion function J is a non-convex function, and so coordinate descent on
J is not guaranteed to converge to the global minimum. In other words, k-means
can be susceptible to local optima. Very often k-means will work fine and come
up with very good clusterings despite this. But if you are worried about getting
stuck in bad local minima, one common thing to do is run k-means many times
(using different random initial values for the cluster centroids µ j ). Then, out of
all the different clusterings found, pick the one that gives the lowest distortion
J (c, µ).
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
`(φ, µ, Σ) = ∑ log p(x(i) ; φ, µ, Σ)
i =1
n k
= ∑ log ∑ p( x (i) | z(i) ; µ, Σ) p(z(i) ; φ)
i =1 z (i ) =1
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
`(φ, µ, Σ) = ∑ log p(x(i) | z(i) ; µ, Σ) + log p(z(i) ; φ)
i =1
Indeed, we see that if the z(i) ’s were known, then maximum likelihood esti-
mation 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 1
There are other minor differences
However, in our density estimation problem, the z(i) ’s are not known. What in the formulas here from what
we’d obtained in PS1 with Gaus-
can we do? The EM algorithm is an iterative algorithm that has two main steps. sian discriminant analysis, first be-
cause we’ve generalized the z(i) ’s
2021-05-23 00:18:27-07:00, draft: send comments to [email protected] to be multinomial rather than toc
Bernoulli, and second because here
we are using a different Σ j for each
Gaussian.
117
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:
It’s clear that the EM algorithm has a very natural interpretation of repeatedly
trying to guess the unknown z(i) ’s; but how did it come about, and can we make
any guarantees about it, such as regarding its convergence? In the next set of
notes, we will describe a more general view of EM, one 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.
19 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 00 ( 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 00 ( 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:
20 The EM algorithm
p( x; θ ) = ∑ p(x, z; θ ) (20.1)
z
But explicitly finding the maximum likelihood estimates of the parameters θ may
be hard since it will result in difficult non-convex optimization problems.1 Here, 1
It’s mostly an empirical observa-
the z(i) ’s are the latent random variables; and it is often the case that if the z(i) ’s tion that the optimization problem
is difficult to optimize.
were observed, then maximum likelihood estimation would be easy.
In such a setting, the EM algorithm gives an efficient method for maximum
likelihood estimation. Maximizing `(θ ) explicitly might be difficult, and our
strategy will be to instead repeatedly construct a lower-bound on ` (E-step), and
then optimize that lower-bound (M-step).2 2
Empirically, the E-step and M-
It turns out that the summation ∑in=1 is not essential here, and towards a step can often be computed more
efficiently than optimizing the
simpler exposition of the EM algorithm, we will first consider optimizing the function `(·) directly. However, it
the likelihood log p( x ) for a single example x. After we derive the algorithm for doesn’t necessarily mean that al-
ternating the two steps can always
optimizing log p( x ), we will convert it to an algorithm that works for n examples converge to the global optimum
by adding back the sum to each of the relevant equations. Thus, now we aim to of `(·). Even for mixture of Gaus-
optimize log p( x; θ ) which can be rewritten as: sians, the EM algorithm can either
converge to a global optimum or
get stuck, depending on the prop-
log p( x; θ ) = log ∑ p( x, z; θ ) (20.5) erties of the training data. Empiri-
z cally, for real-world data, often EM
can converge to a solution with rel-
Let Q be a distribution over the possible values of z. That is, ∑z Q(z) = 1, Q(z) ≥ atively high likelihood (if not the
0. optimum), and the theory behind
Consider the following:3 it is still largely not understood.
3
If z were continuous, then Q
would be a density, and the sum-
log p( x; θ ) = log ∑ p( x, z; θ ) (20.6) mations over z in our discussion
z
are replaced with integrals over z.
p( x, z; θ )
= log ∑ Q(z) (20.7)
z Q(z)
p( x, z; θ )
≥ ∑ Q(z) log (20.8)
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 (20.7) to equation (20.8).
Now, for any distribution Q, the formula 20.8 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; θ )
=c
Q(z)
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
p( x, z; θ )
Q(z) = (20.9)
∑z p( x, z; θ )
p( x, z; θ )
= (20.10)
p( x; θ )
= p(z | x; θ ) (20.11)
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 (20.8) is an equality because:
p( x, z; θ ) p( x, z; θ )
∑ Q(z) log Q(z)
= ∑ p(z | x; θ ) log p(z | x; θ )
z z
p(z | x; θ ) p( x; θ )
= ∑ p(z | x; θ ) log
z p(z | x; θ )
= ∑ p(z | x; θ ) log p( x; θ )
z
= log p( x; θ ) ∑ p(z | x; θ )
z
= log p( x; θ ) (because ∑z p(z | x; θ ) = 1)
For convenience, we call the expression in equation (20.8) the evidence lower
bound (ELBO) and we denote it by:
p( x, z; θ )
ELBO( x; Q, θ ) = ∑ Q(z) log Q(z)
(20.12)
z
p ( x (i ) , z (i ) ; θ )
log p( x (i) ; θ ) ≥ ELBO( x (i) ; Qi , θ ) = ∑ Qi (z(i) ) log Q i ( z (i ) )
z (i )
Taking sum over all the examples, we obtain a lower bound for the log- likelihood:
Q i ( z (i ) ) = p ( z (i ) | x (i ) ; θ )
Thus, we simply set the Qi ’s to be the posterior distribution of the z(i) ’s given x (i)
with the current setting of the parameters θ.
Now, for this choice of the Qi ’s, equation (20.14) gives a lower-bound on the
log-likelihood ` that we’re trying to maximize. This is the E-step. In the M-step of
the algorithm, we then maximize our formula in equation (20.14) with respect to
the parameters to obtain a new setting of the θ’s. Repeatedly carrying out these
two steps gives us the EM algorithm, which is as follows:
• Repeat until convergence:
Q i ( z (i ) ) : = p ( z (i ) | x (i ) ; θ )
– (M-step) Set:
n
θ := arg max ∑ ELBO( x (i) ; Qi , θ ) (20.16)
θ i =1
p ( x (i ) , z (i ) ; θ )
= arg max ∑ ∑ Qi (z(i) ) log . (20.17)
θ i z (i ) Q i ( z (i ) )
How do we know if this algorithm will converge? Well, suppose θ (t) and θ (t+1)
are the parameters from two successive iterations of EM. We will now prove
that `(θ (t) ) ≤ `(θ (t+1) ), which shows EM always monotonically improves the log-
likelihood. The key to showing this result lies in our choice of the Qi ’s. Specifically,
on the iteration of EM in which the parameters had started out as θ (t) , we would
(t)
have chosen Qi (z(i) ) := p(z(i) | x (i) ; θ (t) ). We saw earlier that this choice ensures
that Jensen’s inequality, as applied to get equation (20.14), holds with equality,
and hence:
n
∑ ELBO(x(i) ; Qi
(t)
`(θ (t) ) = , θ (t) ) (20.18)
i =1
The parameters θ (t+1) are then obtained by maximizing the right hand side of
the equation above. Thus,
n
∑ ELBO(x(i) ; Qi
(t)
`(θ (t+1) ) ≥ , θ ( t +1) )
i =1
(because inequality 20.14 holds for all Q and θ)
n
∑ ELBO(x(i) ; Qi
(t)
≥ , θ (t) ) (see reason below)
i =1
= `(θ (t) ) (by equation (20.18))
where the last inequality follows from that θ (t+1) is chosen explicitly to be:
n
arg max ∑ ELBO( x (i) ; Qi , θ )
(t)
θ i =1
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 θ.
Q(z)
DKL ( Q || pz ) = ∑ Q(z) log p(z)
(20.22)
z
In many cases, the marginal distribution of z does not depend on the parameter θ.
In this case, we can see that maximizing ELBO over θ is equivalent to maximizing
the first term in 20.21. 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):
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 (20.11) before.
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 calcu-
late:
(i )
w j = Qi (z(i) = j) = P(z(i) = j | x (i) ; φ, µ, Σ)
127
Here, ‘‘Qi (z(i) = j)’’ denotes the probability of z(i) taking the value j under the
distribution Qi .
Next, in the M-step, we need to maximize, with respect to our parameters
φ, µ, Σ, the quantity:
n
p( x (i) , z(i) ; φ, µ, Σ)
∑ ∑ Qi (z(i) ) log Q i ( z (i ) )
i =1 z (i )
n k
p( x (i) | z(i) = j; µ, Σ) p(z(i) = j; φ)
= ∑ ∑ Qi (z(i) = j) log Q i ( z (i ) = j )
i =1 j =1
1
exp − 1 (i )
( x − µ ) > Σ −1 ( x ( i ) − µ ) · φ
n k (2π )d/2 |Σ |1/2 2 j j j j
∑ ∑ wj
(i ) j
= log (i )
i =1 j =1 wj
Let’s maximize this with respect to µl . If we take the derivative with respect to µl ,
we find:
1
exp − 1 (i )
( x − µ ) > Σ −1 ( x ( i ) − µ ) · φ
n k (2π )d/2 |Σ j |1/2 2 j j j j
∇µl ∑ ∑ w j log
(i )
(i )
i =1 j =1 wj
n k
(i ) 1
= −∇µl ∑ ∑ wj 2
( x (i ) − µ j ) > Σ − 1 (i )
j (x − µj )
i =1 j =1
n
1
2 i∑
(i )
= wl ∇µl 2µ> −1 ( i )
l Σl x − µ> −1
l Σl µl
=1
n
= ∑ wl Σ−
(i ) 1 (i )
l x − Σ− 1
l µl
i =1
Setting this to zero and solving for µl therefore yields the update rule
(i )
∑in=1 wl x (i)
µl := (i )
,
∑in=1 wl
which was what we had in the previous set of notes.
Let’s do one more example, and derive the M-step update for the parameters
φj . Grouping together only the terms that depend on φj , we find that we need to
maximize:
n k
∑ ∑ wj
(i )
log φj
i =1 j =1
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 assumption
is the mean field assumption, which assumes that Qi (z) gives a distribution
with independent coordinates, or in other words, Qi can be decomposed into
Qi (z) = Q1i (z1 ) · · · Qik (zk ). There are tremendous applications of mean field
assumptions to learning generative models with discrete latent variables, and
we refer to Blei, Kucukelbir, and McAuliffe for a survey of these models and
their impact to a wide range of applications including computational biology,
computational neuroscience, social sciences. We will not get into the details about
the discrete latent variable cases, and our main focus is to deal with continuous
latent variables, which requires not only mean field assumptions, but additional
techniques.
When z ∈ Rk is a continuous latent variable, there are several decisions to
make towards successfully optimizing equation (22.3). First we need to give a
succinct representation of the distribution Qi because it is over an infinite number
of points. A natural choice is to assume Qi is a Gaussian distribution with some
mean and variance. We would also like to have more succinct representation of
the means of Qi of all the examples. Note that Qi (z(i) ) is supposed to approximate
p(z(i) | x (i) ; θ ). It would make sense let all the means of the Qi ’s be some function
of x (i) . Concretely, let q(·; φ), v(·; φ) be two functions that map from dimension d
to k, which are parameterized by φ and ψ, we assume that:
Here diag(w) means the k × k matrix with the entries of w ∈ Rk on the diagonal.
In other words, the distribution Qi is assumed to be a Gaussian distribution with
independent coordinates, and the mean and standard deviations are governed
by q and v. Often in variational auto-encoder, q and v are chosen to be neural
networks.2 In recent deep learning literature, often q,v are called encoder (in the 2
q and v can also share parameters.
sense of encoding the data into latent code), whereas g(z; θ ) if often referred to We sweep this level of details under
the rug in this note.
as the decoder.
We remark that Qi of such form in many cases are very far from a good ap-
proximation of the true posterior distribution. However, some approximation is
necessary for feasible optimization. In fact, the form of Qi needs to satisfy other
requirements (which happened to be satisfied by the form 22.4)
Before optimizing the ELBO, let’s first verify whether we can efficiently evaluate
the value of the ELBO for fixed Q of the form 22.4 and θ. We rewrite the ELBO as
a function of φ, ψ, θ by:
" #
n
p ( x (i ) , z (i ) ; θ )
ELBO(φ, ψ, θ ) = ∑ Ez(i) ∼Q log , (22.5)
i =1
i Q i ( z (i ) )
where Qi = N (q( x (i) ; φ), diag(v( x (i) ; ψ))2 ). Note that to evaluate Qi (z(i) ) inside
the expectation, we should be able to compute the density of Qi . To estimate
the expectation Ez(i) ∼Q , we should be able to sample from distribution Qi so
i
that we can build an empirical estimator with samples. It happens that for Gaus-
sian 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 Gaussian mixture model
in section 20.1, 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(φ, ψ, θ )
But computing the gradient over φ and ψ is tricky because the sampling dis-
tribution Qi depends on φ and ψ. (Abstractly speaking, the issue we face can be
simplified as the problem of computing the gradient 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:
Here x y denotes the entry-wise product of two vectors of the same dimen-
sion. 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:
" # " #
p ( x (i ) , z (i ) ; θ ) p ( x (i ) , q ( x (i ) ; φ ) + v ( x (i ) ; ψ ) ξ (i ) ; θ )
Ez (i ) ∼ Q log = Eξ (i) ∼N (0,1) log (22.10)
i Q i ( z (i ) ) Q i ( q ( x (i ) ; φ ) + v ( x (i ) ; ψ ) ξ (i ) )
It follows that:
" #
p ( x (i ) , z (i ) ; θ )
∇φ Ez(i) ∼Q log (22.11)
i Q i ( z (i ) )
" #
p ( x (i ) , q ( x (i ) ; φ ) + v ( x (i ) ; ψ ) ξ (i ) ; θ )
= ∇φ Eξ (i) ∼N (0,1) log (22.12)
Q i ( q ( x (i ) ; φ ) + v ( x (i ) ; ψ ) ξ (i ) )
" #
p ( x (i ) , q ( x (i ) ; φ ) + v ( x (i ) ; ψ ) ξ (i ) ; θ )
= Eξ (i) ∼N (0,1) ∇φ log (22.13)
Q i ( q ( x (i ) ; φ ) + v ( x (i ) ; ψ ) ξ (i ) )
we would find that the matrix Σ is singular. This means that Σ−1 does not exist, and
1/|Σ|1/2 = 1/0. But both of these terms are needed in computing the usual density
of a multivariate Gaussian distribution. Another way of stating this difficulty is
that maximum likelihood estimates of the parameters result in a Gaussian that
places all of its probability in the affine space spanned by the data,4 and this 4
This is the set of points x satisfy-
ing x = ∑in=1 αi x (i) , for some αi ’s
corresponds to a singular covariance matrix.
so that ∑in=1 αi = 1.
More generally, unless n exceeds d by some reasonable amount, the maximum
likelihood estimates of the mean and covariance may be quite poor. Nonetheless,
we would still like to be able to fit a reasonable Gaussian model to the data, and
perhaps capture some interesting covariance structure in the data. How can we
do this?
In the next section, we begin by reviewing two possible restrictions on Σ that
allow us to fit Σ with small amounts of data but neither will give a satisfactory
solution to our problem. We next discuss some properties of Gaussians that will
be needed later; specifically, how to find marginal and conditonal distributions of
Gaussians. Finally, we present the factor analysis model, and EM for it.
23 Restrictions of Σ
If we do not have sufficient data to fit a full covariance matrix, we may place
some restrictions on the space of matrices Σ that we will consider. For instance,
we may choose to fit a covariance matrix Σ that is diagonal. In this setting, the
reader may easily verify that the maximum likelihood estimate of the covariance
matrix is given by the diagonal matrix Σ satisfying
n
1
∑ (xj
(i )
Σ jj = − µ j )2 .
n i =1
Thus, Σ jj is just the empirical estimate of the variance of the j-th coordinate of the
data.
Recall that the contours of a Gaussian density are ellipses. A diagonal Σ corre-
sponds to a Gaussian where the major axes of these ellipses are axis-aligned.
Sometimes, we may place a further restriction on the covariance matrix that not
only must it be diagonal, but its diagonal entries must all be equal. In this setting,
we have Σ = σ2 I, where σ2 is the parameter under our control. The maximum
likelihood estimate of σ2 can be found to be:
d n
1
∑ ∑ (xj
(i )
σ2 = − µ j )2 .
nd j =1 i =1
This model corresponds to using Gaussians whose densities have contours that
are circles (in 2 dimensions; or spheres/hyperspheres in higher dimensions).
If we are fitting a full, unconstrained, covariance matrix Σ to data, it is necessary
that n ≥ d + 1 in order for the maximum likelihood estimate of Σ not to be singular.
Under either of the two restrictions above, we may obtain non-singular Σ when
n ≥ 2.
However, restricting Σ to be diagonal also means modeling the different coor-
dinates xi , x j of the data as being uncorrelated and independent. Often, it would
be nice to be able to capture some interesting correlation structure in the data. If
we were to use either of the restrictions on Σ described above, we would therefore
fail to do so. In this set of notes, we will describe the factor analysis model, which
uses more parameters than the diagonal Σ and captures some correlations in the
data, but also without having to fit a full covariance matrix.
135
Before describing factor analysis, we digress to talk about how to find conditional
and marginal distributions of random variables with a joint multivariate Gaussian
distribution.
Suppose we have a vector-valued random variable
" #
x
x= 1 ,
x2
Here, µ1 ∈ Rr , µ2 ∈ Rs , Σ11 ∈ Rr×r , Σ12 ∈ Rr×s , and so on. Note that since
covariance matrices are symmetric, Σ12 = Σ21 >.
Cov( x ) = Σ
" #
Σ11 Σ12
=
Σ21 Σ22
= E[( x − µ)( x − µ)> ]
x1 − µ1 >
h i
= E ( xx1 − µ1
) ( )
2 − µ2 x2 − µ2
" #
( x1 − µ1 )( x1 − µ1 )> ( x1 − µ1 )( x2 − µ2 )>
=E .
( x2 − µ2 )( x1 − µ1 )> ( x2 − µ2 )( x2 − µ2 )>
Matching the upper-left subblocks in the matrices in the second and the last lines
above gives the result.
When we work with the factor analysis model in the next section, these formulas
for finding conditional and marginal distributions of Gaussians will be very
useful.
z ∼ N (0, I )
x | z ∼ N (µ + Λz, Ψ)
Here, the parameters of our model are the vector µ ∈ Rd , the matrix Λ ∈ Rd×k ,
and the diagonal matrix Ψ ∈ Rd×d . The value of k is usually chosen to be smaller
than d.
Thus, we imagine that each datapoint x (i) is generated by sampling a k dimen-
sion multivariate Gaussian z(i) . Then, it is mapped to a d-dimensional affine space
of Rd by computing µ + Λz(i) . Lastly, x (i) is generated by adding covariance Ψ
noise to µ + Λz(i) .
Equivalently (convince yourself that this is the case), we can therefore also
define the factor analysis model according to
z ∼ N (0, I )
e ∼ N (0, Ψ)
x = µ + Λz + e
137
In the last step, we used the fact that E[zz> ] = Cov(z) (since z has zero mean),
and E[ze> ] = E[z]E[e> ] = 0 (since z and e are independent, and hence the
expectation of their product is the product of their expectations). Similarly, we
can find Σ xx as follows:
Hence, we also see that the marginal distribution of x is given by x ∼ N (µ, ΛΛ> +
Ψ). Thus, given a training set x (i) ; i = 1, . . . , n, we can write down the log likeli-
hood of the parameters:
n
1 1 (i )
`(µ, Λ, Ψ) = log ∏ d/2 | ΛΛ> + Ψ |1/2
> >
exp − ( x − µ) (ΛΛ + Ψ) ( x − µ) −1 ( i )
i =1 (2π )
2
(25.2)
To perform maximum likelihood estimation, we would like to maximize this
quantity with respect to the parameters. But maximizing this formula explicitly
is hard (try it yourself), and we are aware of no algorithm that does so in closed-
form. So, we will instead use to the EM algorithm. In the next section, we derive
EM for factor analysis.
The derivation for the E-step is easy. We need to compute Qi (z(i) ) = p(z(i) |
x (i) ; µ, Λ, Ψ).
By substituting the distribution given in equation (25.1) into the
formulas 24.1-24.2 used for finding the conditional distribution of a Gaussian,
we find that z(i) | x (i) ; µ, Λ, Ψ ∼ N (µz(i) | x(i) , Σz(i) | x(i) ), where
So, using these definitions for µz(i) | x(i) and Σz(i) | x(i) , we have:
1 1 (i ) > −1
Q i ( z (i ) ) = exp − ( z − µ z (i ) | x (i ) ) Σ ( z (i )
− µ z (i ) | x (i ) )
(2π )k/2 |Σz(i) | x(i) |1/2 2 z (i ) | x (i )
with respect to the parameters µ, Λ, Ψ. We will work out only the optimization
with respect to Λ, and leave the derivations of the updates for µ and Ψ as an
exercise to the reader.
We can simplify equation (26.1) as follows:
n Z
∑ (i )
Qi (z(i) )[log p( x (i) | z(i) ; µ, Λ, Ψ) + log p(z(i) ) − log Qi (z(i) )]dz(i) (26.2)
i =1 z
n
= ∑ Ez(i) ∼Qi [log p(x(i) | z(i) ; µ, Λ, Ψ) + log p(z(i) ) − log Qi (z(i) )] (26.3)
i =1
Here, the ‘‘z(i) ∼ Qi ’’ subscript indicates that the expectation is with respect to
z(i) drawn from Qi . In the subsequent development, we will omit this subscript
when there is no risk of ambiguity. Dropping terms that do not depend on the
parameters, we find that we need to maximize:
n
∑ E[log p(x(i) | z(i) ; µ, Λ, Ψ)]
i =1
n
1 1
= ∑E log
(2π )d/2 |Ψ|1/2
exp − ( x (i) − µ − Λz(i) )> Ψ−1 ( x (i) − µ − Λz(i) )
2
i =1
n
1 n 1
= ∑ E − log |Ψ| − log(2π ) − ( x (i) − µ − Λz(i) )> Ψ−1 ( x (i) − µ − Λz(i) )
i =1
2 2 2
Let’s maximize this with respect to Λ. Only the last term above depends on Λ.
Taking derivatives, and using the facts that tr( a) = a (for a ∈ R), tr( AB) =
tr( BA), and ∇ A tr( ABA> C ) = CAB + C > AB> , we get:
n
1
∇Λ ∑ −E ( x (i) − µ − Λz(i) )> Ψ−1 ( x (i) − µ − Λz(i) )
i =1
2
n
1 > >
= ∑ ∇Λ E − tr( z(i) Λ> Ψ−1 Λz(i) ) + tr(z(i) Λ> Ψ−1 ( x (i) − µ))
i =1
2
n
1 > >
= ∑ ∇Λ E − tr( Λ> Ψ−1 Λz(i) z(i) ) + tr(Λ> Ψ−1 ( x (i) − µ)z(i) )
i =1
2
n
> >
h i
= ∑ E −Ψ−1 Λz(i) z(i) + Ψ−1 ( x (i) − µ)z(i)
i =1
It is interesting to note the close relationship between this equation and the normal
equation that we’d derived for least squares regression,
The analogy is that here, the x’s are a linear function of the z’s (plus noise). Given
the ‘‘guesses’’ for z that the E-step has found, we will now try to estimate the
unknown linearity Λ relating the x’s and z’s. It is therefore no surprise that we
obtain something similar to the normal equation. There is, however, one important
difference between this and an algorithm that performs least squares using just
the ‘‘best guesses’’ of the z’s; we will see this difference shortly.
To complete our M-step update, let’s work out the values of the expectations
in equation (26.4). From our definition of Qi being Gaussian with mean µz(i) | x(i)
and covariance Σz(i) | x(i) , we easily find
h >i
Ez (i ) ∼ Q z ( i ) = µ > z (i ) | x (i )
i
>
h i
Ez (i ) ∼ Q z ( i ) z ( i ) = µ z (i ) | x (i ) µ >
z (i ) | x (i )
+ Σ z (i ) | x (i )
i
The latter comes from the fact that, for a random variable Y, Cov(Y ) = E[YY > ] −
E[Y ]E[Y ]> , and hence E[YY > ] = E[Y ]E[Y ]> + Cov(Y ). Substituting this back
into equation (26.4), we get the M-step update for Λ:
! ! −1
n n
Λ= ∑ (x(i) − µ)µ>z(i) |x(i) ∑ µz(i) |x(i) µ>z(i) |x(i) + Σz(i) |x(i) (26.5)
i =1 i =1
It is important to note the presence of the Σz(i) | x(i) on the right hand side of this
equation. This is the covariance in the posterior distribution p(z(i) | x (i) ) of z(i)
given x (i) , and the M-step must take into account this uncertainty about z(i) in the
posterior. A common mistake in deriving EM is to assume that in the E-step, we
need to calculate only expectation E[z] of the latent random variable z, and then
plug that into the optimization in the M-step everywhere z occurs. While this
worked for simple problems such as the mixture of Gaussians, in our derivation
for factor analysis, we needed E[zz> ] as well as E[z]; and as we saw, E[zz> ] and
E[z]E[z]> differ by the quantity Σz| x . Thus, the M-step update must take into
account the covariance of z in the posterior distribution p(z(i) | x (i) ).
Lastly, we can also find the M-step optimizations for the parameters µ and Ψ.
It is not hard to show that the first is given by
n
1
µ=
n ∑ x (i ) .
i =1
Since this doesn’t change as the parameters are varied (i.e., unlike the update for
Λ, the right hand side does not depend on Qi (z(i) ) = p(z(i) | x (i) ; µ, Λ, Ψ), which
in turn depends on the parameters), this can be calculated just once and needs
not be further updated as the algorithm is run. Similarly, the diagonal Ψ can be
found by calculating
n
1 > (i )>
Φ=
n ∑ x (i ) x (i ) − x (i ) µ >( i
z |x ) ( i ) Λ >
− Λµ ( i
z |x ) ( i ) x + Λ µ ( i
z |x ) ( i ) µ >
( i
z |x ) ( i ) + Σ ( i
z |x ) ( i ) Λ> ,
i =1
and setting Ψii = Φii (i.e., letting Ψ be the diagonal matrix containing only the
diagonal entries of Φ).
and variance 1. We do this by subtracting the mean and dividing by the empirical
standard deviation:
(i )
(i )
xj − µj
xj ←
σj
(i ) (i )
where µ j = n1 ∑in=1 x j and σj2 = n1 ∑in=1 ( x j − µ j )2 are the mean variance of
feature j, respectively.
Subtracting µ j zeros out the mean and may be omitted for data known to have
zero mean (for instance, time series corresponding to speech or other acoustic
signals). Dividing by the standard deviation σj rescales each coordinate to have
unit variance, which ensures that different attributes are all treated on the same
‘‘scale.’’ For instance, if x1 was cars’ maximum speed in mph (taking values in the
high tens or low hundreds) and x2 were the number of seats (taking values around
2-4), then this renormalization rescales the different attributes to make them more
comparable. This rescaling may be omitted if we had a priori knowledge that the
different attributes are all on the same scale. One example of this is if each data
(i )
point represented a grayscale image, and each x j took a value in {0, 1, . . . , 255}
corresponding to the intensity value of pixel j in image i.
Now, having normalized our data, how do we compute the ‘‘major axis of vari-
ation’’ u—that is, the direction on which the data approximately lies? One way is
to pose this problem as finding the unit vector u so that when the data is projected
onto the direction corresponding to u, the variance of the projected data is maxi-
mized. Intuitively, the data starts off with some amount of variance/information
in it. We would like to choose a direction u so that if we were to approximate the
data as lying in the direction/subspace corresponding to u, as much as possible
of this variance is still retained. Consider the following dataset, on which we have
already carried out the normalization steps:
Now, suppose we pick u to correspond the the direction shown in the figure
below. The circles denote the projections of the original data onto this line.
We see that the projected data still has a fairly large variance, and the points
tend to be far from zero. In contrast, suppose had instead picked the following
direction:
Here, the projections have a significantly smaller variance, and are much closer
to the origin.
We would like to automatically select the direction u corresponding to the first
of the two figures shown above. To formalize this, note that given a unit vector u
and a point x, the length of the projection of x onto u is given by x > u. I.e., if x (i)
is a point in our dataset (one of the crosses in the plot), then its projection onto u
(the corresponding circle in the figure) is distance x > u from the origin. Hence, to
maximize the variance of the projections, we would like to choose a unit-length u
so as to maximize:
n n
1 > 1 >
n ∑ ( x (i ) u )2 = n ∑ u > x (i ) x (i ) u
i =1 i =1
!
n
1 (i ) (i )>
=u >
n ∑x x u
i =1
We easily recognize that the maximizing this subject to kuk2 = 1 gives the prin-
>
cipal eigenvector of Σ = n1 ∑in=1 x (i) x (i) , which is just the empirical covariance
matrix of the data (assuming it has zero mean).1 1
If you haven’t seen this before, try
To summarize, we have found that if we wish to find a 1-dimensional subspace using the method of Lagrange mul-
tipliers to maximize u> Σu subject
with with to approximate the data, we should choose u to be the principal eigen- to that u> u = 1. You should be
vector of Σ. More generally, if we wish to project our data into a k-dimensional able to show that Σu = λu, for
some λ, which implies u is an eigen-
subspace (k < d), we should choose u1 , . . . , uk to be the top k eigenvectors of Σ. vector of Σ, with eigenvalue λ.
The ui ’s now form a new, orthogonal basis for the data.2 2
Because Σ is symmetric, the ui ’s
Then, to represent x (i) in this basis, we need only compute the corresponding will (or always can be chosen to be)
orthogonal toeach other.
vector
u1> x (i)
> (i )
u2 x
y (i ) =
.. ∈ R .
k
.
u>
k x
(i )
Thus, whereas x (i) ∈ Rd , the vector y(i) now gives a lower, k-dimensional, approx-
imation/representation for x (i) . PCA is therefore also referred to as a dimension-
ality reduction algorithm. The vectors u1 , . . . , uk are called the first k principal
components of the data.
Remark. Although we have shown it formally only for the case of k = 1, using
well-known properties of eigenvectors it is straightforward to show that of all
possible orthogonal bases u1 , . . . , uk , the one that we have chosen maximizes
∑i ky(i) k22 . Thus, our choice of a basis preserves as much variability as possible in
the original data.
In problem set 4, you will see that PCA can also be derived by picking the basis
that minimizes the approximation error arising from projecting the data onto the
k-dimensional subspace spanned by them.
PCA has many applications; we will close our discussion with a few examples.
First, compression—representing x (i) ’s with lower dimension y(i) ’s—is an obvious
application. If we reduce high dimensional data to k = 2 or 3 dimensions, then
we can also plot the y(i) ’s to visualize the data. For instance, if we were to reduce
our automobiles data to 2 dimensions, then we can plot it (one point in our plot
would correspond to one car type, say) to see what cars are similar to each other
and what groups of cars may cluster together.
Another standard application is to preprocess a dataset to reduce its dimension
before running a supervised learning learning algorithm with the x (i) ’s as inputs.
Apart from computational benefits, reducing the data’s dimension can also reduce
the complexity of the hypothesis class considered and help avoid overfitting
(e.g., linear classifiers over lower dimensional input spaces will have smaller VC
dimension).
Lastly, as in our RC pilot example, we can also view PCA as a noise reduc-
tion algorithm. In our example it, estimates the intrinsic ‘‘piloting karma’’ from
the noisy measures of piloting skill and enjoyment. In class, we also saw the
application of this idea to face images, resulting in eigenfaces method. Here,
each point x (i) ∈ R100×100 was a 10000 dimensional vector, with each coordinate
corresponding to a pixel intensity value in a 100 × 100 image of a face. Using PCA,
we represent each image x (i) with a much lowerdimensional y(i) . In doing so, we
hope that the principal components we found retain the interesting, systematic
variations between faces that capture what a person really looks like, but not the
‘‘noise’’ in the images introduced by minor lighting variations, slightly different
imaging conditions, and so on. We then measure distances between faces i and j
by working in the reduced dimension, and computing ky(i) − y( j) k2 . This resulted
in a surprisingly good face-matching and retrieval algorithm.
very different.
As a motivating example, consider the ‘‘cocktail party problem.’’ Here, d
speakers are speaking simultaneously at a party, and any microphone placed in
the room records only an overlapping combination of the d speakers’ voices. But
lets say we have d different microphones placed in the room, and because each
microphone is a different distance from each of the speakers, it records a different
combination of the speakers’ voices. Using these microphone recordings, can we
separate out the original d speakers’ speech signals?
To formalize this problem, we imagine that there is some data s ∈ Rd that is
generated via d independent sources. What we observe is
x = As,
where A is an unknown square matrix called the mixing matrix. Repeated obser-
vations gives us a dataset x (i) ; i = 1, . . . , n, and our goal is to recover the sources
s(i) that had generated our data ( x (i) = As(i) ).
(i )
In our cocktail party problem, s(i) is an d-dimensional vector, and s j is the
sound that speaker j was uttering at time i. Also, x (i) in an d-dimensional vector,
(i )
and x j is the acoustic reading recorded by microphone j at time i.
Let W = A−1 be the unmixing matrix. Our goal is to find W, so that given our
microphone recordings x (i) , we can recover the sources by computing s(i) = Wx (i) .
For notational convenience, we also let wi> denote the i-th row of W, so that
— w1> —
..
W= .
— wd —>
(i )
Thus, wi ∈ Rd , and the j-th source can be recovered as s j = w> (i )
j x .
148 chapter 27. ica ambiguities
27 ICA ambiguities
Before moving on to derive the ICA algorithm proper, we first digress briefly
to talk about the effect of linear transformations on densities.
Suppose a random variable s is drawn according to some density ps (s). For
simplicity, assume for now that s ∈ R is a real number. Now, let the random
variable x be defined according to x = As (here, x ∈ R, A ∈ R). Let p x be the
density of x. What is p x ?
Let W = A−1 . To calculate the ‘‘probability’’ of a particular value of x, it
is tempting to compute s = Wx, then then evaluate ps at that point, and con-
clude that ‘‘p x ( x ) = ps (Wx ).’’ However, this is incorrect. For example, let s ∼
Uniform[0, 1], so ps (s) = 1{0 ≤ s ≤ 1}. Now, let A = 2, so x = 2s. Clearly,
x is distributed uniformly in the interval [0, 2]. Thus, its density is given by
p x ( x ) = (0.5)1{0 ≤ x ≤ 2}. This does not equal ps (Wx ), where W = 0.5 = A−1 .
Instead, the correct formula is p x ( x ) = ps (Wx )|W |.
More generally, if s is a vector-valued distribution with density ps , and x = As
for a square, invertible matrix A, then the density of x is given by
p x ( x ) = ps (Wx ) · |W |,
where W = A−1 .
Remark. If you’re seen the result that A maps [0, 1]d to a set of volume | A|,
then here’s another way to remember the formula for p x given above, that also
generalizes our previous 1-dimensional example. Specifically, let A ∈ Rd×d be
given, and let W = A−1 as usual. Also let C1 = [0, 1]d be the d-dimensional
hypercube, and define C2 = { As : s ∈ C1 } ⊆ Rd to be the image of C1 under the
mapping given by A. Then it is a standard result in linear algebra (and, indeed,
one of the ways of defining determinants) that the volume of C2 is given by | A|.
Now, suppose s is uniformly distributed in [0, 1]d , so its density is ps (s) = 1{s ∈
C1 }. Then clearly x will be uniformly distributed in C2 . Its density is therefore
found to be p x ( x ) = 1{ x ∈ C2 }/ vol(C2 ) (since it must integrate over C2 to
1). But using the fact that the determinant of the inverse of a matrix is just the
inverse of the determinant, we have 1/ vol(C2 ) = 1/| A| = | A−1 | = |W |. Thus,
p x ( x ) = 1{ x ∈ C2 }|W | = 1{Wx ∈ C1 }|W | = ps (Wx )|W |.
29 ICA algorithm
d
p(s) = ∏ p s ( s j ).
j =1
d
p( x ) = ∏ ps (w>j x) · |W |
j =1
All that remains is to specify a density for the individual sources ps . Recall that,
given a real-valued random variable z, its cumulative distribution function (cdf) F
Rz
is defined by F (z0 ) = P(z ≤ z0 ) = −0∞ pz (z)dz and the density is the derivative
of the cdf: pz (z) = F 0 (z).
Thus, to specify a density for the si ’s, all we need to do is to specify some cdf for
it. A cdf has to be a monotonic function that increases from zero to one. Following
our previous discussion, we cannot choose the Gaussian cdf, as ICA doesn’t work
on Gaussian data. What we’ll choose instead as a reasonable ‘‘default’’ cdf that
slowly increases from 0 to 1, is the sigmoid function g(s) = 1/(1 + e−s ). Hence,
p s ( s ) = g 0 ( s ) .1 1
If you have prior knowledge that
The square matrix W is the parameter in our model. Given a training set the sources’ densities take a certain
form, then it is a good idea to sub-
{ x i) ; i = 1, . . . , n}, the log likelihood is given by
(
stitute that in here. But in the ab-
! sence of such knowledge, the sig-
n d moid function can be thought of as
`(W ) = ∑ ∑ log g0 (w>j x(i) ) + log |W | . a reasonable default that seems to
i =1 j =1 work well for many problems. Also,
the presentation here assumes that
We would like to maximize this in terms W. By taking derivatives and using the either the data x (i) has been pre-
processed to have zero mean, or
fact (from the first set of notes) that ∇W |W | = |W |(W −1 )> , we easily derive a that it can naturally be expected to
stochastic gradient ascent learning rule. For a training example x (i) , the update have zero mean (such as acoustic
rule is: signals). This is necessary because
our assumption that ps (s) = g0 (s)
1 − 2g(w1> x (i) )
implies E[s] = 0 (the derivative of
1 − 2g(w2> x (i) ) (i)>
> −1
the logistic function is a symmetric
W := W + α .. x
+ (W ) , function, and hence gives a density
. corresponding to a random vari-
>
1 − 2g(wd x )( i ) able with zero mean), which im-
plies E[ x ] = E[ As] = 0.
mimic the labels y given in the training set. In that setting, the labels gave an
unambiguous ‘‘right answer’’ for each of the inputs x. In contrast, for many
sequential decision making and control problems, it is very difficult to provide
this type of explicit supervision to a learning algorithm. For example, if we have
just built a four-legged robot and are trying to program it to walk, then initially
we have no idea what the ‘‘correct’’ actions to take are to make it walk, and so do
not know how to provide explicit supervision for a learning algorithm to try to
mimic.
In the reinforcement learning framework, we will instead provide our algo-
rithms only a reward function, which indicates to the learning agent when it is
doing well, and when it is doing poorly. In the four-legged walking example, the
reward function might give the robot positive rewards for moving forwards, and
negative rewards for either moving backwards or falling over. It will then be the
learning algorithm’s job to figure out how to choose actions over time so as to
obtain large rewards.
Reinforcement learning has been successful in applications as diverse as au-
tonomous helicopter flight, robot legged locomotion, cell-phone network routing,
marketing strategy selection, factory control, and efficient web-page indexing.
Our study of reinforcement learning will begin with a definition of the Markov
decision processes (MDP), which provides the formalism in which RL problems
are usually posed.
30 Markov decision processes
• A is a set of actions. (For example, the set of all possible directions in which
you can push the helicopter’s control sticks.)
• Psa are the state transition probabilities. For each state s ∈ S and action a ∈ A,
Psa is a distribution over the state space. We’ll say more about this later, but
briefly, Psa gives the distribution over what states we will transition to if we
take action a in state s.
Or, when we are writing rewards as a function of the states only, this becomes
For most of our development, we will use the simpler state-rewards R(s), though
the generalization to state-action rewards R(s, a) offers no special difficulties.
Our goal in reinforcement learning is to choose actions over time so as to
maximize the expected value of the total payoff:
h i
E R(s0 ) + γR(s1 ) + γ2 R(s2 ) + · · ·
V π (s) is simply the expected sum of discounted rewards upon starting in state s,
and taking actions according to π.1 1
This notation in which we condi-
Given a fixed policy π, its value function V π satisfies the Bellman equations: tion on π isn’t technically correct
because π isn’t a random variable,
but this is quite standard in the lit-
V π (s) = R(s) + γ ∑
0
Psπ (s) (s0 )V π (s0 ). erature.
s ∈S
This says that the expected sum of discounted rewards V π (s) for starting in s
consists of two terms: First, the immediate reward R(s) that we get right away
simply for starting in state s, and second, the expected sum of future discounted
rewards. Examining the second term in more detail, we see that the summation
term above can be rewritten Es0 ∼ Psπ (s) [V π (s0 )]. This is the expected sum of dis-
counted rewards for starting in state s0 , where s0 is distributed according Psπ (s) ,
which is the distribution over where we will end up after taking the first action
π (s) in the MDP from state s. Thus, the second term above gives the expected
sum of discounted rewards obtained after the first step in the MDP.
In other words, this is the best possible expected sum of discounted rewards that
can be attained using any policy. There is also a version of Bellman’s equations
for the optimal value function:
The first term above is the immediate reward as before. The second term is the
maximum over all actions a of the expected future sum of discounted rewards
we’ll get upon after action a. You should make sure you understand this equation
and see why it makes sense. (A derivation for equation (30.2) and the equa-
tion (30.3) below are given in chapter 35) We also define a policy π ∗ : S 7→ A as
follows:
π ∗ (s) = arg max ∑ Psa (s0 )V ∗ (s0 ). (30.3)
a∈ A s0 ∈S
Note that π ∗ (s) gives the action a that attains the maximum in the ‘‘max’’ in
equation (30.2).
It is a fact that for every state s and every policy π, we have
∗
V ∗ ( s ) = V π ( s ) ≥ V π ( s ).
∗
The first equality says that the V π , the value function for π ∗ , is equal to the
optimal value function V ∗ for every state s. Further, the inequality above says
that π ∗ ’s value is at least as large as the value of any other other policy. In other
words, π ∗ as defined in equation (30.3) is the optimal policy.
Note that π ∗ has the interesting property that it is the optimal policy for all
states s. Specifically, it is not the case that if we were starting in some state s then
there’d be some optimal policy for that state, and if we were starting in some other
state s0 then there’d be some other policy that’s optimal policy for s0 . The same
policy π ∗ attains the maximum in equation (30.1) for all states s. This means that
we can use the same policy π ∗ no matter what the initial state of our MDP is.
We now describe two efficient algorithms for solving finite-state MDPs. For now,
we will consider only MDPs with finite state and action spaces (|S| < ∞, | A| < ∞).
In this section, we will also assume that we know the state transition probabilities
{ Psa } and the reward function R.
The first algorithm, value iteration, is as follows:
repeat
for every state s, update do
end for
until convergence
repeat
Let V := V π . . typically by linear system solver
for every state s, update do
end for
until convergence
Thus, the inner-loop repeatedly computes the value function for the current
policy, and then updates the policy using the current value function. (The policy
π found in step (b) is also called the policy that is greedy with respect to V.)
Note that step (a) can be done via solving Bellman’s equations as described
earlier, which in the case of a fixed policy, is just a set of |S| linear equations in |S|
variables.
After at most a finite number of iterations of this algorithm, V will converge to
V ∗ , and π will converge to π ∗ .1 1
Note that value iteration cannot
Both value iteration and policy iteration are standard algorithms for solving reach the exact V ∗ in a finite num-
ber of iterations, whereas policy it-
MDPs, and there isn’t currently universal agreement over which algorithm is eration with an exact linear system
better. For small MDPs, policy iteration is often very fast and converges with solver, can. This is because when
the actions space and policy space
very few iterations. However, for MDPs with large state spaces, solving for V π are discrete and finite, and once the
explicitly would involve solving a large system of linear equations, and could be policy reaches the optimal policy
difficult (and note that one has to solve the linear system multiple times in policy in policy iteration, then it will not
change at all. On the other hand,
iteration). In these problems, value iteration may be preferred. For this reason, even though value iteration will
in practice value iteration seems to be used more often than policy iteration. For converge to the V ∗ , but there is al-
ways some non-zero error in the
some more discussions on the comparison and connection of value iteration and learned value function.
policy iteration, please see chapter 34.
So far, we have discussed MDPs and algorithms for MDPs assuming that the
state transition probabilities and rewards are known. In many realistic problems,
we are not given state transition probabilities and rewards explicitly, but must
instead estimate them from data. (Usually, S, A, and γ are known.)
For example, suppose that, for the inverted pendulum problem (see problem
set 4), we had a number of trials in the MDP, that proceeded as follows:
(1) (1) (1) (1)
(1) a0 1 (1) a
2 (1) a (1) a3
s0 −→ s1 −→ s2 −→ s3 −→ . . .
(2) (2) (2) (2)
(2) a0 1 (2) a
2 (2) a (2) a3
s0 −→ s1 −→ s2 −→ s3 −→ . . .
...
( j) ( j)
Here, si is the state we were at time i of trial j, and ai is the corresponding
action that was taken from that state. In practice, each of the trials above might
be run until the MDP terminates (such as if the pole falls over in the inverted
pendulum problem), or it might be run for some large but finite number of
timesteps.
Given this ‘‘experience’’ in the MDP consisting of a number of trials, we can
then easily derive the maximum likelihood estimates for the state transition
probabilities:
# times we took action a in state s and got to s0
Psa (s0 ) = (32.1)
# times we took action a in state s
Or, if the ratio above is ‘‘0/0’’—corresponding to the case of never having taken
action a in state s before—the we might simply estimate Psa (s0 ) to be 1/|S| (i.e.,
estimate Psa to be the uniform distribution over all states.)
Note that, if we gain more experience (observe more trials) in the MDP, there
is an efficient way to update our estimated state transition probabilities using the
new experience. Specifically, if we keep around the counts for both the numerator
and denominator terms of equation (32.1), then as we observe more trials, we
can simply keep accumulating those counts. Computing the ratio of these counts
then given our estimate of Psa .
161
1. Initialize π randomly.
2. Repeat:
We note that, for this particular algorithm, there is one simple optimization
that can make it run much more quickly. Specifically, in the inner loop of the
algorithm where we apply value iteration, if instead of initializing value iteration
with V = 0, we initialize it with the solution found during the previous iteration
of our algorithm, then that will provide value iteration with a much better initial
starting point and make it converge more quickly.
So far, we’ve focused our attention on MDPs with a finite number of states. We
now discuss algorithms for MDPs that may have an infinite number of states.
For example, for a car, we might represent the state as ( x, y, θ, ẋ, ẏ, θ̇ ), compris-
ing its position ( x, y); orientation θ; velocity in the x and y directions ẋ and ẏ;
and angular velocity θ̇. Hence, S = R6 is an infinite set of states, because there
is an infinite number of possible positions and orientations for the car.1 Simi- 1
Technically, θ is an orientation and
larly, the inverted pendulum you saw in PS4 has states ( x, θ, ẋ, θ̇ ), where θ is so the range of θ is better written
θ ∈ [−π, π ) than θ ∈ R; but for
the angle of the pole. And, a helicopter flying in 3d space has states of the form our purposes, this distinction is not
( x, y, z, φ, θ, ψ, ẋ, ẏ, ż, φ̇, θ̇, ψ̇), where here the roll φ, pitch θ, and yaw ψ angles important.
specify the 3d orientation of the helicopter.
In this section, we will consider settings where the state space is S = Rd , and
describe ways for solving such MDPs.
33.1 Discretization
of the discretization intervals (i.e., that the value function is piecewise constant
in each of the gridcells).
To better understand the limitations of such a representation, consider a su-
pervised learning problem of fitting a function to this dataset:
Clearly, linear regression would do fine on this problem. However, if we instead
discretize the x-axis, and then use a representation that is piecewise constant in
each of the discretization intervals, then our fit to the data would look like this:
This piecewise constant representation just isn’t a good representation for
many smooth functions. It results in little smoothing over the inputs, and no
generalization over the different grid cells. Using this sort of representation, we
would also need a very fine discretization (very small grid cells) to get a good
approximation.
A second downside of this representation is called the curse of dimensionality.
Suppose S = Rd , and we discretize each of the d dimensions of the state into
k values. Then the total number of discrete states we have is kd . This grows
exponentially quickly in the dimension of the state space d, and thus does not
scale well to large problems. For example, with a 10d state, if we discretize each
state variable into 100 values, we would have 1001 0 = 102 0 discrete states, which
is far too many to represent even on a modern desktop computer.
As a rule of thumb, discretization usually works extremely well for 1d and
2d problems (and has the advantage of being simple and quick to implement).
Perhaps with a little bit of cleverness and some care in choosing the discretization
method, it often works well for problems with up to 4d states. If you’re extremely
clever, and somewhat lucky, you may even get it to work for some 6d problems.
But it very rarely works for problems any higher dimensional than that.
using an algorithm similar to linear regression. Here, the parameters of the model
are the matrices A and B, and we can estimate them using the data collected from
The main idea of fitted value iteration is that we are going to approximately
carry out this step, over a finite sample of states s(1) , . . . , s(n) . Specifically, we
will use a supervised learning algorithm—linear regression in our description
below—to approximate the value function as a linear or non-linear function of
the states:
V ( s ) = θ > φ ( s ).
Here, φ is some appropriate feature mapping of the states.
For each state s in our finite sample of n states, fitted value iteration will first
compute a quantity y(i) , which will be our approximation to R(s) + γ maxa Es0 ∼ Psa [V (s0 )]
(the right hand side of equation (33.3)). Then, it will apply a supervised learning
algorithm to try to get V (s) close to R(s) + γ maxa Es0 ∼ Psa [V (s0 )] (or, in other
words, to try to get V (s) close to y(i) ).
In detail, the algorithm is as follows:
2. Initialize θ := 0.
3. Repeat:
For i = 1, . . . , n
For each action a ∈ A
Sample s10 , . . . , s0k ∼ Ps( i)a (using a model of the MDP).
Set q( a) = 1
k ∑kj=1 R(s(i) ) + γV (s0j )
// Hence, q( a) is an estimate of R(s(i) ) + γEs0 ∼ P (i) [V (s0 )].
s a
Above, we had written out fitted value iteration using linear regression as
the algorithm to try to make V (s(i) ) close to y(i) . That step of the algorithm is
completely analogous to a standard supervised learning (regression) problem in
which we have a training set ( x (1) , y(1) ), ( x (2) , y(2) ), . . . , ( x (n) , y(n) ), and want to
learn a function mapping from x to y; the only difference is that here s plays the
role of x. Even though our description above used linear regression, clearly other
regression algorithms (such as locally weighted linear regression) can also be
used.
Unlike value iteration over a discrete set of states, fitted value iteration cannot
be proved to always to converge. However, in practice, it often does converge (or
approximately converge), and works well for many problems. Note also that if we
are using a deterministic simulator/model of the MDP, then fitted value iteration
can be simplified by setting k = 1 in the algorithm. This is because the expectation
in equation (33.3) becomes an expectation over a deterministic distribution, and
so a single example is sufficient to exactly compute that expectation. Otherwise, in
the algorithm above, we had to draw k samples, and average to try to approximate
that expectation (see the definition of q( a), in the algorithm pseudo-code).
Finally, fitted value iteration outputs V, which is an approximation to V ∗ . This
implicitly defines our policy. Specifically, when our system is in some state s, and
we need to choose an action, we would like to choose the action
In other words, here we are just setting et = 0 (i.e., ignoring the noise in the
simulator), and setting k = 1. Equivalent, this can be derived from equation (33.4)
where here the expectation is over the random s0 ∼ Psa . So long as the noise terms
et are small, this will usually be a reasonable approximation.
However, for problems that don’t lend themselves to such approximations, hav-
ing to sample k| A| states using the model, in order to approximate the expectation
above, can be computationally expensive.
In the policy iteration, line 3 of algorithm 31.2, we typically use linear system
solver to compute V π . Alternatively, one can also the iterative Bellman updates,
similarly to the value iteration, to evaluate V π , as in the Procedure VE(·) in line 1
of algorithm 34.1 below. Here if we take option 1 in line 2 of the Procedure VE, then
the difference between the Procedure VE from the value iteration (algorithm 31.1)
is that on line 4, the procedure is using the action from π instead of the greedy
action.
Using the Procedure VE, we can build algorithm 34.1, which is a variant of
policy iteration that serves an intermediate algorithm that connects policy itera-
tion and value iteration. Here we are going to use option 2 in VE to maximize the
re-use of knowledge learned before. One can verify indeed that if we take k = 1
and use option 2 in line 2 in algorithm 34.1, then algorithm 34.1 is semantically
equivalent to value iteration (algorithm 31.2). In other words, both algorithm 34.1
and value iteration interleave the updates in equation (34.2) and equation (34.1).
algorithm 34.1 alternate between k steps of update equation (34.1) and one step of
equation (34.2), whereas value iteration alternates between 1 step of update equa-
tion (34.1) and one step of equation (34.2). Therefore generally algorithm 34.1
should not be faster than value iteration, because assuming that update equa-
tion (34.1) and equation (34.2) are equally useful and time-consuming, then the
optimal balance of the update frequencies could be just k = 1 or k ≈ 1.
On the other hand, if k steps of update equation (34.1) can be done much
faster than k times a single step of equation (34.1), then taking additional steps of
equation equation (34.1) in group might be useful. This is what policy iteration is
leveraging—the linear system solver can give us the result of Procedure VE with
k = ∞ much faster than using the Procedure VE for a large k. On the flip side,
when such a speeding-up effect no longer exists, e.g., when the state space is large
and linear system solver is also not fast, then value iteration is more preferable.
170 chapter 34. connections between policy and value iteration (optional)
end for
end for
return V
Require: hyperparameter k.
Initialize π randomly.
repeat
Let V = VE(π, k).
for every state s, update do
end for
until convergence
Here we give a derivation for the Bellman Equation given in chapter 30. Recall
that the value function for a policy π is defined as
h i
V π (s) = E R(s0 ) + γR(s1 ) + γ2 R(s2 ) + · · · | s0 = s, π .
Therefore, we have
h i
V π (s) = E R(s0 ) + γR(s1 ) + γ2 R(s2 ) + · · · | s0 = s, π
Now we derive the Bellman Equation for the optimal value function.
Here the fourth equality is because that for MDP, the optimal action at a later
state is independent of actions at previous states, hence the optimal policy at the
current state can be decomposed to an action followed by the optimal policy at
the new state.
A Lagrange Multipliers
From CS229 Spring 2021, Andrew
We consider a special case of Lagrange Multipliers for constrained optimization. Ng, Moses Charikar & Christopher
The class quickly sketched the ‘‘geometric’’ intuition for Lagrange multipliers, Ré, Stanford University.
L( x, µ) = f ( x ) + µ> ( Ax − b) in which µ ∈ Rn
We’ll show that the critical points of the constrained function f are critical points
of L( x, µ).
Finding the Space of Solutions. Assume the constraints are satisfiable, then let
x0 be such that Ax0 = b. Let rank( A) = r, then let {u1 , . . . , uk } be an orthonormal
basis for the null space of A in which k = d − r. Note if k = 0, then x0 is uniquely
defined. So we consider k > 0. We write this basis as a matrix:
U = [ u 1 , . . . , u k ] ∈ Rd × k
To make sure the types are clear: ∇y g(y) ∈ Rk , ∇ f (z) ∈ Rd and U ∈ Rd×k . In
both cases, 0 is the 0 vector in Rk .
The above condition says that if y is a critical point for g, then ∇ f ( x ) must
be orthogonal to U. However, U forms a basis for the null space of A and the
rowspace is orthogonal to it. In particular, any element of the rowspace can be
written z = A> µ ∈ Rd . We verify that z and u = Uy are orthogonal since:
The Clever Lagrangian. We now observe that the critical points of the Lagrangian
are (by differentiating and setting to 0)
∇ x L( x, µ) = ∇ f ( x ) + A> µ = 0
and
∇µ L( x, µ) = Ax − b = 0
The first condition is exactly the condition that x be a critical point in the way
we derived it above, and the second condition says that the constraint be satisfied.
Thus, if x is a critical point, there exists some µ as above, and ( x, µ) is a critical
point for L.
We give a simple example to show that you cannot just set the derivatives to Example A.1. Need for constrained
optimization.
0. Consider f ( x1 , x2 ) = x1 and g( x1 , x2 ) = x12 + x22 and so:
max f ( x ) subject to g( x ) = 1.
x
This is just a linear functional over the circle, and it is compact, so the function
must achieve a maximum value. Intuitively, we can see that (1, 0) is the
maximum possible value (and hence a critical point). Here, we have:
1 x
∇ f (x) = and ∇ g( x ) = 2 1
0 x2
We have seen so far how to solve classification (and other) problems when we have
a data representation already chosen. We now talk about a procedure, known as
boosting, which was originally discovered by Rob Schapire, and further developed
by Schapire and Yoav Freund, that automatically chooses feature representations.
We take an optimization-based perspective, which is somewhat different from
the original interpretation and justification of Freund and Schapire, but which
lends itself to our approach of (1) choose a representation, (2) choose a loss, and
(3) minimize the loss.
Before formulating the problem, we give a little intuition for what we are going
to do. Roughly, the idea of boosting is to take a weak learning algorithm—any
learning algorithm that gives a classifier that is slightly better than random—
and transforms it into a strong classifier, which does much much better than
random. To build a bit of intuition for what this means, consider a hypothetical
digit recognition experiment, where we wish to distinguish 0s from 1s, and we
receive images we must classify. Then a natural weak learner might be to take
the middle pixel of the image, and if it is colored, call the image a 1, and if it is
blank, call the image a 0. This classifier may be far from perfect, but it is likely
better than random. Boosting procedures proceed by taking a collection of such
weak classifiers, and then reweighting their contributions to form a classifier with
much better accuracy than any individual classifier.
With that in mind, let us formulate the problem. Our interpretation of boosting
is as a coordinate descent method in an infinite dimensional space, which—while
it sounds complex—is not so bad as it seems. First, we assume we have raw input
examples x ∈ Rn with labels y ∈ {−1, 1}, as is usual in binary classification. We
also assume we have an infinite collection of feature functions φj : Rn 7→ {−1, 1}
and an infinite vector θ = [θ1 θ2 · · · ]> , but which we assume always has only a
finite number of non-zero entries. For our classifier we use
∞
!
hθ ( x ) = sign ∑ θ j φj ( x ) .
j =1
176 appendix b. boosting
Then we say that there is a weak learner with margin γ > 0 if for any distribution p
on the m training examples there exists one weak hypothesis φj such that
m n o 1
∑ p (i )
1 y (i )
6 = φ j ( x (i )
) ≤ − γ.
2
(B.1)
i =1
That is, we assume that there is some classifier that does slightly better than
random guessing on the dataset. The existence of a weak learning algorithm is an
assumption, but the surprising thing is that we can transform any weak learning
algorithm into one with perfect accuracy.
In more generality, we assume we have access to a weak learner, which is an
algorithm that takes as input a distribution (weights) p on the training examples
and returns a classifier doing slightly better than random. We will show how,
given access to a weak learning algorithm, boosting can return a classifier with
perfect accuracy on the training data. (Admittedly, we would like the classifer to
generalize well to unseen data, but for now, we ignore this issue.)
We first show how to compute the exact form of the coordinate descent update
for the risk J (θ ). Coordinate descent iterates as follows:
(ii) Update θ j to
θ j = arg min J (θ )
θj
in α = θk . Now, define
W + := ∑ w (i ) and W − := ∑ w (i )
i:y(i) φk ( x (i) )=1 i:y(i) φk ( x (i) )=−1
to be the sums of the weights of examples that φk classifies correctly and incorrectly,
respectively. Then finding θk is the same as choosing
1 W+
α = arg min W + e−α + W − eα = log − .
α 2 W
To see the final equality, take derivatives and set the resulting equation to zero,
+
so we have −W + e−α + W − eα = 0. That is, W − e2α = W + , or α = 12 log W W−
.
What remains is to choose the particular coordinate to perform coordinate
descent on. We assume we have access to a weak-learning algorithm as in algo-
rithm B.1, which at iteration t takes as input a distribution p on the training set and
returns a weak hypothesis φt satisfying the margin condition in equation (B.1).
We present the full boosting algorithm in algorithm B.2. It proceeds in iterations
t = 1, 2, 3, . . .. We represent the set of hypotheses returned by the weak learning
algorithm at time t by {φ1 , . . . , φt }.
We now argue that the boosting procedure achieves 0 training error, and we
also provide a rate of convergence to zero. To do so, we present a lemma that
guarantees progress is made.
Then q
J ( θ (t) ) ≤ 1 − 4γ2 J (θ (t−1) ).
As the proof of the lemma is somewhat involved and not the central focus of
these notes—though it is important to know one’s algorithm will converge!— we
defer the proof to appendix B.4. Let us describe how it guarantees convergence of
the boosting procedure to a classifier with zero training error.
We initialize the procedure at θ (0) = 0, so that the initial empirical risk J (θ (0) ) =
1. Now, we note that for any θ, the misclassification error satisfies
n o n o
1 sign(θ > φ( x )) 6= y = 1 yθ > φ( x ) ≤ 0 ≤ exp −yθ > φ( x )
(iii) Compute Wt+ = ∑i:y(i) φt ( x(i) )=1 w(i) and Wt− = ∑i:y(i) φt ( x(i) )=−1 w(i) and
set
1 W+
θt = log t− .
2 Wt
because ez ≥ 1 for all z ≥ 0. Thus, we have that the misclassification error rate
has upper bound
m
1 n o
m ∑1 sign(θ > φ( x (i) )) 6= y(i) ≤ J ( θ ),
i =1
and so if J (θ ) < m1 then the vector θ makes no mistakes on the training data. After
t iterations of boosting, we find that the empirical risk satisfies
These classifiers are simple enough that we can fit them efficiently even to a
weighted dataset, as we now describe.
Indeed, a decision stump weak learner proceeds as follows. We begin with a
distribution—set of weights p(1) , . . . , p(m) summing to 1—on the training set, and
we wish to choose a decision stump of the form of equation (B.2) to minimize
the error on the training set. That is, we wish to find a threshold s ∈ R and index
j such that
m n o m n o
∑ p (i ) 1 ∑ p (i ) 1
(i )
c (φj , s, p) =
Err φj,s ( x (i) ) 6= y(i) = y (i ) ( x j − s ) ≤ 0 (B.3)
i =1 i =1
As the only values s for which the error of the decision stump can change are the
(i )
values x j , a bit of clever book-keeping allows us to compute
m n o m n o
∑ p (i ) 1 ∑ p (i k ) 1
(i ) (i k )
y (i ) ( x j − s ) ≤ 0 = y (i k ) ( x j − s) ≤ 0
i =1 k =1
c (−φj,s , p) = 1 − Err
Err c (φj,s , p).
(You should convince yourself that this is true.) Thus, it is important to also
track the smallest value of 1 − Err
c (φj,s , p) over all thresholds, because this may be
smaller than Err(φj,s , p), which gives a better weak learner. Using this procedure
c
for our weak learner (algorithm B.1) gives the basic, but extremely useful, boosting
classifier.
We now return to prove the progress lemma. We prove this result by directly
showing the relationship of the weights at time t to those at time t − 1. In particular,
we note by inspection that
q
J (θ (t) ) = min{Wt+ e−α + Wt− eα } = 2 Wt+ Wt−
α
while !
m t −1
1
J (θ ( t −1)
)=
m ∑ exp −y (i )
∑ θτ φτ (x (i )
) = Wt+ + Wt− .
i =1 τ =1
We know by the weak-learning assumption that
m n o 1 1 1
∑ p (i ) 1 y(i) 6= φt ( x (i) ) ≤ − γ,
2
or
Wt+ + Wt−
∑ w (i ) ≤
2
− γ.
i =1
i : y(i) φt ( x (i) ) = −1
| {z }
=Wt−
Rewriting this expression by noting that the sum on the right is nothing but Wt− ,
we have
1 1 + 2γ −
Wt− ≤ − γ (Wt+ + Wt− ), or Wt+ ≥ W .
2 1 − 2γ t
1 − 4γ2 J (θ (t−1) ).
p
References
1. D. M. Blei, A. Kucukelbir, and J. D. McAuliffe, ‘‘Variational Inference: A Review for
Statisticians,’’ Journal of the American Statistical Association, vol. 112, no. 518, pp. 859–
877, 2017 (cit. on p. 130).
2. D. P. Kingma and M. Welling, ‘‘Auto-Encoding Variational Bayes,’’ ArXiv Preprint
ArXiv:1312.6114, 2013 (cit. on pp. 128, 132).
3. M. J. Kochenderfer and T. A. Wheeler, Algorithms for Optimization. MIT Press, 2019 (cit.
on p. viii).