def compare_oracles(oracle, oracle_optimized, x_init): """ :param oracle: Oracle model for LogReg :param oracle_optimized: Oracle model for optimized LogReg :param x_init: Initial point for optimization :return: """ [_, _, history] = optimization.gradient_descent(oracle, x_init, line_search_options={ 'method': 'Wolfe', 'c': 1 }, trace=True) [_, _, history_optimized] = optimization.gradient_descent(oracle_optimized, x_init, line_search_options={ 'method': 'Wolfe', 'c': 1 }, trace=True) plot_function_values_vs_iteration(history, history_optimized) plot_function_values_vs_time(history, history_optimized) plot_grad_norm_vs_time(history, history_optimized)
def test_gd_1d(): oracle = get_1d(0.5) x0 = np.array([1.0]) FUNC = [ np.array([2.14872127]), np.array([0.8988787]), np.array([0.89869501]), np.array([0.89869434]), np.array([0.89869434]), ] GRAD_NORM = [ 1.8243606353500641, 0.021058536428132546, 0.0012677045924299746, 7.5436847232768223e-05, 4.485842052370792e-06, ] TIME = [0] * 5 # Dummy values. X = [ np.array([1.]), np.array([-0.42528175]), np.array([-0.40882976]), np.array([-0.40783937]), np.array([-0.40778044]), ] TRUE_HISTORY = { 'func': FUNC, 'grad_norm': GRAD_NORM, 'time': TIME, 'x': X, } # Armijo rule. x_star, msg, history = optimization.gradient_descent(oracle, x0, max_iter=5, tolerance=1e-10, trace=True, line_search_options={ 'method': 'Armijo', 'alpha_0': 100, 'c1': 0.3, }) ok_(np.allclose(x_star, [-0.4077], atol=1e-3)) eq_(msg, 'success') check_equal_histories(history, TRUE_HISTORY) # Constant step size. x_star, msg, history = optimization.gradient_descent(oracle, x0, max_iter=5, tolerance=1e-10, trace=False, line_search_options={ 'method': 'Constant', 'c': 1.0, }) ok_(np.allclose(x_star, [-0.4084371], atol=1e-2)) eq_(msg, 'iterations_exceeded') eq_(history, None)
def get_iteration_num_for_fixed_n(condition_num_values, n, x_init, method_name, b_min=-1, b_max=1): """ Returns an array of iterations number for gradient descent for each value in condition_num_values for given n :param condition_num_values: array of condition numbers :param n: number of features :param x_init: initial point for gradient descent :param b_min: min value in b :param b_max: max value in b :param method_name: 'Wolfe', 'Armijo' or 'Constant' :return: an array with number of iterations for each value in condition_num_values """ iteration_num_values = [] for condition_num in condition_num_values: A, b = generate_task(condition_num, n, b_min, b_max) oracle = oracles.QuadraticOracle(A, b) [x_star, _, history] = optimization.gradient_descent(oracle, x_init, \ line_search_options={'method': method_name, 'c': 0.001}, \ trace=True) # print(x_star) # print(history['grad_norm']) # print("====================================") iteration_num_values.append(len(history['grad_norm'])) return iteration_num_values
def linearRegression(w,x,y): """linear regression model input training data w:weight and bias x:variable y:actual result output expected w date:2020/1/18 """ #set parameters learning_rate=0.0000001 iteration_times=1000 #iteration for i in range(iteration_times): #g=2X^T(Xw-y) gradient=2*np.dot(x.transpose(),np.dot(x,w)-y) #gradient iteration w=gradient_descent(w,gradient,learning_rate) #calculate the cost print("R2 cost:",R2(y,np.dot(x,w))) return w
def plot_results(dataset): """ Plots function values dependency and squared norm of gradient on time for gradient descent and newton optimization methods :param dataset: One of 'w8a', 'gissete', 'real-sim' :return: """ available_datasets = ['w8a', 'gissete', 'real-sim'] if dataset not in available_datasets: raise ValueError( "Dataset {0} currently is not supported. Available datasets are: {1}" .format(dataset, ' '.join(available_datasets))) A, b = load_svmlight_file('./data/{}'.format(dataset)) oracle = oracles.create_log_reg_oracle(A, b, 1 / len(b)) x_init = np.zeros(A.shape[1]) [_, _, history_grad] = optimization.gradient_descent(oracle, x_init, line_search_options={ 'method': 'Wolfe', 'c': 1 }, trace=True) [_, _, history_newton] = optimization.newton(oracle, x_init, line_search_options={ 'method': 'Wolfe', 'c': 1 }, trace=True) plot_function_values_on_time(dataset, history_grad, history_newton) plot_grad_norm_values_on_time(dataset, history_grad, history_newton)
def run_trajectory(x_0, A, b=np.zeros((2, )), tag=""): oracle = QuadraticOracle(A, b) func = oracle.func line_search_options = [{ 'method': 'Constant', 'c': 0.1 }, { 'method': 'Constant', 'c': 1.0 }, { 'method': 'Armijo' }, { 'method': 'Wolfe' }] Path("pics/" + tag).mkdir(parents=True, exist_ok=True) for options in line_search_options: x_opt, message, history = gradient_descent(oracle, x_0, trace=True, line_search_options=options) plt.figure() plot_levels(func) plot_trajectory(func, history['x']) filename = "pics/{2}/{0}{1}.png".format( options['method'], "_" + str(options['c']) if 'c' in options else "", tag) print("{}: {}".format(options['method'], len(history['x']))) plt.savefig(filename)
def first_experiment(): A_good = np.array([[1, 0.2], [0.2, 1.2]]) A_bad = np.array([[0.1, 0.1], [0.1, 1]]) for i, A in enumerate([A_good, A_bad]): cond = np.linalg.cond(A) oracle = oracles.QuadraticOracle(A, np.zeros(2)) np.random.seed(i * 42) x_0_s = [np.random.uniform(-5, 5, size=2) for _ in range(3)] for j in range(3): x_0 = x_0_s[j] for method in ['Wolfe', 'Armijo', 'Constant']: _, _, history = optimization.gradient_descent( oracle, x_0, line_search_options={ 'method': method, 'alpha_0': 100 }, trace=True) plot_trajectory_2d.plt.figure() plot_trajectory_2d.plot_levels(oracle.func) name = 'experiment_1/{}-{}-{}'.format(round(cond, 3), method, j) plot_trajectory_2d.plot_trajectory(oracle.func, history['x'], save=name) print('cond = {}, j = {}, method = {}, steps = {}'.format( round(cond, 3), j, method, len(history['x'])))
def save_comparison_plot(oracle, x_init, xrange=None, yrange=None, levels=None, plot_name='plot'): """ Save trajectory plot for three different optimization methods (Wolfe, Armijo and Constant) :param oracle: Oracle model :param x_init: Initial point for optimization :param xrange: Range of x values for plot_levels() :param yrange: Range of y values for plot_levels() :param levels: Range of level values for plot_levels() :return: None """ [_, _, history_wolf ] = optimization.gradient_descent(oracle, x_init, line_search_options={'method': 'Wolfe'}, trace=True) [_, _, history_armijo] = optimization.gradient_descent( oracle, x_init, line_search_options={'method': 'Armijo'}, trace=True) [_, _, history_constant] = optimization.gradient_descent(oracle, x_init, line_search_options={ 'method': 'Constant', 'c': 0.01 }, trace=True) plot_levels(oracle.func, xrange=xrange, yrange=yrange, levels=levels) plot_trajectory(oracle.func, history_constant['x'], color='red') plot_trajectory(oracle.func, history_armijo['x'], color='orange') plot_trajectory(oracle.func, history_wolf['x'], color='blue') plt.xlabel('x') plt.ylabel('y') plt.legend([ 'Constant, iterations number = {}'.format( len(history_constant['x']) - 1), 'Armije, iterations number = {}'.format(len(history_armijo['x']) - 1), 'Wolfe, iterations number = {}'.format(len(history_wolf['x']) - 1) ]) plt.savefig('./3.1/{}'.format(plot_name)) plt.show()
def experiment_3(): np.random.seed(31415) data_path = "data" datasets = ["w8a", "gisette_scale", "real-sim", "news20", "rcv1_train"] for dataset in datasets: print("___________________________") logging.info(f"{dataset} is in process...") A, b = load_svmlight_file(os.path.join(data_path, dataset)) print(A.shape, 1 - A.size / (A.shape[0] * A.shape[1])) m = b.size oracle = create_log_reg_oracle(A, b, 1.0 / m, "optimized") x_0 = np.zeros((A.shape[1],)) x_opt1, message, history1 = gradient_descent(oracle, x_0, trace=True) logging.info("GD ended") x_opt2, message, history2 = hessian_free_newton(oracle, x_0, trace=True) logging.info("HFN ended") x_opt3, message, history3 = lbfgs(oracle, x_0, trace=True) logging.info("L-BFGS ended") os.makedirs("report/pics/3", exist_ok=True) plt.figure() plt.plot(history1['func'], label='GD') plt.plot(history2['func'], label='HFN') plt.plot(history3['func'], label='L-BFGS') print(f"GD iterations={len(history1['func'])}, time={history1['time'][-1]}") print(f"HFN iterations={len(history2['func'])}, time={history2['time'][-1]}") print(f"L-BFGS iterations={len(history3['func'])}, time={history3['time'][-1]}") plt.xlabel('Номер итерации') plt.ylabel('Значение функции потерь') plt.legend() plt.grid() plt.savefig(f"report/pics/3/logreg_loss_value_vs_iter_{dataset}.pdf", bbox_inches='tight') plt.figure() plt.plot(history1['time'], history1['func'], label='GD') plt.plot(history2['time'], history2['func'], label='HFN') plt.plot(history3['time'], history3['func'], label='L-BFGS') plt.xlabel('Время от начала эксперимента в секундах') plt.ylabel('Значение функции потерь') plt.legend() plt.grid() plt.savefig(f"report/pics/3/logreg_loss_value_vs_time_{dataset}.pdf", bbox_inches='tight') plt.figure() plt.plot(history1['time'], (history1['grad_norm'] / history1['grad_norm'][0]) ** 2, label='GD') plt.plot(history2['time'], (history2['grad_norm'] / history2['grad_norm'][0]) ** 2, label='HFN') plt.plot(history3['time'], (history3['grad_norm'] / history3['grad_norm'][0]) ** 2, label='L-BFGS') plt.yscale('log') plt.xlabel('Время от начала эксперимента в секундах') plt.ylabel('Относительный квадрат нормы градиента') plt.legend() plt.grid() plt.savefig(f"report/pics/3/logreg_grad_norm_vs_time_{dataset}.pdf", bbox_inches='tight')
def experiment_3(): np.random.seed(31415) m, n = 10000, 8000 A = np.random.randn(m, n) b = np.sign(np.random.randn(m)) regcoef = 1 / m oracle1 = create_log_reg_oracle(A, b, regcoef, oracle_type='usual') oracle2 = create_log_reg_oracle(A, b, regcoef, oracle_type='optimized') x_0 = np.zeros((n, )) x_opt1, message, history1 = gradient_descent(oracle1, x_0, trace=True) x_opt2, message, history2 = gradient_descent(oracle2, x_0, trace=True) print(x_opt1, x_opt2) plt.figure() plt.plot(history1['func'], label='Usual') plt.plot(history2['func'], label='Optimized') plt.xlabel('Номер итерации') plt.ylabel('Значение функции потерь') plt.legend() plt.grid() plt.savefig("pics/logreg_values") plt.figure() plt.plot(history1['time'], history1['func'], label='Usual') plt.plot(history2['time'], history2['func'], label='Optimized') plt.xlabel('Время от начала эксперимента в секундах') plt.ylabel('Значение функции потерь') plt.legend() plt.grid() plt.savefig("pics/logreg_loss_value_vs_time") plt.figure() plt.plot(history1['time'], 2 * np.log((history1['grad_norm'] / history1['grad_norm'][0])), label='Usual') plt.plot(history2['time'], 2 * np.log((history2['grad_norm'] / history2['grad_norm'][0])), label='Optimized') plt.xlabel('Время от начала эксперимента в секундах') plt.ylabel('Логарифм относительного квадрата нормы градиента') plt.legend() plt.grid() plt.savefig("pics/logreg_grad_norm_vs_time")
def analyze_on_click(self): if self.field_entry_x.get_text() is None: return if self.field_entry_y.get_text() is None: return if self.field_alpha.get_text() is None: return if self.field_tolerance.get_text() is None: return if self.iterations_field.get_text() is None: return file_x = open(self.field_entry_x.get_text(), 'rU') file_y = open(self.field_entry_y.get_text(), 'rU') if file_x is not None and file_y is not None: alpha = float(self.field_alpha.get_text()) tol = float(self.field_tolerance.get_text()) it = float(self.iterations_field.get_text()) samples_on_x = [] for line in file_x: elements = line.strip().split(',') for i in range(len(elements)): elements[i] = float(elements[i]) samples_on_x.append(elements) samples_on_y = [] for line in file_y: elements = line.strip().split(',') for i in range(len(elements)): elements[i] = float(elements[i]) samples_on_y.append(elements) result = optimization.gradient_descent(samples_on_x, samples_on_y, alpha, tol, it) if result is not None: res_path = self.field_entry_x.get_text() + '.res' response_file = open(res_path, 'w') str_res = "" for i in range(len(result)): str_res += repr(result[i]) if i < len(result) - 1: str_res += "\r" response_file.write(str_res) else: return
def analyze_wolfe(oracle, x_init, c2_values, colors): for i, c2 in enumerate(c2_values): [_, _, history] = optimization.gradient_descent(oracle, x_init, line_search_options={ 'method': 'Wolfe', 'c2': c2 }, trace=True) plot_grad_norm_vs_time(history, colors[i], 'Wolfe, c2={}'.format(c2))
def analyze_armijo(oracle, x_init, c1_values, colors): for i, c1 in enumerate(c1_values): [_, _, history] = optimization.gradient_descent(oracle, x_init, line_search_options={ 'method': 'Armijo', 'c1': c1 }, trace=True) plot_grad_norm_vs_time(history, colors[i], "Armijo, c1={}".format(c1))
def experiment_2(): np.random.seed(31415) kappas = np.linspace(1., 1000., 50) plt.figure() colors = ['red', 'green', 'blue', 'orange'] labels = ['n = 2', 'n = 10', 'n = 100', 'n = 1000'] repeat_times = 100 runs = pd.DataFrame() for i, n in enumerate([2, 10, 100, 1000]): for j in range(repeat_times): iter_num_arr = [] for kappa in kappas: D = np.random.uniform(1, kappa, n) D[0] = 1 D[-1] = kappa A = diags(D) b = np.random.uniform(-1, 1, (n, )) x_0 = np.zeros((n, )) oracle = QuadraticOracle(A, b) x_opt, message, history = gradient_descent(oracle, x_0, trace=True, max_iter=10**6) iter_num = len(history['func']) iter_num_arr.append(iter_num) runs['run_%s' % j] = iter_num_arr means = runs.mean(axis=1) stds = runs.std(axis=1) plt.plot(kappas, means, color=colors[i], label=labels[i], alpha=1.0) plt.fill_between(kappas, means - stds, means + stds, color=colors[i], alpha=0.3) plt.plot(kappas, means - stds, color=colors[i], alpha=0.7, linewidth=0.1) plt.plot(kappas, means + stds, color=colors[i], alpha=0.7, linewidth=0.1) plt.legend() plt.grid() plt.xlabel('Число обуcловленности') plt.ylabel('Число итераций до сходимости') plt.savefig("pics/iterations_vs_condition_number.pdf")
def analyze_constant(oracle, x_init, c_values, colors): for i, c in enumerate(c_values): [_, _, history] = optimization.gradient_descent(oracle, x_init, line_search_options={ 'method': 'Constant', 'c': c }, trace=True) plot_grad_norm_vs_time(history, colors[i], 'Constant, c={}'.format(c))
def third_experiment(): data_path = 'experiment_3/datasets/' result_path = 'experiment_3/' names = ['w8a', 'gisette_scale', 'real-sim'] def plotting(history_gd, history_nm, param): f_gd = np.array(history_gd[param]) f_nm = np.array(history_nm[param]) time_gd = list(map(lambda i: i.total_seconds(), history_gd['time'])) time_nm = list(map(lambda i: i.total_seconds(), history_nm['time'])) if param == 'grad_norm': f_gd = np.log(f_gd / f_gd[0]) f_nm = np.log(f_nm / f_nm[0]) plt.figure() plt.plot(time_gd, f_gd, label='GD') plt.plot(time_nm, f_nm, label='Newton') plt.xlabel('seconds') ylabel = 'func' if param == 'func' else r'$\log\left(grad\_norm\right)$' plt.ylabel(ylabel) plt.legend() plt.grid() plt.savefig(result_path + name + '-' + param) for name in names: A, b = load_svmlight_file(data_path + name) m, n = A.shape oracle = oracles.create_log_reg_oracle(A, b, 1 / m) if name != 'real-sim': print('begin') x_star_nm, _, history_nm = optimization.newton(oracle, np.zeros(n), trace=True) print('Newton is finished') x_star_gd, _, history_gd = optimization.gradient_descent(oracle, np.zeros(n), trace=True) print('GD is finished') plotting(history_gd, history_nm, 'func') plotting(history_gd, history_nm, 'grad_norm')
def second_experiment(): ns = [10, 100, 1000, 10000] colors = ['g', 'r', 'b', 'y'] kappas = list(range(1, 1000, 100)) iterations = 10 T = {} np.seterr(all='print') t0 = datetime.now() for n, color in zip(ns, colors): T[n] = [[] for _ in range(iterations)] for i in range(iterations): for kappa in kappas: np.random.seed(1000 * i + kappa) diag = np.random.uniform(low=1, high=kappa, size=n) diag[0], diag[-1] = 1, kappa A = diags(diag) b = np.random.uniform(low=1, high=kappa, size=n) oracle = oracles.QuadraticOracle(A, b) x_star, msg, history = optimization.gradient_descent( oracle, np.zeros(n), trace=True) if msg == 'success': T[n][i].append(len(history['grad_norm'])) else: T[n][i].append(10000) plt.plot(kappas, T[n][i], ls='--', color=color, alpha=0.2) plt.plot(kappas, np.mean(T[n], axis=0), color=color, label='n = {}'.format(n)) print('n = {}, time = {}'.format(n, datetime.now() - t0)) plt.grid() plt.legend() plt.ylabel('iterations') plt.xlabel(r'$\ae$') plt.savefig('experiment_2/T(n, kappa)')
def T(k): quad = oracles.QuadraticOracle(Aa(k), b) x_star, msg, history = optimization.gradient_descent( quad, np.random.uniform(10, 30, n), trace=True) return len(history['grad_norm'])
def experiment_5_and_6(algo='gd'): np.random.seed(31415) m, n = 2000, 1000 A = np.random.randn(m, n) b = np.sign(np.random.randn(m)) regcoef = 1 / m logreg_oracle = create_log_reg_oracle(A, b, regcoef, oracle_type='optimized') line_search_options = [ { 'method': 'Constant', 'c': 1.0 }, { 'method': 'Constant', 'c': 0.95 }, { 'method': 'Constant', 'c': 0.9 }, { 'method': 'Constant', 'c': 0.85 }, { 'method': 'Armijo', 'c1': 1e-8 }, { 'method': 'Armijo', 'c1': 1e-6 }, { 'method': 'Armijo', 'c1': 1e-4 }, { 'method': 'Armijo', 'c1': 1e-1 }, { 'method': 'Wolfe', 'c2': 1.5 }, { 'method': 'Wolfe', 'c2': 0.9 }, { 'method': 'Wolfe', 'c2': 0.1 }, { 'method': 'Wolfe', 'c2': 0.01 }, ] colors = ['#e66101', '#fdb863', '#b2abd2', '#5e3c99'] styles = { 'Constant': { 'linestyle': '--', 'dashes': (2, 5), 'linewidth': 2 }, 'Armijo': { 'linestyle': '--', 'dashes': (5, 2) }, 'Wolfe': { 'linestyle': 'solid' }, } x_0_list = [None] * 3 x_0_list[0] = np.zeros((n, )) x_0_list[1] = np.random.uniform(-1, 1, (n, )) x_0_list[2] = np.ones((n, )) for k, x_0 in enumerate(x_0_list): plt.figure(figsize=(12, 9)) for i, options in tqdm(enumerate(line_search_options)): if algo == 'GD': x_opt, message, history = gradient_descent( logreg_oracle, x_0, trace=True, line_search_options=options) else: x_opt, message, history = newton(logreg_oracle, x_0, trace=True, line_search_options=options) args = list(options.keys()) + list(options.values()) label = "{} ({}={}, {}={})".format(algo, args[0], args[2], args[1], args[3]) values = 2 * np.log( (history['grad_norm'] / history['grad_norm'][0])) method = args[2] plt.plot(values + np.random.randn(values.size) * 0.05, color=colors[i % len(colors)], label=label, alpha=0.7, **styles[method]) plt.xlabel('Номер итерации') plt.ylabel('Логарифм относительного квадрата нормы градиента') plt.legend(loc='upper right') plt.grid() Path("pics/logreg_{}_linear_search_strategies".format(algo)).mkdir( parents=True, exist_ok=True) plt.savefig( "pics/logreg_{}_linear_search_strategies/x_0_{}.png".format( algo, k)) np.random.seed(31415) n = 2000 C = ortho_group.rvs(n) A = C.T @ np.diag(np.random.uniform(1, 20, (n, ))) @ C b = np.random.randn(n) x_0 = np.zeros((n, )) quadratic_oracle = QuadraticOracle(A, b) x_opt = np.linalg.solve(A, b) f_opt = quadratic_oracle.func(x_opt) line_search_options = [ { 'method': 'Constant', 'c': 0.09 }, { 'method': 'Constant', 'c': 0.085 }, { 'method': 'Constant', 'c': 0.08 }, { 'method': 'Constant', 'c': 0.075 }, { 'method': 'Armijo', 'c1': 1e-10 }, { 'method': 'Armijo', 'c1': 1e-7 }, { 'method': 'Armijo', 'c1': 1e-4 }, { 'method': 'Armijo', 'c1': 1e-1 }, { 'method': 'Wolfe', 'c2': 1.5 }, { 'method': 'Wolfe', 'c2': 0.9 }, { 'method': 'Wolfe', 'c2': 0.1 }, { 'method': 'Wolfe', 'c2': 0.01 }, ] x_0_list = [None] * 3 x_0_list[0] = np.zeros((n, )) x_0_list[1] = np.random.uniform(-1, 1, (n, )) x_0_list[2] = x_opt + np.random.randn(n, ) * 0.2 for k, x_0 in enumerate(x_0_list): plt.figure(figsize=(12, 9)) for i, options in tqdm(enumerate(line_search_options)): if algo == 'GD': x_opt, message, history = gradient_descent( quadratic_oracle, x_0, trace=True, line_search_options=options) else: x_opt, message, history = newton(quadratic_oracle, x_0, trace=True, line_search_options=options) args = list(options.keys()) + list(options.values()) label = "{} ({}={}, {}={})".format(algo, args[0], args[2], args[1], args[3]) values = np.log(np.abs((history['func'] - f_opt) / f_opt) + 1e-16) method = args[2] plt.plot(values + np.random.randn(values.size) * 0.05, color=colors[i % len(colors)], label=label, alpha=0.7, **styles[method]) plt.xlabel('Номер итерации') plt.ylabel('Логарифм относительной невязки') plt.legend(loc='upper right') plt.grid() Path("pics/quadratic_{}_linear_search_strategies".format(algo)).mkdir( parents=True, exist_ok=True) plt.savefig( "pics/quadratic_{}_linear_search_strategies/x_0_{}.png".format( algo, k))
}, { "A": np.array([[25, 2], [2, 1]]), "b": np.array([0, 0]), "st": "stratched" }] for ix, x_0 in enumerate([np.array([3, 4]), np.array([1, 25])]): for ip, params in enumerate(exps): for met in ["Wolfe", "Armijo", "Constant"]: plt.clf() qua1 = oracles.QuadraticOracle(params['A'], params['b']) plot_trajectory_2d.plot_levels(qua1.func) x_star, msg, history = optimization.gradient_descent( qua1, x_0, trace=True, line_search_options={ 'method': met, 'c': 0.1 }) plot_trajectory_2d.plot_trajectory(qua1.func, history['x']) plt.savefig("exp1/{0}-{1}-{2}.png".format(x_0, params['st'], met)) for e in exps: print("{0} - {1}".format(e['st'], li.cond(e['A']))) # ------------------------ Experiment 2 np.random.seed(7412) plt.clf()
x_values, y_values = zip(*history) plt.plot(x_values, y_values, '-v', linewidth=5.0, ms=12.0, alpha=1.0, c='r', label=label) # Tries to adapt axis-ranges for the trajectory: if fit_axis: xmax, ymax = np.max(x_values), np.max(y_values) COEF = 1.5 xrange = [-xmax * COEF, xmax * COEF] yrange = [-ymax * COEF, ymax * COEF] plt.xlim(xrange) plt.ylim(yrange) plt.show() if __name__ == '__main__': A = np.eye(2) b = np.array([1, 2]) quadratic = oracles.QuadraticOracle(A, b) plot_levels(quadratic.func) x_star, msg, history = optimization.gradient_descent(quadratic, np.array([3.0, 1.5]), trace=True) plot_trajectory(quadratic.func, history['x'])
import oracles from plot_trajectory_2d import plot_levels from plot_trajectory_2d import plot_trajectory import optimization import numpy as np import matplotlib.pyplot as plt oracle = oracles.QuadraticOracle(np.array([[1.0, 2.0], [2.0, 5.0]]), np.zeros(2)) [x_star, msg, history] = optimization.gradient_descent(oracle, np.array([3.0, 1.5]), trace=True) plt.figure() plot_levels(oracle.func) plot_trajectory(oracle.func, history['x']) plt.plot()