def get_approximations(a_approximator): global num_of_samples s = [] for i in range(0, num_of_samples): x_sample = i * 2 * math.pi / num_of_samples y_sample = a_approximator.approximation(x_sample) s.append([x_sample, y_sample]) return s samples = get_samples() main_plot = Drawer() main_plot.add_points(points=samples) approximator = OneVariableApproximator(samples, 5) progress = [approximator.objective()] for i in range(10000): approximator.one_gradient_descent_step() progress.append(approximator.objective()) obj_function_plot = Drawer(xlimits=(0, len(progress)), ylimits=(0, 1.2 * progress[0])) for i, p in zip(range(len(progress)), progress): pair = [i, p] print(pair) obj_function_plot.add_pkoints([pair])
for x1_ind in range(x1_range + 1): for x2_ind in range(x2_range + 1): x1 = (math.pi * x1_ind) / x1_range x2 = (math.pi * x2_ind) / x2_range y = math.sin(x1) + math.cos(x2) s.append([x1, x2, y]) return s refile = open("2varSamples.json", "r") samples = json.loads(refile.read()) approximator = TwoVariableApproximator(samples, 3) progress = [approximator.objective()] for i in range(10000): approximator.one_gradient_descent_step() progress.append(approximator.objective()) print(progress[-1]) obj_function_plot = Drawer(xlimits=(0, len(progress)), ylimits=(0, 1.2 * progress[0])) for i in range(len(progress)): obj_function_plot.add_points([[i, progress[i]]]) print("bettas", approximator.bettas) print("estimate:", approximator.approximation([0.5, 0.5]), approximator.approximation([0.75, 0.25])) print("expected:", math.sin(0.5) + math.cos(0.5), math.sin(0.75) + math.cos(0.25)) obj_function_plot.draw()