Add files via upload

This commit is contained in:
Silviu Marian Udrescu 2020-05-15 04:08:52 -04:00 committed by GitHub
parent 2a57f7935e
commit 63937159cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 21 deletions

View file

@ -19,9 +19,11 @@ from sympy.parsing.sympy_parser import parse_expr
from sympy import Symbol, lambdify, N from sympy import Symbol, lambdify, N
from S_get_number_DL_snapped import get_number_DL_snapped from S_get_number_DL_snapped import get_number_DL_snapped
from S_get_symbolic_expr_error import get_symbolic_expr_error
# parameters: path to data, RPN expression (obtained from bf) # parameters: path to data, RPN expression (obtained from bf)
def RPN_to_pytorch(data_file, math_expr, lr = 1e-2, N_epochs = 500): def RPN_to_pytorch(pathdir,filename, math_expr, lr = 1e-2, N_epochs = 500):
data_file = pathdir + filename
param_dict = {} param_dict = {}
unsnapped_param_dict = {'p':1} unsnapped_param_dict = {'p':1}
@ -90,13 +92,11 @@ def RPN_to_pytorch(data_file, math_expr, lr = 1e-2, N_epochs = 500):
trainable_parameters = trainable_parameters + [vars()[i]] trainable_parameters = trainable_parameters + [vars()[i]]
# Prepare the loaded data # Prepare the loaded data
real_variables = [] real_variables = []
for i in range(len(data[0])-1): for i in range(len(data[0])-1):
real_variables = real_variables + [torch.from_numpy(data[:,i]).float()] real_variables = real_variables + [torch.from_numpy(data[:,i]).float()]
input = trainable_parameters + real_variables input = trainable_parameters + real_variables
y = torch.from_numpy(data[:,-1]).float() y = torch.from_numpy(data[:,-1]).float()
for i in range(N_epochs): for i in range(N_epochs):
@ -109,21 +109,31 @@ def RPN_to_pytorch(data_file, math_expr, lr = 1e-2, N_epochs = 500):
trainable_parameters[j] -= lr * trainable_parameters[j].grad trainable_parameters[j] -= lr * trainable_parameters[j].grad
trainable_parameters[j].grad.zero_() trainable_parameters[j].grad.zero_()
# get the updated symbolic regression
ii = -1 ii = -1
complexity = 0
for parm in unsnapped_param_dict: for parm in unsnapped_param_dict:
if ii == -1: if ii == -1:
ii = ii + 1 ii = ii + 1
else: else:
eq = eq.subs(parm, trainable_parameters[ii]) eq = eq.subs(parm, trainable_parameters[ii])
complexity = complexity + get_number_DL_snapped(trainable_parameters[ii].detach().numpy()) ii = ii + 1
n_variables = len(eq.free_symbols)
n_operations = len(count_ops(eq,visual=True).free_symbols)
if n_operations!=0 or n_variables!=0:
complexity = complexity + (n_variables+n_operations)*np.log2((n_variables+n_operations))
ii = ii+1
error = torch.mean((f(*input)-y)**2).data.numpy()*1 complexity = 0
is_atomic_number = lambda expr: expr.is_Atom and expr.is_number
numbers_expr = [subexpression for subexpression in preorder_traversal(eq) if is_atomic_number(subexpression)]
complexity = 0
for j in numbers_expr:
try:
complexity = complexity + get_number_DL_snapped(float(j))
except:
complexity = complexity + 1000000
n_variables = len(eq.free_symbols)
n_operations = len(count_ops(eq,visual=True).free_symbols)
if n_operations!=0 or n_variables!=0:
complexity = complexity + (n_variables+n_operations)*np.log2((n_variables+n_operations))
error = get_symbolic_expr_error(pathdir,filename,str(eq))
return error, complexity, eq return error, complexity, eq

View file

@ -25,8 +25,7 @@ from S_add_bf_on_numbers_on_pareto import add_bf_on_numbers_on_pareto
from dimensionalAnalysis import dimensionalAnalysis from dimensionalAnalysis import dimensionalAnalysis
PA = ParetoSet() PA = ParetoSet()
def run_AI_all(pathdir,filename,BF_try_time=60,BF_ops_file_type="14ops", polyfit_deg=4, NN_epochs=4000, PA=PA):
def run_AI_all(pathdir,filename,BF_try_time=60,BF_ops_file_type="14ops", polyfit_deg=4, NN_epochs=4000, PA = PA):
try: try:
os.mkdir("results/") os.mkdir("results/")
except: except:
@ -38,6 +37,7 @@ def run_AI_all(pathdir,filename,BF_try_time=60,BF_ops_file_type="14ops", polyfit
# Run bf and polyfit # Run bf and polyfit
PA = run_bf_polyfit(pathdir,pathdir,filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg) PA = run_bf_polyfit(pathdir,pathdir,filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
'''
# Run bf and polyfit on modified output # Run bf and polyfit on modified output
PA = get_acos(pathdir,"results/mystery_world_acos/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg) PA = get_acos(pathdir,"results/mystery_world_acos/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
PA = get_asin(pathdir,"results/mystery_world_asin/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg) PA = get_asin(pathdir,"results/mystery_world_asin/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
@ -50,7 +50,7 @@ def run_AI_all(pathdir,filename,BF_try_time=60,BF_ops_file_type="14ops", polyfit
PA = get_sqrt(pathdir,"results/mystery_world_sqrt/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg) PA = get_sqrt(pathdir,"results/mystery_world_sqrt/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
PA = get_squared(pathdir,"results/mystery_world_squared/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg) PA = get_squared(pathdir,"results/mystery_world_squared/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
PA = get_tan(pathdir,"results/mystery_world_tan/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg) PA = get_tan(pathdir,"results/mystery_world_tan/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
'''
############################################################################################################################# #############################################################################################################################
# check if the NN is trained. If it is not, train it on the data. # check if the NN is trained. If it is not, train it on the data.
print("Checking for symmetry \n", filename) print("Checking for symmetry \n", filename)
@ -162,7 +162,7 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
PA = ParetoSet() PA = ParetoSet()
# Run the code on the train data # Run the code on the train data
PA = run_AI_all(pathdir,filename+"_train",BF_try_time,BF_ops_file_type, polyfit_deg, NN_epochs) PA = run_AI_all(pathdir,filename+"_train",BF_try_time,BF_ops_file_type, polyfit_deg, NN_epochs, PA=PA)
PA_list = PA.get_pareto_points() PA_list = PA.get_pareto_points()
# Run bf snap on the resulted equations # Run bf snap on the resulted equations
@ -215,3 +215,4 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
else: else:
save_data = np.column_stack((log_err,log_err_all,list_dt)) save_data = np.column_stack((log_err,log_err_all,list_dt))
np.savetxt("results/solution_%s" %filename_orig,save_data,fmt="%s") np.savetxt("results/solution_%s" %filename_orig,save_data,fmt="%s")