Lecture 2: Linear regression
1 Introduction
Let’s jump right in and look at our first machine learning algorithm, linear regression. In regression, we are interested in predicting a scalar-valued target, such as the price of a stock. By linear, we mean that the target must be predicted as a linear function of the inputs. This is a kind of supervised learning algorithm; recall that, in supervised learning, we have a collection of training examples labeled with the correct outputs.
Copyright By PowCoder代写 加微信 powcoder
Regression is an important problem in its own right. But today’s dis- cussion will also highlight a number of themes which will recur throughout the course:
• Formulating a machine learning task mathematically as an optimiza- tion problem.
• Thinking about the data points and the model parameters as vectors.
• Solving the optimization problem using two different strategies: deriv- ing a closed-form solution, and applying gradient descent. These two strategies are how we will derive nearly all of the learning algorithms in this course.
• Writing the algorithm in terms of linear algebra, so that we can think about it more easily and implement it efficiently in a high-level pro- gramming language.
• Making a linear algorithm more powerful using basis functions, or features.
• Analyzing the generalization performance of an algorithm, and in par- ticular the problems of overfitting and underfitting.
1.1 Learning goals
• Know what objective function is used in linear regression, and how it is motivated.
• Derive both the closed-form solution and the gradient descent updates for linear regression.
• Write both solutions in terms of matrix and vector operations.
• Be able to implement both solution methods in Python.
Figure 1: Three possible hypotheses for a linear regression model, shown in data space and weight space.
Know how linear regression can learn nonlinear functions using feature maps.
What is meant by generalization, overfitting, and underfitting? How can we measure generalization performance in practice?
Problem setup
In order to formulate a learning problem mathematically, we need to define two things: a model and a loss function. The model, or architecture defines the set of allowable hypotheses, or functions that compute predic- tions from the inputs. In the case of linear regression, the model simply consists of linear functions. Recall that a linear function of D inputs is parameterized in terms of D coefficients, which we’ll call the weights, and an intercept term, which we’ll call the bias. Mathematically, this is written as:
y = X wj xj + b. (1) j
Figure 1 shows two ways to visualize linear models. In this case, the data are one-dimensional, so the model reduces to simply y = wx + b. On one side, we have the data space, or input space, where t is plotted as a function of x. Three different possible linear fits are shown. On the other side, we have weight space, where the corresponding pairs (w, b) are plotted.
Clearly, some of these linear fits are better than others. In order to quantify how good the fit is, we define a loss function. This is a function L(y, t) which says how far off the prediction y is from the target t. In linear regression, we use squared error, defined as
L(y,t)= 21(y−t)2. (2)
This is small when y and t are close together, and large when they are far apart. In general, the value y − t is known as the residual, and we’d like the residuals to be close to zero.
When we combine our model and loss function, we get an optimization problem, where we are trying to minimize a cost function with respect to the model parameters (i.e. the weights and bias). The cost function is simply the loss, averaged over all the training examples. When we plug in
You should study these figures and try to understand how the lines in the left figure map onto the X’s on the right figure. Think back to middle school. Hint: w is the slope of the line, and b is the y-intercept.
Why is there the factor of 1/2 in front? It just makes the calculations convenient.
Figure 2: Left: three hypotheses for a regression dataset. Middle: Contour plot of least-squares cost function for the regression problem. Colors of the points match the hypotheses. Right: Surface plot matching the contour plot. Surface plots are usually hard to interpret, so we won’t look at them very often.
the model definition (Eqn. 1), we get the following cost function:
L(y(i),t(i)) (3) =2N y−t (4)
E(w1,…,wD,b) = N
1 XN (i) (i)2
= X Xwjx(i) +b−t(i) (5) 2Nj
Our goal is to choose w1, . . . , wD and b to minimize E. Note the difference between the loss function and the cost function. The loss is a function of the predictions and targets, while the cost is a function of the model parameters. The cost function is visualized in Figure 2.
3 Solving the optimization problem
In order to solve the optimization problem, we’ll need the concept of partial derivatives. If you haven’t seen these before, then you should go learn about them, on .1 Just as a quick recap, suppose f is a function of x1, . . . , xD. Then the partial derivative ∂f/∂xi says in what way the value of f changes if you increase xi by a small amount, while holding the rest of the arguments fixed. We can evaluate partial derivatives using the tools of single-variable calculus: to compute ∂f/∂xi simply compute the (single- variable) derivative with respect to xi, treating the rest of the arguments as constants.
Whenever we want to solve an optimization problem, a good place to start is to compute the partial derivatives of the cost function. Let’s do that in the case of linear regression. Applying the chain rule for derivatives
1https://www.khanacademy.org/math/calculus-home/multivariable-calculus/ multivariable-derivatives#partial-derivatives
The distinction between loss functions and cost functions will become clearer in a later lecture, when the cost function is augmented to include more than just the loss — it will also include a term called a regularizer which encourages simpler hypotheses.
to Eqn. 5, we get
= Xx(i) Xw ′x(i) +b−t(i) ∂wj N j jj′
i=1 j′ ∂E 1N
= X Xw′x(i)+b−t(i) . ∂b N j j′
It’s possible to simplify this a bit — notice that part of the term in paren- theses is simply the prediction. The partial derivatives can be rewritten as:
= X x(i)(y(i) − t(i)) (8)
It’s always a good idea to try to simplify equations by finding familiar terms.
∂wj N j i=1
y(i) − t(i). (9)
Now, it’s good practice to do a sanity check of the derivatives. For instance, suppose we overestimated all of the targets. Then we should be able to improve the predictions by decreasing the bias, while holding all of the weights fixed. Does this work out mathematically? Well, the residuals y(i) − t(i) will be positive, so based on Eqn. 9, ∂E/∂b will be positive. This means increasing the bias will increase E, and deceasing the bias will decrease E — which matches up with our expectation. So Eqn. 9 is plausible. Try to come up with a similar sanity check for ∂E/∂wj.
Now how do we use these partial derivatives? Let’s discuss the two methods which we will use throughout the course.
3.1 Direct solution
One way to compute the minimum of a function is to set the partial deriva- tives to zero. Recall from single variable calculus that (assuming a function is differentiable) the minimum x⋆ of a function f has the property that the derivative df/dx is zero at x = x⋆. Note that the converse is not true: if df/dx = 0, then x⋆ might be a maximum or an inflection point, rather than a minimum. But the minimum can only occur at points that have derivative zero.
An analogous result holds in the multivariate case: if f is differentiable, then all of the partial derivatives ∂f/∂xi are zero at the minimum. The intuition is simple: if ∂f/∂xi is positive, then one can decrease f slightly by decreasing xi slightly. Conversely, if ∂f/∂xi is negative, then one can decrease f slightly by increasing xi slightly. In either case, this implies we’re not at the minimum. Therefore, if the minimum exists (i.e. f doesn’t keep growing as x goes to infinity), it occurs at a critical point, i.e. a point where the partial derivatives are zero. This gives us a strategy for finding minima: set the partial derivatives to zero, and solve for the parameters. This method is known as direct solution.
Let’s apply this to linear regression. For simplicity, let’s assume the model doesn’t have a bias term. (We actually don’t lose anything by getting
Later in this course, we’ll introduce a more powerful way to test partial derivative computations, but you should still get used to doing sanity checks on all your computations!
rid of the bias. Just add a “dummy” input x0 which always takes the value 1; then the weight w0 acts as a bias.) We simplify Eqn. 6 to remove the bias, and set the partial derivatives to zero:
= X x(i) X w ′ x(i) − t(i) = 0 (10)
∂wjNjjj′ i=1 j′=1
Since we’re trying to solve for the weights, let’s pull these out: ∂E1DN !1N
= X Xx(i)x(i) w′ − Xx(i)t(i)=0 (11) ∂wj N j j′ j N j
j′=1 i=1 i=1
The details of this equation aren’t important; what’s important is that we’ve wound up with a system of D linear equations in D variables. In other words, we have the system of linear equations
XAjj′wj′ −cj =0 ∀j∈{1,…,D}, (12) j′=1
where A ′ = 1 PN x(i)x(i) and c = 1 PN x(i)t(i). As computer scien- jj N i=1 j j′ j N i=1 j
tists, we’re done, because this gives us an algorithm for finding the optimal regression weights: we first compute all the values Aj j ′ and cj , and then solve the system of linear equations using a linear algebra library such as NumPy. (We’ll give an implementation of this later in this lecture.)
Note that the solution we just derived is very particular to linear re- gression. In general, the system of equations will be nonlinear, and except in rare cases, systems of nonlinear equations don’t have closed form solu- tions. Linear regression is very unusual, in that it has a closed-form solution. We’ll only be able to come up with closed form solutions for a handful of the algorithms we cover in this course.
3.2 Gradient descent
Now let’s minimize the cost function a different way: gradient descent. This is an example of an iterative algorithm, which means that we apply a certain update rule over and over again, and if we’re lucky, our iterates will gradually improve according to our objective function. To do gradient descent, we initialize the weights to some value (e.g. all zeros), and repeat- edly adjust them in the direction that most decreases the cost function. If we visualize the cost function as a surface, so that lower is better, this is the direction of steepest descent. We repeat this procedure until the iterates converge, or stop changing much. (Or, in practice, we often run it until we get tired of waiting.) If we’re lucky, the final iterate will be close to the optimum.
In order to make this mathematically precise, we must introduce the gradient, the direction of steepest ascent (i.e. fastest increase) of a function. The entries of the gradient vector are simply the partial derivatives with respect to each of the variables:
∂E ∂E ∂w1
∂w= . (13)
The reason that this formula gives the direction of steepest ascent is beyond the scope of this course. (You would learn about it in a multivariable calculus class.) But this suggests that to decrease a function as quickly as possible, we should update the parameters in the direction opposite the gradient.
We can formalize this using the following update rule, which is known as gradient descent:
or in terms of coordinates,
w ← w − α ∂E , (14) ∂w
wj ← wj − α ∂E . (15) ∂wj
The symbol ← means that the left-hand side is updated to take the value on the right-hand side; the constant α is known as a learning rate. The larger it is, the larger a step we take. We’ll talk in much more detail later about how to choose a learning rate, but in general it’s good to choose a small value such as 0.01 or 0.001. If we plug in the formula for the partial derivatives of the regression model (Eqn. 8), we get the update rule:
In practice, we rarely if ever go
through this last step. From a 1N software engineering perspective,
wj←wj−α Xxj(y(i)−t(i)) (16) N i=1
So we just repeat this update lots of times. What does gradient descent give us in the end? For analyzing iterative algorithms, it’s useful to look for fixed points, i.e. points where the iterate doesn’t change. By inspecting Eqn. 14, setting the left-hand side equal to the right-hand side, we see that the fixed points occur where ∂E/∂w = 0. Since we know the gradient must be zero at the optimum, this is an encouraging sign that maybe it will converge to the optimum. But there are lots of things that could go wrong, such as divergence or local optima; we’ll look at these in more detail in a later lecture.
You might ask: by setting the partial derivatives to zero, we compute the exact solution. With gradient descent, we never actually reach the optimum, but merely approach it gradually. Why, then, would we ever prefer gradient descent? Two reasons:
1. We can only solve the system of equations explicitly for a handful of models. By contrast, we can apply gradient descent to any model for which we can compute the gradient. This is usually pretty easy to do efficiently. Importantly, it can usually be done automatically, so software packages like Theano and TensorFlow can save us from ever having to compute partial derivatives by hand.
2. Solving a large system of linear equations can be expensive, possi- bly many orders of magnitude more expensive than a single gradient descent update. Therefore, gradient descent can sometimes find a rea- sonable solution much faster than solving the linear system. There- fore, gradient descent is often more practical than computing exact solutions, even for models where we are able to derive the latter.
it’s better to write our code in a modular way, where one function computes the gradient, and another function implements gradient descent, taking the gradient as given.
Lecture 9 discusses optimization issues.
For these reasons, gradient descent will be our workhorse throughout the course. We will use it to train almost all of our models, with the exception of a handful for which we can derive exact solutions.
4 Vectorization
Now it’s time to bring in linear algebra. We’re going to rewrite the linear regression model, as well as both solution methods, in terms of operations on matrices and vectors. This process is known as vectorization. There are two reasons for doing this:
1. The formulas can be much simpler, more compact, and more readable in this form.
2. Vectorized code can be much faster than explicit for-loops, for several reasons.
• High-level languages like Python can introduce a lot of inter- preter overhead, and if we explicitly write a for-loop correspond- ing to Eqn. 16, this might be 10-100 times slower than the C equivalent. If we instead write the algorithm in terms of a much smaller number of linear algebra operations, then it can per- form the same computations much faster with minimal inter- preter overhead.
• Since linear algebra is used all over the place, linear algebra li- braries have been extremely well optimized for various computer architectures. Hence, they use much more efficient memory- access patterns than a na ̈ıve for-loop, even one written in C.
• Matrix multiplication is inherently highly parallelizable and in- volve little control flow. Hence, it’s ideal for graphics process- ing unit (GPU) architectures. We’re not going to talk much about GPUs in this course, but just think “matrix multiplication + GPU = good”. If you run vectorized code on a GPU using a framework like TensorFlow or PyTorch, it may run 50 times faster than the CPU version. As it turns out, most of the com- putation in deep learning is matrix multiplications, which is why it’s been such an incredibly good match for GPUs.
First, we need to represent the data and model parameters in the form of matrices and vectors. If we have N training examples, each D-dimensional, we will represent the inputs as an N × D matrix X. Each row of X cor- responds to a training example, and each column corresponds to a single input dimension. The weights are represented as a D-dimensional vector w, and the targets are represented as a N-dimensional vector t.
The predictions are computed using a matrix-vector product
y = Xw + b1, (17) where 1 denotes a vector of all ones. We can express the cost function in
Vectorization takes a lot of practice to get used to. We’ll cover a lot of examples in the first few weeks of the course. I’d recommend practicing these until they start to feel natural.
In general, matrices will be denoted with capital boldface, vectors with lowercase boldface, and scalars with plain type.
vectorized form:
You should stop now and try to show that these equations are equivalent to Eqns. 3–5. The only way you get comfortable with this is by practicing.
E=1∥y−t∥2 (18) 2N
= 2N ∥Xw+b1−t∥2.
Even more importantly,
indices i and j. As our convenient letters to use
w=XX Xt. (20)
An exact solution which we can express with a formula is known as a closed- form solution.
Similarly, we can vectorize the gradient descent update from Eqn. 16: w ← w − Nα X⊤(y − t), (21)
where y is computed as in Eqn. 17. 5 Feature mappings
Linear regression might sound pretty limited. What if the true relationship between inputs and targets is nonlinear? Fortunately, there’s an easy way to use linear regression to learn nonlinear dependencies: use a feature mapping. I’ll introduce this by way of an example. Suppose we want to approximate it with a cubic polynomial. In other words, we would compute the predictions as:
y = w3x3 + w2x2 + w1x + w0. (22)
This setting is known as polynomial regression.
Let’s use the squared error loss function, just as with ordinary linear re-
gression. The important thing to notice is that algorithmically, polynomial regression is no different from linear regression. We can apply any of the linear regression algorithms described above, using (x, x2, x3) as the inputs. Mathematically, we define a feature mapping φ, in this case
φ(x) = x, (23)
Note that this is considerably simpler than Eqn. 5. it saves us from having to explicitly sum over the models get more complicated, we would run out of as indices if we didn’t vectorize.
Now let’s revisit the exact solution for linear
a system of linear equations, with coefficients A ′ = 1 PN x(i)x(i) and
jj N i=1 j j′
cj = 1 PN x(i)t(i). In terms of linear algebra, we can write these as the
matrix A = N1 X⊤X and c = N1 X⊤t. The solution to the linear system Aw = c is given by w = A−1c (assuming A is invertible), so this gives us a formula for the optimal weights:
and compute the predictions as y = w⊤φ(x) instead of w⊤x. The rest of the algorithm is completely unchanged.
x 2 x3
regression. We derived
Just as in Section 3.1, we’re including a constant feature to account for the bias term, since this simplifies the notation.
Feature maps are a useful tool, but they’re not a silver bullet, for several reasons:
• The features must be known in advance. It’s not always easy to pick good features, and up until very recently, feature engineering would take up most of the time and ingenuity in building a practical machine learning system.
• In high dimensions, the feature representations can get very large. For instance, the number of terms in a cubic polynomial is cubic in the dimension!
In this course, rather than construct feature maps, we will use neural net- works to learn nonlinear predictors directly from the raw inputs. In most cases, this eliminates the need for hand-engineering of features.
6 Generalization
We don’t just want a learning algorithm to make correct predictions on the training exam
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com