def test_disp(self): """Check if something is printed when `disp` is True.""" with Capturing() as output: hfn(self.func, self.x0, self.hess_vec, disp=True) self.assertTrue( len(output) > 0, 'You should print the progress when `disp` is True.')
def test_default(self): """Check if everything works correctly with default parameters.""" with Capturing() as output: x_min, f_min, status = hfn(self.func, self.x0, self.hess_vec) assert_equal(status, 0) self.assertTrue(norm(self.A.dot(x_min) - self.b, np.inf) <= 1e-4) self.assertTrue(abs(f_min) <= 1e-8) self.assertTrue( len(output) == 0, 'You should not print anything by default.')
def test_trace(self): """Check if the history is returned correctly when `trace` is True.""" x_min, f_min, status, hist = hfn(self.func, self.x0, self.hess_vec, trace=True) self.assertTrue(isinstance(hist['f'], np.ndarray)) self.assertTrue(isinstance(hist['norm_g'], np.ndarray)) self.assertTrue(isinstance(hist['n_evals'], np.ndarray)) self.assertTrue(isinstance(hist['elaps_t'], np.ndarray)) assert_equal(len(hist['norm_g']), len(hist['f'])) assert_equal(len(hist['n_evals']), len(hist['f'])) assert_equal(len(hist['elaps_t']), len(hist['f'])) # make sure `hist['n_evals']` is a cumulative sum of integers assert_equal(np.round(hist['n_evals']), hist['n_evals']) self.assertTrue(np.all(hist['n_evals'] >= 0)) self.assertTrue(np.all(hist['n_evals'][1:] - hist['n_evals'][:-1] > 0))
'disp': False, 'maxiter': 100 }, tol=1e-5, jac=True))['fun'] print("Optimal Value Found") # Newton method w_opt_newton, hist_newton = newton(oracle_hess, w0, disp=False, maxiter=100, tol=1e-10) w_opt_hfn, hist_hfn = hfn(oracle, w0, hessvec, disp=False, maxiter=200, tol=1e-7) # optimal_value = hist_newton['f'][-1] # Plotting plot_performance(hist_newton['f'], optimal_value, 'b', "Метод Ньютона", time_vec=hist_newton['elaps']) plot_performance(hist_hfn['f'], optimal_value, 'r', "Неточный Метод\n Ньютона",
mydisp=True, method="CG", options={'maxiter': 10}) print("BFGS") optimal_value_bfgs, hist_bfgs = minimize_wrapper(fun, w0, mydisp=True, method="L-BFGS-B", options={ 'ftol': 0, 'maxiter': 20 }) print("Inexact Newton") w_opt_hfn, hist_hfn = hfn(oracle, w0, hessvec, disp=True, maxiter=10, tol=1e-9) print("Newton") w_opt_newton, hist_newton = newton(oracle_hess, w0, disp=True, maxiter=10, tol=1e-9) # Plotting # print(optimal_value_cg['fun']) # print(optimal_value_bfgs['fun']) # print((hist_hfn['f'])[-1]) optimal_value = min( [optimal_value_cg['fun'], optimal_value_bfgs['fun'], (hist_hfn['f'])[-1]])
def fun(w): f, g = logreg(w.reshape((w.shape[0], 1)), Z_mat, gamma, hess=False) return f, g[:, 0] # def grad(w): # return (logreg(w.reshape((w.shape[0], 1)), Z_mat, gamma, hess=True)[1])[:, 0] optimal_value = (minimize(fun, w0[:, 0], method="CG", options={'disp': False, 'maxiter': 100}, tol=1e-5, jac=True))['fun'] print("Optimal Value Found") # Newton method w_opt_newton, hist_newton = newton(oracle_hess, w0, disp=False, maxiter=100, tol=1e-10) w_opt_hfn, hist_hfn = hfn(oracle, w0, hessvec, disp=False, maxiter=200, tol=1e-7) # optimal_value = hist_newton['f'][-1] # Plotting plot_performance(hist_newton['f'], optimal_value, 'b', "Метод Ньютона", time_vec=hist_newton['elaps']) plot_performance(hist_hfn['f'], optimal_value, 'r', "Неточный Метод\n Ньютона", time_vec=hist_hfn['elaps']) plt.ylabel(r'$\log(f_k - f_*)$') plt.xlabel("Время (сек)") if real_data: title = "Набор данных " + data_name else: title = "Сгенерированные данные" title += ", D = " + str(dim + 1) + ", N = " + str(data_size) + "." plt.title(title) plt.legend()
def test_c2(self): """Check if argument `c2` is supported.""" hfn(self.func, self.x0, self.hess_vec, c2=0.1)
def test_c1(self): """Check if argument `c1` is supported.""" hfn(self.func, self.x0, self.hess_vec, c1=0.2)
def test_max_iter(self): """Check if argument `max_iter` is supported.""" hfn(self.func, self.x0, self.hess_vec, max_iter=15)
def test_tol(self): """Check if argument `tol` is supported.""" hfn(self.func, self.x0, self.hess_vec, tol=1e-6)