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)
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
You are initializing self.errors =[]
(so size = 0)
first time you get in for loop you append to it ( self.errors.append(errors/n)
so size is 1, but in the next line you try to access an index that doesn't exists since size is 1 ( if (((self.errors[-1] - self.errors[-2])/self.errors[-1]) < 0.01): )
ence why you have an error
For self.errors[-2]
to not throw an exception your list should be at least size 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.