I am trying to modify the code so that if the change in loss is less than 1%, it exits the iterations.
class MyLinReg(object):
def __init__(self, activation_function):
self.activation_function = activation_function
def fit(self, X, y, alpha = 0.001, epochs = 10):
self.theta = np.random.rand(X.shape[1] + 1)
self.errors =[]
n = X.shape[0]
for _ in range(epochs):
errors = 0
sum_1 = 0
sum_2 = 0
for xi, yi in zip(X, y):
sum_1 += (self.predict(xi) - yi)*xi
sum_2 += (self.predict(xi) - yi)
errors += ((self.predict(xi) - yi)**2)
self.theta[:-1] -= 2*alpha*sum_1/n
self.theta[-1] -= 2*alpha*sum_2/n
self.errors.append(errors/n)
if (((self.errors[-1] - self.errors[-2])/self.errors[-1]) < 0.01):
break
return self
def predict(self, X):
weighted_sum = np.dot(X, self.theta[:-1]) + self.theta[-1]
return self.activation_function(weighted_sum)