/
ml_prototype.py
276 lines (210 loc) · 8.56 KB
/
ml_prototype.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#
# This version will randomly draw N points in binary orbital parameter
# space, that plausibly merge within a Hubble time
#
import argparse
from scipy.stats import loguniform, uniform
import numpy as np
import pandas as pd
import multiprocessing
import c3o_binary_better as c3o
import tensorflow as tf
from tensorflow import keras
parser = argparse.ArgumentParser(description="Keras ML model of adiabatically extended Peters' equations for cosmological coupling",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("model",
help="Name of model to train")
parser.add_argument("--psize",
help="How many children to spawn during computation",
type=int,
default=1)
parser.add_argument("--batchsize",
help="Size of each training, validation, and testing batch",
type=int,
default=1000)
parser.add_argument("--existing",
help="Use a library of existing training data until exhausted")
#parser.add_argument("--validate",
# help="Number of random validation pulls to examine",
# type=int,
# default=1000)
#parser.add_argument("--order",
# help="Order at which to use map_coordinates()",
# type=int,
# default=1)
parser.add_argument("--k",
help="Value of k for this table",
type=float,
default=0.0)
parser.add_argument("--q",
help="Range of mass ratio q in q_min:q_max format",
default='1e-1:1')
parser.add_argument("--e",
help="Range of eccentricity e in e_min:e_max format",
default='0:0.99')
parser.add_argument("--a_DCO",
help="Range of double compact object scale factors a in a_min:a_max format",
default='0.01:0.9999')
# These variables all have pretty large ranges, so we should be using loguniform
# distributions
parser.add_argument("--R",
help="Range of semi-major axis R in R_min:R_max format",
default='1e-3:1e4')
parser.add_argument("--M",
help="Range of primary mass M in M_min:M_max format",
default='0.1:55')
parser.add_argument("--log",
help="Variables from which to pull samples uniformly in logspace",
default='R,M')
args = parser.parse_args()
# Parse out the ranges
logrange = args.log.split(',')
params = ['q', 'e', 'a_DCO', 'R', 'M']
dists = {}
for param in params:
try:
low,high = [float(x) for x in eval('args.%s' % param).split(':')]
# Replace the argument with a distribution from which random variates can be drawn
dists[param] = loguniform(low, high) if param in logrange else uniform(low, high - low)
except:
print("ded")
# Make initial values
# Establish the Datasets for TensorFlow
#train_dataset = tf.data.Dataset.from_tensors(datasets[:args.batchsize])
#for element in train_dataset:
# print(element)
#quit()
#val_dataset = tf.data.Dataset.from_tensors(datasets[args.batchsize:2*args.batchsize])
#test_dataset = tf.data.Dataset.from_tensors(datasets[2*args.batchsize:])
stash_count = 0
def get_dataset(size, psize=1, label=None, existing=None):
# Count of batches for this run
global stash_count
# If its a new model, we can train it on already existing data
dataset = None
for fname in existing:
try:
dataset = pd.read_hdf(fname, key="binaries")
if 'periastron' not in dataset.columns:
dataset['periastron'] = dataset['R']*(1. - dataset['e'])
break
except Exception as e:
print("Failed to get data from", fname, e)
continue
if dataset is None:
# Generate initial conditions
dataset = pd.DataFrame(np.asarray([dist.rvs(size=size) for dist in dists.values()]).T, columns=params)
# Set the coupling strength to be the same across everything
dataset['k'] = args.k
# Spawn offspring to compute exact results
with multiprocessing.Pool(args.psize,
initializer=c3o.setup_locking,
initargs=(multiprocessing.Lock(),)) as p:
# Take the first 2/3. The final 1/3 is for testing.
dataset['a_merger'] = pd.concat(p.map(c3o.c3o_binary_a_worker, np.array_split(dataset.copy(), psize)))
# Remove the k column, because its unnecessary for fitting
dataset.drop(columns=['k'], inplace=True)
# Add periastron
dataset['periastron'] = dataset['R']*(1. - dataset['e'])
# If stash, save it, so that we can train off of it
# later, and compare model performance
if args.existing:
# If no label is given, use the time
if label is None:
import time
label = str(time.time())
dataset.to_hdf("%s/%s_%.10d.hdf5" % (args.existing, label, stash_count), "binaries")
stash_count += 1
# Return the datasets
return dataset
# Now build a Keras model
from tensorflow.keras import layers
# Create a MirroredStrategy.
strategy = tf.distribute.MirroredStrategy()
callbacks = [
keras.callbacks.ModelCheckpoint(
filepath='path/to/my/model_{epoch}',
save_freq='epoch'),
keras.callbacks.TensorBoard(log_dir='./logs')
]
# Open a strategy scope.
#with strategy.scope():
try:
model = keras.models.load_model(args.model)
print("Loaded model from", args.model)
except:
print("Did not find model at", args.model, "\nMaking a new one...")
# Everything that creates variables should be under the strategy scope.
# In general this is only model construction & `compile()`.
# The model takes 5 input parameters, goes through 4 dense layers, and outputs a single number
inputs = keras.Input(shape=(len(params)+1,), name='BinarySpec')
categorizer = layers.Dense(5, activation="relu")(inputs)
valler = layers.Dense(32, activation="linear")(inputs)
x = layers.Concatenate()([categorizer, valler])
x = layers.Dense(32, activation="linear")(x)
x = layers.Dense(1, activation="sigmoid")(x)
outputs = layers.Rescaling(2)(x)
model = keras.Model(inputs=inputs, outputs=outputs)
loss = tf.keras.losses.MeanAbsolutePercentageError(
reduction="auto", name="mean_absolute_percentage_error"
)
model.compile(
optimizer='adam',
loss=loss)
#metrics=[tf.keras.metrics.RootMeanSquaredError()])
#model.compile(optimizer='adam',
# loss=loss)
model.summary()
# List all existing data if present
if args.existing:
import glob
existing_files = glob.glob("%s/*.hdf5" % args.existing)
if len(existing_files):
print("Successfully found", len(existing_files), "data files. Will pull from these first.")
existing_files = iter(existing_files)
else:
print("No data found.")
# Label these batches with the start time
import time
label = str(time.time())
while True:
# Get a dataset
training = get_dataset(args.batchsize,
args.psize,
label=label,
existing=existing_files)
print(training)
# Get targets
targets = training['a_merger']
print(targets)
# Get inputs
inputs = training.drop(columns=['a_merger'])
# Now train on this batch
model.train_on_batch(inputs[100:].to_numpy(),
targets[100:].to_numpy())
# How did we do?
print("Performance: ", model.test_on_batch(inputs[:100].to_numpy(),
targets[:100].to_numpy()))
# Visualize performance
derp = training[:100].copy();
derp['estimates'] = model.predict(inputs[:100].to_numpy())
print(derp)
model.save(args.model)
# import time
# # Get a test run
# t1 = time.time()
# dataset, mergers = get_dataset(1000, args.psize)
# t2 = time.time()
# print("Generation took: ", (t2-t1)*args.psize/1000, "s per binary")
# durs = []
# for i in range(10):
# t1 = time.time()
# model.evaluate(datasets[i*100:(i+1)*100].to_numpy())
# t2 = time.time()
# delta = (t2-t1)/100.
# print("Eval took: ", delta)
# durs.append(delta)
# # Final stats, dropping the bootstrap one that has to cache it
# durs = np.asarray(durs[1:])
# print(durs)
# print("Delta:", np.average(durs), "+/-", np.sqrt(np.var(durs)))