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 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