Add files via upload

This commit is contained in:
Silviu Marian Udrescu 2020-03-23 03:35:19 -04:00 committed by GitHub
parent 5a301e2f48
commit f0cc7dfcaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 1210 additions and 182 deletions

View file

@ -20,7 +20,8 @@ from S_get_symbolic_expr_error import get_symbolic_expr_error
from S_add_snap_expr_on_pareto import add_snap_expr_on_pareto
from S_add_sym_on_pareto import add_sym_on_pareto
from S_run_bf_polyfit import run_bf_polyfit
from S_final_gd import final_gd
from dimensionalAnalysis import dimensionalAnalysis
PA = ParetoSet()
@ -36,8 +37,7 @@ def run_AI_all(pathdir,filename,BF_try_time=60,BF_ops_file_type="14ops", polyfit
# Run bf and polyfit
PA = run_bf_polyfit(pathdir,pathdir,filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
'''
# 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_asin(pathdir,"results/mystery_world_asin/",filename,BF_try_time,BF_ops_file_type, PA, polyfit_deg)
@ -50,13 +50,21 @@ 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_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)
'''
#############################################################################################################################
# check if the NN is trained. If it is not, train it on the data.
print("Checking for symmetry \n", filename)
if path.exists("results/NN_trained_models/models/" + filename + ".h5") or len(data[0])<3:
if len(data[0])<3:
print("Just one variable!")
pass
elif path.exists("results/NN_trained_models/models/" + filename + ".h5"):# or len(data[0])<3:
print("NN already trained \n")
print("NN loss: ", NN_eval(pathdir,filename), "\n")
elif path.exists("results/NN_trained_models/models/" + filename + "_pretrained.h5"):
print("Found pretrained NN \n")
NN_train(pathdir,filename,NN_epochs/2,lrs=1e-3,N_red_lr=3,pretrained_path="results/NN_trained_models/models/" + filename + "_pretrained.h5")
print("NN loss after training: ", NN_eval(pathdir,filename), "\n")
else:
print("Training a NN on the data... \n")
NN_train(pathdir,filename,NN_epochs)
@ -129,27 +137,67 @@ def run_AI_all(pathdir,filename,BF_try_time=60,BF_ops_file_type="14ops", polyfit
return PA
# this runs snap on the output of aifeynman
def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4, NN_epochs=4000, DR_file=""):
def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4, NN_epochs=4000, vars_name=[],test_percentage=20):
# If the variable names are passed, do the dimensional analysis first
filename_orig = filename
try:
if vars_name!=[]:
dimensionalAnalysis(pathdir,filename,vars_name)
DR_file = filename + "_dim_red_variables.txt"
filename = filename + "_dim_red"
else:
DR_file = ""
except:
DR_file = ""
# Split the data into train and test set
input_data = np.loadtxt(pathdir+filename)
sep_idx = np.random.permutation(len(input_data))
train_data = input_data[sep_idx[0:8*len(input_data)//10]]
test_data = input_data[sep_idx[8*len(input_data)//10:len(input_data)]]
train_data = input_data[sep_idx[0:(100-test_percentage)*len(input_data)//100]]
test_data = input_data[sep_idx[test_percentage*len(input_data)//100:len(input_data)]]
np.savetxt(pathdir+filename+"_train",train_data)
np.savetxt(pathdir+filename+"_test",test_data)
if test_data.size != 0:
np.savetxt(pathdir+filename+"_test",test_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_list = PA.get_pareto_points()
PA_snapped = ParetoSet()
# Run bf snap on the resulted equations
for i in range(len(PA_list)):
try:
PA = add_bf_on_numbers_on_pareto(pathdir,filename,PA,PA_list[i][-1])
except:
continue
PA_list = PA.get_pareto_points()
np.savetxt("results/solution_before_snap_%s.txt" %filename,PA_list,fmt="%s")
# Run zero, integer and rational snap on the resulted equations
PA_snapped_1 = ParetoSet()
for j in range(len(PA_list)):
PA_snapped_1 = add_snap_expr_on_pareto(pathdir,filename,PA_list[j][-1],PA_snapped_1, "")
PA_list = PA_snapped_1.get_pareto_points()
np.savetxt("results/solution_first_snap_%s.txt" %filename,PA_list,fmt="%s")
# Run gradient descent on the data one more time
for i in range(len(PA_list)):
try:
gd_update = final_gd(pathdir+filename,PA_list[i][-1])
PA_snapped_1.add(Point(x=gd_update[1],y=gd_update[0],data=gd_update[2]))
except:
continue
PA_list = PA_snapped_1.get_pareto_points()
PA_snapped = ParetoSet()
for j in range(len(PA_list)):
PA_snapped = add_snap_expr_on_pareto(pathdir,filename,PA_list[j][-1],PA_snapped, DR_file)
list_dt = np.array(PA_snapped.get_pareto_points())
list_dt = np.array(PA_snapped.get_pareto_points())
data_file_len = len(np.loadtxt(pathdir+filename))
log_err = []
log_err_all = []
@ -160,7 +208,7 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
log_err_all = np.array(log_err_all)
# Try the found expressions on the test data
if DR_file=="":
if DR_file=="" and test_data.size != 0:
test_errors = []
for i in range(len(list_dt)):
test_errors = test_errors + [get_symbolic_expr_error(pathdir,filename+"_test",str(list_dt[i][-1]))]
@ -169,5 +217,4 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
save_data = np.column_stack((test_errors,log_err,log_err_all,list_dt))
else:
save_data = np.column_stack((log_err,log_err_all,list_dt))
np.savetxt("results/solution_%s.txt" %filename,save_data,fmt="%s")
np.savetxt("results/solution_%s.txt" %filename_orig,save_data,fmt="%s")