Add files via upload
This commit is contained in:
parent
2a57f7935e
commit
63937159cc
2 changed files with 32 additions and 21 deletions
|
|
@ -19,9 +19,11 @@ from sympy.parsing.sympy_parser import parse_expr
|
|||
from sympy import Symbol, lambdify, N
|
||||
|
||||
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)
|
||||
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 = {}
|
||||
unsnapped_param_dict = {'p':1}
|
||||
|
||||
|
|
@ -75,7 +77,7 @@ def RPN_to_pytorch(data_file, math_expr, lr = 1e-2, N_epochs = 500):
|
|||
variables = variables + [possible_vars[i]]
|
||||
for i in range(N_params-1):
|
||||
params = params + ["p%s" %i]
|
||||
|
||||
|
||||
symbols = params + variables
|
||||
|
||||
f = lambdify(symbols, N(eq), torch)
|
||||
|
|
@ -90,13 +92,11 @@ def RPN_to_pytorch(data_file, math_expr, lr = 1e-2, N_epochs = 500):
|
|||
trainable_parameters = trainable_parameters + [vars()[i]]
|
||||
|
||||
# Prepare the loaded data
|
||||
|
||||
real_variables = []
|
||||
for i in range(len(data[0])-1):
|
||||
real_variables = real_variables + [torch.from_numpy(data[:,i]).float()]
|
||||
|
||||
input = trainable_parameters + real_variables
|
||||
|
||||
y = torch.from_numpy(data[:,-1]).float()
|
||||
|
||||
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].grad.zero_()
|
||||
|
||||
# get the updated symbolic regression
|
||||
ii = -1
|
||||
complexity = 0
|
||||
for parm in unsnapped_param_dict:
|
||||
if ii == -1:
|
||||
ii = ii + 1
|
||||
else:
|
||||
eq = eq.subs(parm, trainable_parameters[ii])
|
||||
complexity = complexity + get_number_DL_snapped(trainable_parameters[ii].detach().numpy())
|
||||
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
|
||||
ii = ii + 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,7 @@ from S_add_bf_on_numbers_on_pareto import add_bf_on_numbers_on_pareto
|
|||
from dimensionalAnalysis import dimensionalAnalysis
|
||||
|
||||
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:
|
||||
os.mkdir("results/")
|
||||
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
|
||||
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,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_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)
|
||||
|
|
@ -136,7 +136,7 @@ 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, vars_name=[],test_percentage=20):
|
||||
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:
|
||||
|
|
@ -162,9 +162,9 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
|
|||
|
||||
PA = ParetoSet()
|
||||
# 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()
|
||||
|
||||
|
||||
# Run bf snap on the resulted equations
|
||||
for i in range(len(PA_list)):
|
||||
try:
|
||||
|
|
@ -172,7 +172,7 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
|
|||
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
|
||||
|
|
@ -215,3 +215,4 @@ def run_aifeynman(pathdir,filename,BF_try_time,BF_ops_file_type, polyfit_deg=4,
|
|||
else:
|
||||
save_data = np.column_stack((log_err,log_err_all,list_dt))
|
||||
np.savetxt("results/solution_%s" %filename_orig,save_data,fmt="%s")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue