In the previous article, Linear Regression 1 – Simple Linear Regression and Cost Function, we introduced the concept of simple linear regression, which is basically to find a regression line model

so that the prediction M_{w}(x) is as close to the y of our training data (x,y) as possible. To find the best fit regression line, we are actually finding the optimal combination of the weight parameters w_{0} and w_{1} and trying to minimize the errors between the predictions and the actual values of target feature y. We then defined the error function or cost function that measuring the errors as:

To find the minimum of the cost function we first calculate the gradient of cost function. That is, we calculate the partial derivatives of *J* with respect to w_{0} and w_{1}

Linear regression is a kind of convex problem which means it has the global optimum, and the gradient at that point equals to zero. Therefore, we can set the previous two partial derivatives to zero and solve the equations analytically to find the optimal w_{0} and w_{1}. However, due to the problem size, in both the number of instances and number of features, solving the above equations directly is not feasible for most real world problems. Instead of solving the equations and finding the optimal values in one shot, there are other approaches to learn the weights in the iterative manner and one of them is **Gradient Descent**.

Gradient Descent starts by first picking a random point, i.e. a set of random values of the weights (w_{0} and w_{1} in our example case), in the error surface. Then it tries to look for a direction which leads the current point in the error surface moving downward, which means to determine the *slope* of the error surface at that point so that we can slightly move the point along the slope and give a smaller error. The algorithm then repeats the slope finding and moving steps and tries to reach the global minimum. Below figure shows an example of starting from a random point and gradually moving to the minimum. Blue line segments show the jumps between each (w_{1}, J) pairs and finally reach (1.0, 0).

*Gradient* is the slope of a function *f* at a certain point ** x**, represented by . In order to determine the gradient at

**, we need to first calculate the partial derivatives of**

*x**f*with respect to

*x*. The partial derivative shows how

_{i}*f*changes when

*x*changes at

_{i}**. Gradient is then defined as the set of all partial derivatives . After knowing the slope, we can determine how we should adjust point**

*x***in the next step to move toward the minimum, just like walking downhill from a random location of a valley, and that is why the algorithm called**

*x**Gradient Descent*.

Using the model at the beginning as an example, in the simplest form, what gradient descent algorithm does is to:

- Start from a random pair of w
_{0}and w_{1} - Iteratively update w
_{0}and w_{1}to reduce the cost function*J(w*by gradient descent, that is to reduce the error generated by the model until a minimum is found, or other stopping criteria are met._{0}, w_{1})

There is an important assumption in the gradient descent algorithm. We consider each weight is independent to each other and we can and should update them simultaneously. This should be noted in the below calculations.

To update the weights, we just need to adjust them along the gradient as below:

which has been calculated at the beginning. Hence the updating process in step 2 becomes:

The constant is called the *learning rate*. It determines how far should each adjustment go, and we shall discuss it in the next article.