def fit_steepestgd(self, X, y, iters=1000, verbose=False): self.coefs = np.ones(X.shape[1]) cur_loss = self.mse_loss(y, self.predict(X)) i = 0 sk = 1 while i < iters and np.linalg.norm(sk) > 10**(-6): func_for_opt = self.cost(X, y) ad_obj = AD(func_for_opt, multivar=True) if verbose: print("=====\nIter {} Loss: {}".format(i, cur_loss)) print(self.coefs) grads = ad_obj.get_der_numpy(self.coefs) sk = -1 * grads etak = line_search(ad_obj.get_val_numpy, ad_obj.get_der_numpy, self.coefs, sk)[0] self.coefs = self.coefs + etak * sk cur_loss = self.mse_loss(y, self.predict(X)) i += 1
def test_numpy_wrapper(): functionxx_class = AD(functionxx) val = functionxx_class.get_val_numpy(np.array([1, 2])) der = functionxx_class.get_der_numpy(np.array([1, 2])) assert list(val) == [2, 4] assert list(der) == [2, 2]