






















Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!























Supervised learning
Let’s start by talking about a few examples of supervised learning problems. Suppose we have a dataset giving the living areas and prices of 47 houses from Portland, Oregon:
Living area (feet^2 ) Price (1000$s) 2104 400 1600 330 2400 369 1416 232 3000 540 .. .
We can plot this data:
500 1000 1500 2000 2500 3000 3500 4000 4500 5000
0
100
200
300
400
500
600
700
800
900
1000
housing prices
square feet
price (in $1000)
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?
CS229 Fall 2012 2
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 m training examples {(x(i), y(i)); i = 1 ,... , m}—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:
Training set
house.)
(living area of
Learning algorithm
x (^) h predicted y(predicted price) 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 regression prob- lem. When y can take on only a small number of discrete values (such as if, given the living area, we wanted to predict if a dwelling is a house or an apartment, say), we call it a classification problem.
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:
J(θ) =
∑^ m
i=
(hθ(x(i)) − y(i))^2.
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.
1 LMS algorithm
We want to choose θ so as to minimize J(θ). To do so, let’s use a search algorithm that starts with some “initial guess” for θ, and that repeatedly changes θ to make J(θ) smaller, until hopefully we converge to a value of θ that minimizes J(θ). Specifically, let’s consider the gradient descent algorithm, which starts with some initial θ, and repeatedly performs the update:
θj := θj − α
∂θj
J(θ).
(This update is simultaneously performed for all values of j = 0,... , n.) 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: ∂ ∂θj
J(θ) =
∂θj
(hθ(x) − y)^2
(hθ(x) − y) ·
∂θj
(hθ(x) − y)
= (hθ(x) − y) ·
∂θj
( (^) n ∑
i=
θixi − y
= (hθ(x) − y) xj
For a single training example, this gives the update rule:^1
θj := θj + α
y(i)^ − hθ(x(i))
x( ji ).
The rule is called the LMS update rule (LMS stands for “least mean squares”), and is also known as the Widrow-Hoff learning rule. This rule has several properties that seem natural and intuitive. For instance, the magnitude of the update is proportional to the error term (y(i)^ − hθ(x(i))); thus, for in- stance, if we are encountering a training example on which our prediction nearly matches the actual 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’d 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:
Repeat until convergence {
θj := θj + α
∑m i=
y(i)^ − hθ (x(i))
x( ji ) (for every j).
}
The reader can easily verify that the quantity in the summation in the update rule above is just ∂J(θ)/∂θj (for the original definition of J). So, this is simply 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 function.
(^1) We use the notation “a := b” to denote an operation (in a computer program) in which we set the value of a variable a to be equal to the value of b. In other words, this operation overwrites a with the value of b. In contrast, we will write “a = b” when we are asserting a statement of fact, that the value of a is equal to the value of b.
Loop {
for i=1 to m, { θj := θj + α
y(i)^ − hθ(x(i))
x( ji ) (for every j). }
}
In this algorithm, we repeatedly run through the training set, and each time we encounter a training example, we update the parameters according to the gradient of the error with respect to that single training example only. This algorithm is called stochastic gradient descent (also incremental gradient descent). Whereas batch gradient descent has to scan through the entire training set before taking a single step—a costly operation if m is large—stochastic gradient descent can start making progress right away, and continues to make progress with each example it looks at. Often, stochastic gradient descent gets θ “close” to the minimum much faster than batch gra- dient descent. (Note however that it may never “converge” to the minimum, and the parameters θ will keep oscillating around the minimum of J(θ); but in practice most of the values near the minimum will be reasonably good approximations to the true minimum.^2 ) For these reasons, particularly when the training set is large, stochastic gradient descent is often preferred over batch gradient descent.
2 The normal equations
Gradient descent gives one way of minimizing J. Let’s discuss a second way of doing so, this time performing the minimization explicitly and without resorting to an iterative algorithm. In this method, we will minimize J by explicitly taking its derivatives with respect to the θj ’s, and setting them to zero. To enable us to 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. (^2) While it is more common to run stochastic gradient descent as we have described it
and with a fixed learning rate α, by slowly letting the learning rate α decrease to zero as the algorithm runs, it is also possible to ensure that the parameters will converge to the global minimum rather then merely oscillate around the minimum.
For a function f : Rm×n^7 → R mapping from m-by-n matrices to the real numbers, we define the derivative of f with respect to A to be:
∇Af (A) =
∂f ∂A 11 · · ·^
∂f ∂A 1 n .. .
∂f ∂Am 1 · · ·^
∂f ∂Amn
Thus, the gradient ∇Af (A) is itself an m-by-n matrix, whose (i, j)-element
is ∂f /∂Aij. For example, suppose A =
A 11 A 12 A 21 A 22
is a 2-by-2 matrix, and
the function f : R^2 ×^2 7 → R is given by
f (A) =
Here, Aij denotes the (i, j) entry of the matrix A. We then have
∇Af (A) =
We also introduce the trace operator, written “tr.” For an n-by-n (square) matrix A, the trace of A is defined to be the sum of its diagonal entries:
trA =
∑^ n
i=
Aii
If a is a real number (i.e., a 1-by-1 matrix), then tr a = a. (If you haven’t seen this “operator notation” before, you should think of the trace of A as tr(A), or as application of the “trace” function to the matrix A. It’s more commonly written without the parentheses, however.) The trace operator has the property that for two matrices A and B such that AB is square, we have that trAB = trBA. (Check this yourself!) As corollaries of this, we also have, e.g.,
trABC = trCAB = trBCA, trABCD = trDABC = trCDAB = trBCDA.
The following properties of the trace operator are also easily verified. Here, A and B are square matrices, and a is a real number:
trA = trAT tr(A + B) = trA + trB tr aA = atrA
the training examples’ input values in its rows:
— (x(1))T^ — — (x(2))T^ — .. . — (x(m))T^ —
Also, let ~y be the m-dimensional vector containing all the target values from the training set:
~y =
y(1) y(2) .. . y(m)
Now, since hθ(x(i)) = (x(i))T^ θ, we can easily verify that
Xθ − ~y =
(x(1))T^ θ .. . (x(m))T^ θ
y(1) .. . y(m)
hθ(x(1)) − y(1) .. . hθ(x(m)) − y(m)
Thus, using the fact that for a vector z, we have that zT^ z =
i z
2 i :
1 2
(Xθ − ~y)T^ (Xθ − ~y) =
∑^ m
i=
(hθ (x(i)) − y(i))^2
= J(θ)
Finally, to minimize J, let’s find its derivatives with respect to θ. Combining Equations (2) and (3), we find that
∇AT^ trABAT^ C = BT^ AT^ CT^ + BAT^ C (5)
Hence,
∇θJ(θ) = ∇θ
(Xθ − ~y)T^ (Xθ − ~y)
=
∇θ
θT^ XT^ Xθ − θT^ XT^ ~y − ~yT^ Xθ + ~yT^ ~y
∇θ tr
θT^ XT^ Xθ − θT^ XT^ ~y − ~yT^ Xθ + ~yT^ ~y
∇θ
tr θT^ XT^ Xθ − 2tr ~yT^ Xθ
XT^ Xθ + XT^ Xθ − 2 XT^ ~y
= XT^ Xθ − XT^ ~y
In the third step, we used the fact that the trace of a real number is just the real number; the fourth step used the fact that trA = trAT^ , and the fifth step used Equation (5) with AT^ = θ, B = BT^ = XT^ X, and C = I, and Equation (1). To minimize J, we set its derivatives to zero, and obtain the normal equations: XT^ Xθ = XT^ ~y
Thus, the value of θ that minimizes J(θ) is given in closed form by the equation θ = (XT^ X)−^1 XT^ ~y.
3 Probabilistic interpretation
When faced with a regression problem, why might linear regression, and specifically why might the least-squares cost function J, be a reasonable choice? In this section, we will give a set of probabilistic assumptions, under which least-squares regression is derived as a very natural algorithm. Let us assume that the target variables and the inputs are related via the equation y(i)^ = θT^ x(i)^ + ǫ(i),
where ǫ(i)^ is an error term that captures either unmodeled effects (such as if there are some features very pertinent to predicting housing price, but that we’d left out of the regression), or random noise. Let us further assume that the ǫ(i)^ are distributed IID (independently and identically distributed) according to a Gaussian distribution (also called a Normal distribution) with
instead maximize the log likelihood ℓ(θ):
ℓ(θ) = log L(θ)
= log
∏^ m
i=
2 πσ
exp
(y(i)^ − θT^ x(i))^2 2 σ^2
∑^ m
i=
log
2 πσ
exp
(y(i)^ − θT^ x(i))^2 2 σ^2
= m log
2 πσ
σ^2
∑^ m
i=
(y(i)^ − θT^ x(i))^2.
Hence, maximizing ℓ(θ) gives the same answer as minimizing
1 2
∑^ m
i=
(y(i)^ − θT^ x(i))^2 ,
which we recognize to be J(θ), our original least-squares cost function. To summarize: Under the previous probabilistic assumptions on the data, least-squares regression corresponds to finding the maximum likelihood esti- mate of θ. This is thus one set of assumptions under which least-squares re- gression can be justified as a very natural method that’s just doing maximum likelihood estimation. (Note however that the probabilistic assumptions are by no means necessary for least-squares to be a perfectly good and rational procedure, and there may—and indeed there are—other natural assumptions that can also be used to justify it.) Note also that, in our previous discussion, our final choice of θ did not depend on what was σ^2 , and indeed we’d have arrived at the same result even if σ^2 were unknown. We will use this fact again later, when we talk about the exponential family and generalized linear models.
4 Locally weighted linear regression
Consider the problem of predicting y from x ∈ R. The leftmost figure below shows the result of fitting a y = θ 0 + θ 1 x to a dataset. We see that the data doesn’t really lie on straight line, and so the fit is not very good.
(^00 1 2 3 4 5 6 )
1
2
3
4
x
y
(^00 1 2 3 4 5 6 )
1
2
3
4
x
y
(^00 1 2 3 4 5 6 )
1
2
3
4
x
y
Instead, if we had added an extra feature x^2 , and fit y = θ 0 + θ 1 x + θ 2 x^2 , then we obtain a slightly better fit to the data. (See middle figure) Naively, it might seem that the more features we add, the better. However, there is also a danger in adding too many features: The rightmost figure is the result of fitting a 5-th order polynomial y =
j=0 θj^ x
j (^). We see that even though the
fitted curve passes through the data perfectly, we would not expect this to be a very good predictor of, say, housing prices (y) for different living areas (x). Without formally defining what these terms mean, we’ll say the figure on the left shows an instance of underfitting—in which the data clearly shows structure not captured by the model—and the figure on the right is an example of overfitting. (Later in this class, when we talk about learning theory we’ll formalize some of these notions, and also define more carefully just what it means for a hypothesis to be good or bad.) As discussed previously, and as shown in the example above, the choice of features is important to ensuring good performance of a learning algorithm. (When we talk about model selection, we’ll also see algorithms for automat- ically choosing a good set of features.) In this section, let us talk briefly talk about the locally weighted linear regression (LWR) algorithm which, assum- ing there is sufficient training data, makes the choice of features less critical. This treatment will be brief, since you’ll get a chance to explore some of the properties of the LWR algorithm yourself in the homework. In the original linear regression algorithm, to make a prediction at a query point x (i.e., to evaluate h(x)), we would:
i(y
(i) (^) − θT (^) x(i)) (^2).
In contrast, the locally weighted linear regression algorithm does the fol- lowing:
i w (i)(y(i) (^) − θT (^) x(i)) (^2).
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. 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
hθ(x) = g(θT^ x) =
1 + e−θT^ x^
where
g(z) =
1 + e−z
is called the logistic function or the sigmoid function. Here is a plot showing g(z):
−5^0 −4 −3 −2 −1 0 1 2 3 4 5
1
z
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 x 0 = 1, so that θT^ x = θ 0 +
∑n j=1 θj^ xj^. 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 g′:
g′(z) =
d dz
(1 + e−z)^2
e−z^
(1 + e−z)
(1 + e−z^ )
= g(z)(1 − g(z)).
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 like- lihood 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.
Above, we used the fact that g′(z) = g(z)(1 − g(z)). This therefore gives us the stochastic gradient ascent rule
θj := θj + α
y(i)^ − hθ(x(i))
x (i) j
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 θT^ 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 get get to GLM models. (See also the extra credit problem on Q3 of problem set 1.)
6 Digression: The perceptron learning algo-
rithm
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:
g(z) =
1 if z ≥ 0 0 if z < 0
If we then let hθ(x) = g(θT^ x) as before but using this modified definition of g, and if we use the update rule
θj := θj + α
y(i)^ − hθ(x(i))
x( ji ).
then we have the perceptron learning algorithm. In the 1960s, this “perceptron” was argued to be a rough model for how individual neurons in the brain work. Given how simple the algorithm is, it will also provide a starting point for our analysis when we talk about learning theory later in this class. Note however that even though the perceptron may be cosmetically similar to the other algorithms we talked about, it is actually a very different type of algorithm than logistic regression and least squares linear regression; in particular, it is difficult to endow the perceptron’s predic- tions with meaningful probabilistic interpretations, or derive the perceptron as a maximum likelihood estimation algorithm.
7 Another algorithm for maximizing ℓ(θ)
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 (θ) f ′(θ)
This method has a natural interpretation in which we can think of it as approximating the function f via a linear function that is tangent to f at the current guess θ, solving for where that linear function equals to zero, and letting the next guess for θ be where that linear function is zero. Here’s a picture of the Newton’s method in action:
−10 1 1.5 2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
x
f(x)
−10 1 1.5 2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
x
f(x)
−10 1 1.5 2 2.5 3 3.5 4 4.5 5
0
10
20
30
40
50
60
x
f(x)
In the leftmost figure, we see the function f plotted along with the line y = 0. We’re trying to find θ so that f (θ) = 0; the value of θ that achieves this is about 1.3. Suppose we initialized the algorithm with θ = 4.5. Newton’s method then fits a straight line tangent to f at θ = 4.5, and solves for the where that line evaluates to 0. (Middle figure.) This give us the next guess for θ, which is about 2.8. The rightmost figure shows the result of running one more iteration, which the updates θ to about 1.8. After a few more iterations, we rapidly approach θ = 1.3. Newton’s method gives a way of getting to f (θ) = 0. What if we want to use it to maximize some function ℓ? The maxima of ℓ correspond to points where its first derivative ℓ′(θ) is zero. So, by letting f (θ) = ℓ′(θ), we can use the same algorithm to maximize ℓ, and we obtain update rule:
θ := θ −
ℓ′(θ) ℓ′′(θ)
(Something to think about: How would this change if we wanted to use Newton’s method to minimize rather than maximize a function?)