Week 1 Lecture ML : Linear Regression ~ parameter learning
mcdn2020. 8. 6. 14:37
반응형
ML:Linear Regression with One Variable
Model Representation
Recall that inregression problems, we are taking input variables and trying to fit the output onto acontinuousexpected result function.
Linear regression with one variable is also known as "univariate linear regression."
Univariate linear regression is used when you want to predict asingle outputvalue y from asingle inputvalue x. We're doingsupervised learninghere, so that means we already have an idea about what the input/output cause and effect should be.
Note that this is like the equation of a straight line. We give toh_\theta(x)hθ(x)values for\theta_0θ0and\theta_1θ1to get our estimated output\hat{y}y^. In other words, we are trying to create a function calledh_\thetahθthat is trying to map our input data (the x's) to our output data (the y's).
Example:
Suppose we have the following set of training data:
input xoutput y
0
4
1
7
2
7
3
8
Now we can make a random guess about ourh_\thetahθfunction:\theta_0=2θ0=2and\theta_1=2θ1=2. The hypothesis function becomesh_\theta(x)=2+2xhθ(x)=2+2x.
So for input of 1 to our hypothesis, y will be 4. This is off by 3. Note that we will be trying out various values of\theta_0θ0and\theta_1θ1to try to find values which provide the best possible "fit" or the most representative "straight line" through the data points mapped on the x-y plane.
Cost Function
We can measure the accuracy of our hypothesis function by using acost function. This takes an average (actually a fancier version of an average) of all the results of the hypothesis with inputs from x's compared to the actual output y's.
Now we are able to concretely measure the accuracy of our predictor function against the correct results we have so that we can predict new results we don't have.
If we try to think of it in visual terms, our training data set is scattered on the x-y plane. We are trying to make straight line (defined byh_\theta(x)hθ(x)) which passes through this scattered set of data. Our objective is to get the best possible line. The best possible linewill be such so that the average squared vertical distances of the scattered points from the line will be the least. In the best case, the line should pass through all the points of our training data set. In such a case the value ofJ(\theta_0, \theta_1)J(θ0,θ1)will be 0.
ML:Gradient Descent
So we have our hypothesis function and we have a way of measuring how well it fits into the data. Now we need to estimate the parameters in hypothesis function. That's where gradient descent comes in.
Imagine that we graph our hypothesis function based on its fields\theta_0θ0and\theta_1θ1(actually we are graphing the cost function as a function of the parameter estimates). This can be kind of confusing; we are moving up to a higher level of abstraction. We are not graphing x and y itself, but the parameter range of our hypothesis function and the cost resulting from selecting particular set of parameters.
We put\theta_0θ0on the x axis and\theta_1θ1on the y axis, with the cost function on the vertical z axis. The points on our graph will be the result of the cost function using our hypothesis with those specific theta parameters.
We will know that we have succeeded when our cost function is at the very bottom of the pits in our graph, i.e. when its value is the minimum.
The way we do this is by taking the derivative (the tangential line to a function) of our cost function. The slope of the tangent is the derivative at that point and it will give us a direction to move towards. We make steps down the cost function in the direction with the steepest descent, and the size of each step is determined by the parameter α, which is called the learning rate.
Gradient Descent Intuition
In this video we explored the scenario where we used one parameter\theta_1θ1and plotted its cost function to implement a gradient descent. Our formula for a single parameter was :
Regardless of the slope's sign for\frac{d}{d\theta_1} J(\theta_1)dθ1dJ(θ1),\theta_1θ1eventually converges to its minimum value. The following graph shows that when the slope is negative, the value of\theta_1θ1increases and when it is positive, the value of\theta_1θ1decreases.
On a side note, we should adjust our parameter\alphaαto ensure that the gradient descent algorithm converges in a reasonable time. Failure to converge or too much time to obtain the minimum value imply that our step size is wrong.
How does gradient descent converge with a fixed step size\alphaα?
The intuition behind the convergence is that\frac{d}{d\theta_1} J(\theta_1)dθ1dJ(θ1)approaches 0 as we approach the bottom of our convex function. At the minimum, the derivative will always be 0 and thus we get:
\theta_1:=\theta_1-\alpha * 0θ1:=θ1−α∗0
Gradient Descent for Linear Regression
When specifically applied to the case of linear regression, a new form of the gradient descent equation can be derived. We can substitute our actual cost function and our actual hypothesis function and modify the equation to (the derivation of the formulas are out of the scope of this course, but a really great one can be found here):
where m is the size of the training set,\theta_0θ0a constant that will be changing simultaneously with\theta_1θ1andx_{i}, y_{i}xi,yiare values of the given training set (data).
Note that we have separated out the two cases for\theta_jθjinto separate equations for\theta_0θ0and\theta_1θ1; and that for\theta_1θ1we are multiplyingx_{i}xiat the end due to the derivative.
The point of all this is that if we start with a guess for our hypothesis and then repeatedly apply these gradient descent equations, our hypothesis will become more and more accurate.
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 calledbatch 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.
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 as it converged to its minimum.
Gradient Descent for Linear Regression: visual worked example