-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolic_regression.py
executable file
·178 lines (143 loc) · 6.17 KB
/
symbolic_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from planets_tf2 import read_data
from ml_model import LearnForces, Normalize_gn
from helper_functions import cartesian_to_spherical_coordinates
import tensorflow as tf
import numpy as np
from pysr import PySRRegressor
import os
# Training variables
num_time_steps_tr = 512000 # Number of time steps for training (~27 years).
noise_level = 0.01 # Standard deviation of Gaussian noise for randomly perturbing input data
# One time step is 30 minutes
# An orbit for saturn is 129110 steps
num_time_steps_val = 10000 # Using few to speed up calculations
# Global constants
AU = 149.6e6 * 1000 # Astronomical Unit in meters.
DAY = 24*3600. # Day in seconds
YEAR = 365.25*DAY # Year
delta_time = (0.5/24.) # 30 minutes
MSUN = 1.9885e+30 # kg
MEARTH = 5.9724e+24 # kg
G = 6.67428e-11/AU**3*MSUN*DAY**2 # Change units of G to AU^3 MSun^{-1} Day^{-2}
A_norm = 0.00042411583592113497 # From planets_tf2 (I will change the way
# this is stored eventually)
def force_newton(x, m1, m2):
return G*m1*m2/np.linalg.norm(x, axis = -1, keepdims=True)**3.*x
def load_model(system, norm_layer, senders, receivers):
""" Load the model"""
# Restore best weights not working, but found way around using checkpoint
checkpoint_filepath = './saved_models/planetsonly_i2'
# Create a model
model = LearnForces(system.numPlanets, senders, receivers, norm_layer,
noise_level=noise_level)
# Compile
model.compile()
model.load_weights(checkpoint_filepath)
return model
def format_data_symreg(data_tr, data_symreg, system):
"""
Convert the data into normalized tensorflow data objects that we can
use for training
"""
nedges = system.numEdges
masses = system.get_masses()
# Create empty arrays for the distances for training and validation
D_tr = np.empty([len(data_tr), nedges, 3])
D_symreg = np.empty([len(data_symreg), nedges, 3])
F_symreg = np.empty([len(data_symreg), nedges, 3])
k = 0
# Create empty lists for the senders and receivers that will be used for
# the edges of the graph
senders, receivers = [], []
for i in range(system.numPlanets):
for j in range(system.numPlanets):
if i > j:
# For every pair of objects, assign a distance
D_tr[:, k, :] = data_tr[:, j, :3] - data_tr[:, i, :3]
D_symreg[:, k, :] = data_symreg[:, j, :3] - \
data_symreg[:, i, :3]
F_symreg[:, k, :] = force_newton(
D_symreg[:, k, :], masses[i], masses[j])
k += 1
# Add sender and receiver index
receivers.append(i)
senders.append(j)
# Flatten the arrays
D_tr = np.reshape(D_tr, [-1, 3])
# Convert them to tensors
D_tr = tf.convert_to_tensor(D_tr, dtype="float32")
# Create a normalization layer
norm_layer = Normalize_gn(cartesian_to_spherical_coordinates(D_tr))
return D_symreg, F_symreg, norm_layer, senders, receivers
def run_symbolic_regression(D, model, system, num_pts=1000, name='eqns'):
D_tf = tf.convert_to_tensor(D.reshape(-1, 3), dtype="float32")
_, F = model.call(D_tf, extract=True)
names = system.get_names()
learned_masses = model.logm_planets.numpy()
isun = names.index("sun")
learned_msun = learned_masses[isun]
learned_masses -= learned_msun
X = np.zeros([D.shape[0], D.shape[1], 6])
X[:, :, 2:5] = D
X[:, :, 5] = np.linalg.norm(D, axis=-1)
k=0
for i in range(system.numPlanets):
for j in range(system.numPlanets):
if i > j:
X[:, k, 0] = 10 ** (learned_masses[i])
X[:, k, 1] = 10 ** (learned_masses[j])
# print(fp[0,k,0], X[0,k,0]*X[0,k,1]*X[0,k,2]/X[0,k,5]**3.)
k += 1
X = X.reshape([-1, 6])
F = F.reshape([-1, 3])
y = F[:,0] #F_x
#X[:, [0, 1]] = np.exp(X[:, [0, 1]])/1e23 #re-scale to prevent precision issues, since pysr uses 32-bit floats
y /= np.std(y) #same as above
m_std = np.std(X[:,:2])
x_std = np.std(X[:,2:5])
X[:,:2]/= m_std
X[:,2:]/= x_std
idx = np.random.choice(X.shape[0], num_pts, replace=False)
X = X[idx]
y = y[idx]
# Randomly swap masses
for i in range(len(X)):
r = np.random.rand()
if r > 0.5:
X[i, 0], X[i, 1] = X[i, 1], X[i, 0]
pysr_model = PySRRegressor(populations=64,
niterations=1000,
binary_operators=["plus", "sub", "mult",
"pow", "div"],
unary_operators=["exp", "log_abs",
"sin", "cos"],
temp_equation_file=False,
equation_file=os.path.join(
"./data/saved_equations/", name),
batching=True,
batch_size=50,
progress=False,
procs=4,
annealing=False,
maxsize=40,
useFrequency=True,
optimizer_algorithm="BFGS",
optimizer_iterations=10,
optimize_probability=1.0
)
pysr_model.fit(X, y, variable_names = ['m0', 'm1', 'x', 'y', 'z', 'r'])
return pysr_model
if __name__ == "__main__":
tf.config.list_physical_devices('CPU')
tf.config.run_functions_eagerly(False)
data_tr, _, data_symreg, system = read_data(num_time_steps_tr,
num_time_steps_val)
print('Read data')
D_symreg, F_symreg, norm_layer, senders, receivers = format_data_symreg(
data_tr, data_symreg, system)
print('Formatted data')
model = load_model(system, norm_layer, senders, receivers)
print('Model loading completed')
equations = run_symbolic_regression(D_symreg, model, system,
name="planetsonly",
num_pts=5000)