Explanation of how the gradient code works

Why would the value of x never cross 5 irrespective of number of times the loop is increased.

x=0;
error=[]
lr = 0.1
plt.plot(X,Y)
for i in range(100):
gradient = 2*(x-5)
x = x - lr*gradient
print(x)
err = (x-5)**2
error.append(err)
plt.scatter(x,err)

hey @indrabijaynarayan ,
Just imagine you are playing a game in which you have climb stairs only , but after some particular intervals , step size decreases but you speed remains the same ( there is no change in your speed ) , after another interval step size is reduced again, and again and again.
a time comes when these step sizes become really small , almost negligible, but the game keeps on moving.

Similarly , it is working here. Your goal is to reach as close as possible to your stated value 5. But your learning rate says , you keep trying buddy , i will not let you reach 5 , but i can help you reach very much closer to it , such that 5 will be just adjacent to you.
So as you move forward and calculate error , initially its value is big ,so you take big steps even lowering it by multiplying it by learning rate. But after some time , we observe error becomes very less and hence change in value of x is also less. Error keeps on decreasing , decreasing ,decreasing …but never increases. Means your steps are going to be even smaller as you move forward.
This is the reason you are not able to cross value 5 after hundreds of iterations also.

I hope this would have resolved your doubt.
I know this explanation was a kind of waste , you can refer to this link for better understanding.

Thank You and Happy Learning :slightly_smiling_face:.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.