MCPcopy Create free account
hub / github.com/rushter/MLAlgorithms / _gradient_descent

Method _gradient_descent

mla/linear_models.py:91–108  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

89 return X.dot(self.theta)
90
91 def _gradient_descent(self):
92 theta = self.theta
93 errors = [self._cost(self.X, self.y, theta)]
94 # Get derivative of the loss function
95 cost_d = grad(self._loss)
96 for i in range(1, self.max_iters + 1):
97 # Calculate gradient and update theta
98 delta = cost_d(theta)
99 theta -= self.lr * delta
100
101 errors.append(self._cost(self.X, self.y, theta))
102 logging.info("Iteration %s, error %s" % (i, errors[i]))
103
104 error_diff = np.linalg.norm(errors[i - 1] - errors[i])
105 if error_diff < self.tolerance:
106 logging.info("Convergence has reached.")
107 break
108 return theta, errors
109
110
111class LinearRegression(BasicRegression):

Callers 1

_trainMethod · 0.95

Calls 1

_costMethod · 0.95

Tested by

no test coverage detected