I'm working with the following code for gradient descent for simple linear regression:
import numpy as np
def gradient_descent(x,y):
mCurr = 0
bCurr = 0
iterations = 1000
n = len(x)
learning_rate = 0.0005
for i in range(iterations):
y_predicted = mCurr * x + bCurr
cost = (1/n) * sum([val**2 for val in (y-y_predicted)])
md = -(2/n)*sum(x*(y-y_predicted))
bd = -(2/n)*sum(y-y_predicted)
mCurr = mCurr - learning_rate * md
bCurr = bCurr - learning_rate * bd
print ("m {}, b {}, cost {} iteration {}".format(mCurr,bCurr,cost, i))
x = np.array([20, 43, 63, 26, 53, 31, 58, 46, 58, 70, 46, 53, 60, 20, 63, 43, 26, 19, 31, 23])
y = np.array([120, 128, 141, 126, 134, 128, 136, 132, 140, 144, 128, 136, 146, 124, 143, 130, 124, 121, 126, 123])
gradient_descent(x,y)
I'm facing two kids of errors: 1) The value of slope (m) and y-intercept (b) keep on alternating between negative and positive values:
m -5.0820333396910965e+35, b -1.0389559623628228e+34, cost 4.57760985674227e+74 iteration 997
m 5.5128418892908475e+35, b 1.1270291963079477e+34, cost 5.386601277550346e+74 iteration 998
m -5.980170468178623e+35, b -1.2225684777262613e+34, cost 6.338564061017721e+74 iteration 999
2) when i try to get rid of this error by hyper parameter tuning (i.e. changing the learning rate or the number of iterations), after a certain number of iterations I get NaN values for m and b and infinity value for cost.
Does anyone know what I can do to fix this? I've already tried changing the learning rate and the number of iterations.