Docsity

Supervised Learning For CSS - Topics in Description., Lecture notes of Computer Science

Toggle sidebar
bg1
Chapter 1
Linear regression
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:
Living area (feet2) #bedrooms Price (1000$s)
2104 3 400
1600 3 330
2400 3 369
1416 2 232
3000 4 540
.
.
..
.
..
.
.
Here, the x’s are two-dimensional vectors in R2. For instance, x(i)
1is the
living area of the i-th house in the training set, and x(i)
2is its number of
bedrooms. (In general, when designing a learning problem, it will be up to
you to decide what features to choose, so if you are out in Portland gathering
housing data, you might also decide to include other features such as whether
each house has a fireplace, the number of bathrooms, and so on. We’ll say
more about feature selection later, but for now let’s take the features as
given.)
To perform supervised learning, we must decide how we’re going to rep-
resent functions/hypotheses hin a computer. As an initial choice, let’s say
we decide to approximate yas a linear function of x:
hθ(x) = θ0+θ1x1+θ2x2
Here, the θi’s are the parameters (also called weights) parameterizing the
space of linear functions mapping from Xto Y. When there is no risk of
9
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Supervised Learning For CSS - Topics in Description. and more Lecture notes Computer Science in PDF only on Docsity!

Chapter 1

Linear regression

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:

Living area (feet^2 ) #bedrooms Price (1000$s) 2104 3 400 1600 3 330 2400 3 369 1416 2 232 3000 4 540 .. .

Here, the x’s are two-dimensional vectors in R^2. For instance, x (i) 1 is the living area of the i-th house in the training set, and x (i) 2 is its number of bedrooms. (In general, when designing a learning problem, it will be up to you to decide what features to choose, so if you are out in Portland gathering housing data, you might also decide to include other features such as whether each house has a fireplace, the number of bathrooms, and so on. We’ll say more about feature selection later, but for now let’s take the features as given.) To perform supervised learning, we must decide how we’re going to rep- resent functions/hypotheses h in a computer. As an initial choice, let’s say we decide to approximate y as a linear function of x:

hθ(x) = θ 0 + θ 1 x 1 + θ 2 x 2

Here, the θi’s are the parameters (also called weights) parameterizing the space of linear functions mapping from X to Y. When there is no risk of

confusion, we will drop the θ subscript in hθ(x), and write it more simply as h(x). To simplify our notation, we also introduce the convention of letting x 0 = 1 (this is the intercept term), so that

h(x) =

∑^ d

i=

θixi = θT^ x,

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 x 0 ). 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(θ) =

∑^ n

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.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,... , d.) 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

By grouping the updates of the coordinates into an update of the vector θ, we can rewrite update (1.1) in a slightly more succinct way:

θ := θ + α

∑^ n

i=

y(i)^ − hθ(x(i))

x(i)

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.

5 10 15 20 25 30 35 40 45 50

5

10

15

20

25

30

35

40

45

50

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 x’s 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 to predict housing price as a function of living area, we obtain θ 0 = 71.27, θ 1 = 0.1345. If we plot hθ(x) as a function of x (area), along with the training data, we obtain the following figure:

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)

If the number of bedrooms were included as one of the input features as well, we get θ 0 = 89. 60 , θ 1 = 0.1392, θ 2 = − 8 .738. The above results were obtained with batch gradient descent. There is an alternative to batch gradient descent that also works very well. Consider the following algorithm:

Loop {

for i = 1 to n, {

θj := θj + α

y(i)^ − hθ(x(i))

x( ji ), (for every j) (1.2)

}

}

By grouping the updates of the coordinates into an update of the vector θ, we can rewrite update (1.2) in a slightly more succinct way:

θ := θ + α

y(i)^ − hθ(x(i))

x(i)

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 n is large—stochastic gradient descent can start making progress right away, and

Here, Aij denotes the (i, j) entry of the matrix A. We then have

∇Af (A) =

[ 3

2 10 A^12

A 22 A 21

]

1.2.2 Least squares revisited

Armed with the tools of matrix derivatives, let us now proceed to find in closed-form the value of θ that minimizes J(θ). We begin by re-writing J in matrix-vectorial notation. 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 =

— (x(1))T^ — — (x(2))T^ — .. . — (x(n))T^ —

Also, let ~y be the n-dimensional vector containing all the target values from the training set:

~y =

y(1) y(2) .. . y(n)

Now, since hθ(x(i)) = (x(i))T^ θ, we can easily verify that

Xθ − ~y =

(x(1))T^ θ .. . (x(n))T^ θ

y(1) .. . y(n)

hθ(x(1)) − y(1) .. . hθ(x(n)) − y(n)

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) =

∑^ n

i=

(hθ(x(i)) − y(i))^2

= J(θ)

Finally, to minimize J, let’s find its derivatives with respect to θ. Hence,

∇θJ(θ) = ∇θ

(Xθ − ~y)T^ (Xθ − ~y)

=

∇θ

(Xθ)T^ Xθ − (Xθ)T^ ~y − ~yT^ (Xθ) + ~yT^ ~y

∇θ

θT^ (XT^ X)θ − ~yT^ (Xθ) − ~yT^ (Xθ)

∇θ

θT^ (XT^ X)θ − 2(XT^ ~y)T^ θ

2 XT^ Xθ − 2 XT^ ~y

= XT^ Xθ − XT^ ~y

In the third step, we used the fact that aT^ b = bT^ a, and in the fifth step used the facts ∇xbT^ x = b and ∇xxT^ Ax = 2Ax for symmetric matrix A (for more details, see Section 4.3 of “Linear Algebra Review and Reference”). 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

1.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), (^3) Note that in the above step, we are implicitly assuming that XT (^) X is an invertible

matrix. This can be checked before calculating the inverse. If either the number of linearly independent examples is fewer than the number of features, or if the features are not linearly independent, then XT^ X will not be invertible. Even in such cases, it is possible to “fix” the situation with additional techniques, which we skip here for the sake of simplicty.

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 maximize the log likelihood `(θ):

`(θ) = log L(θ)

= log

∏^ n

i=

2 πσ

exp

(y(i)^ − θT^ x(i))^2 2 σ^2

∑^ n

i=

log

2 πσ

exp

(y(i)^ − θT^ x(i))^2 2 σ^2

= n log

2 πσ

σ^2

∑^ n

i=

(y(i)^ − θT^ x(i))^2.

Hence, maximizing `(θ) gives the same answer as minimizing

1 2

∑^ n

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.

1.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. 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

(^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

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 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:

  1. Fit θ to minimize

i(y

(i) (^) − θT (^) x(i)) (^2).

  1. Output θT^ x.

In contrast, the locally weighted linear regression algorithm does the fol- lowing:

  1. Fit θ to minimize

i w

(i)(y(i) (^) − θT (^) x(i)) (^2).

  1. Output θT^ x.