Spaces:
Runtime error
Runtime error
| """"""""""""""""""""""""""""""""" | |
| This file is for running. | |
| Do not modify this file. | |
| For running: DiffEqnSolver.py | |
| For modifying: settings.py | |
| """"""""""""""""""""""""""""""""" | |
| import os | |
| import time | |
| import numpy as np | |
| from pysr import pysr, best | |
| import DataUtils | |
| import Settings as settings | |
| from SymbolicFunctionLearner import SFL | |
| # Dataset | |
| X = 2 * np.random.randn(100, 5) | |
| y = 2 * np.cos(X[:, 3]) + X[:, 0] ** 2 - 2 | |
| # Learn equations | |
| equations = pysr(X, y, niterations=5, | |
| binary_operators=["plus", "mult"], | |
| unary_operators=[ | |
| "cos", "exp", "sin"]) | |
| # Pre-defined library of operators (see https://pysr.readthedocs.io/en/latest/docs/operators/) | |
| # "inv(x) = 1/x"]) # Define your own operator! (Julia syntax) | |
| # (you can use ctl-c to exit early) | |
| print(best(equations)) | |
| settings.mode = "sr" | |
| if not os.path.exists('images'): | |
| os.makedirs('images') | |
| input_file = open("FeynmanEqns.txt", "r") | |
| input_lines = input_file.readlines() | |
| input_file.close() | |
| for line in input_lines[1:]: | |
| line_parts = line.strip().split(";") | |
| eqn_name = line_parts[0].strip() | |
| settings.n_tree_layers = int(line_parts[1].strip()) | |
| settings.num_features = int(line_parts[2].strip()) | |
| eqn_str = line_parts[3].strip() | |
| print("True equation: {}".format(eqn_str)) | |
| settings.true_eqn = eqn_str | |
| settings.initialize_ops = eval(line_parts[4].strip()) | |
| print(settings.initialize_ops) | |
| # Set up data | |
| fixed_x = DataUtils.generate_data(settings.train_N, n_vars=settings.num_features) | |
| print(fixed_x.shape) | |
| fixed_y = DataUtils.true_function(fixed_x) | |
| print(len(fixed_y)) | |
| settings.fixed_x = [] | |
| settings.fixed_y = [] | |
| for i in range(settings.train_N): | |
| settings.fixed_x.append(fixed_x[i, :].tolist()[0]) | |
| settings.fixed_y.append(fixed_y[i]) | |
| current_model = SFL() | |
| print('\nBeginning experiment: {}'.format(current_model.name)) | |
| print("{} tree layers.".format(settings.n_tree_layers)) | |
| print("{} features of {} component(s) each.".format(settings.num_features, settings.num_dims_per_feature)) | |
| print("{} components in output.".format(settings.n_dims_in_output)) | |
| print("{} operators: {}.".format(len(current_model.function_set), | |
| current_model.function_set)) | |
| train_errors = [] | |
| valid_errors = [] | |
| test_errors = [] | |
| true_eqns = [] | |
| # train_X = DataUtils.generate_data(settings.train_N, n_vars=current_model.n_input_variables, | |
| # avoid_zero=settings.avoid_zero) | |
| # valid_X = DataUtils.generate_data(settings.train_N, n_vars=current_model.n_input_variables, | |
| # avoid_zero=settings.avoid_zero) | |
| # test_X = DataUtils.generate_data(settings.test_N, n_vars=current_model.n_input_variables, | |
| # min_x=settings.test_scope[0], | |
| # max_x=settings.test_scope[1]) | |
| train_X = fixed_x | |
| test_X = fixed_x | |
| train_Y = fixed_y | |
| test_Y = fixed_y | |
| print("\n========================") | |
| print("Starting Solver.") | |
| print("==========================\n") | |
| # Train the model from scratch several times, keeping the best one. | |
| start_time = time.time() | |
| best_model, best_iter, best_err = current_model.repeat_train(train_X, train_Y, | |
| settings.num_train_repeat_processes, | |
| test_x=test_X, test_y=test_Y) | |
| running_time = time.time() - start_time | |
| print("best_model: {}".format(best_model)) | |
| print("----------------------") | |
| print("Finished regression. Took {:.2f} minutes.\n".format(running_time / 60)) | |
| print("Final solution found at attempt {}:".format(best_iter)) | |
| print("y = {}".format(best_model)) | |
| print("Test error: {}".format(best_err)) | |
| if best_err < 0.02: | |
| print("Attained error less than 0.02 - great!") | |
| print() | |