How to optimize linear regression model using gradient descent.
Updated: Oct 5, 2022
It is an optimization algorithm that works iteratively and aims to find the minimum value of a convex function with respect to a set of parameters.
You can call it a hit-and-trial method.
Let's understand it with a simple but effective example.
![](https://static.wixstatic.com/media/7ed44c_1f71097e8af143ba9bbe104c286e951c~mv2.gif/v1/fill/w_600,h_479,al_c,pstr/7ed44c_1f71097e8af143ba9bbe104c286e951c~mv2.gif)
For example, if we have a ball and we need to through it in a basket, we will try throwing it at certain angle, it may possible we can't get the goal hit, but we get a rough idea about the angle, we will try again, if fails, then again our angle will improve and so on. Finally with this hit and trial approach we will get the goal hit. The same thing works for Gradient Descent.
Steps involve in gradient descent
1) Random Initialization
Take random value of slope(m) and intercept(c). To avoid everything from scratch, you can take the slope as 0.1 and intercept as a mean of the target value.
2) Generate Prediction
Solve the linear equation using initialization value.
Y = mx+c
3) Calculating the cost
Cost calculation is the most important part of this step, we can calculate using the given formula.
![](https://static.wixstatic.com/media/7ed44c_0405f41760e84a9ab5ced52bcdb8f36d~mv2.png/v1/fill/w_446,h_158,al_c,q_85,enc_auto/7ed44c_0405f41760e84a9ab5ced52bcdb8f36d~mv2.png)
4) Updation of Parameter
As we know new answer is derived from the old values.
![](https://static.wixstatic.com/media/7ed44c_e10cad9c5a624477ad7379401e833148~mv2.png/v1/fill/w_238,h_91,al_c,q_85,enc_auto/7ed44c_e10cad9c5a624477ad7379401e833148~mv2.png)
Z is an unknown quantity, it can be positive or negative.
If Z is positive, then the parameter will decrease.
If Z is negative, then the parameter will increase.
![](https://static.wixstatic.com/media/7ed44c_a3a565863a9c468cae90296c2cbf45b7~mv2.png/v1/fill/w_289,h_192,al_c,q_85,enc_auto/7ed44c_a3a565863a9c468cae90296c2cbf45b7~mv2.png)
But how to know on which side of the minimum cost we are on and what value should we take for Z?
The solution is Partial Differentiation
![](https://static.wixstatic.com/media/7ed44c_0ae9d50919894479b94ee2efa1d0e9bc~mv2.png/v1/fill/w_592,h_388,al_c,q_85,enc_auto/7ed44c_0ae9d50919894479b94ee2efa1d0e9bc~mv2.png)
This calculation is pretty straightforward.
If the value of Alfa is High then the cost function may explode and bounce out far from the minima.
If the value of Alfa is Low then parameters will take a lot of iteration to converge to the optimal values.
We will code in the upcoming projects.
Commenti