-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.py
507 lines (379 loc) · 20.2 KB
/
Functions.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import tensorflow as tf
# from tensorflow import keras
# tf.enable_eager_execution()
# print(tf.executing_eagerly)
import numpy as np
import h5py
# import pandas as pd
# from scipy.optimize import curve_fit
print(tf.__version__)
def TFR_map_func(sequence_example):
'''
This map function returns the data in (input,label) pairs. A new dataset is created
:param sequence_example:
:return:
'''
context_features = {'VN_coeff': tf.FixedLenFeature(shape=(200), dtype=tf.float32)}
sequence_features = {'spectra': tf.FixedLenSequenceFeature(shape=(100), dtype=tf.float32)}
data = tf.parse_single_sequence_example(
sequence_example,
context_features=context_features,
sequence_features=sequence_features
)
return data[1]['spectra'], data[0]['VN_coeff']
def TFR_map_func_pulse(sequence_example):
'''
This map function returns the data in (input,label) pairs. A new dataset is created
:param sequence_example:
:return:
'''
context_features = {'Pulse_truth': tf.FixedLenFeature(shape=(200), dtype=tf.float32)}
sequence_features = {'spectra': tf.FixedLenSequenceFeature(shape=(100), dtype=tf.float32)}
data = tf.parse_single_sequence_example(
sequence_example,
context_features=context_features,
sequence_features=sequence_features
)
return data[1]['spectra'], data[0]['Pulse_truth']
def input_TFR_functor(TFRecords_file_list=[], long=100000, repeat=1, batch_size=64):
filenames = tf.data.Dataset.from_tensor_slices(TFRecords_file_list)
dataset = tf.data.TFRecordDataset(filenames)
# dataset = dataset.map(TFR_map_func)
dataset = dataset.map(TFR_map_func_pulse)
dataset = dataset.shuffle(long).repeat(count=repeat).batch(batch_size=batch_size)
return dataset.make_one_shot_iterator().get_next()
def input_hdf5_functor(transfer='reformed_spectra_final.hdf5', select=(0, 1000), batch_size=64):
h5_reformed = h5py.File(transfer, 'r')
if 'Spectra16' not in h5_reformed:
raise Exception('No "Spectra16" in file.')
else:
Spectra16 = h5_reformed['Spectra16']
if 'VN_coeff' not in h5_reformed:
raise Exception('No "VN_coeff" in file.')
else:
VN_coeff = h5_reformed['VN_coeff']
# random_sample = np.random.random_integers(0, Spectra16.shape[0] - 1, select)
# random_sorted = np.sort(random_sample)
# selections = np.arange(select[0], select[1], 10)
# Spectra16_select = Spectra16[selections, ...]
# VN_coeff_select = VN_coeff[selections, ...]
# VN_coeff_select_expand = np.concatenate((VN_coeff_select.real, VN_coeff_select.imag), axis=1)
# Spectra16_select = Spectra16[select[0]:select[1], ...]
# VN_coeff_select = VN_coeff[select[0]:select[1], ...]
# VN_coeff_select_expand = np.concatenate((VN_coeff_select.real, VN_coeff_select.imag), axis=1)
Spectra16_select = Spectra16[select, ...]
VN_coeff_select = VN_coeff[select, ...]
VN_coeff_select_expand = np.concatenate((np.abs(VN_coeff_select), np.angle(VN_coeff_select)), axis=1)
dataset = tf.data.Dataset.from_tensor_slices((Spectra16_select, VN_coeff_select_expand))
dataset = dataset.shuffle(Spectra16_select.shape[0]).repeat().batch(batch_size)
h5_reformed.close()
return dataset.make_one_shot_iterator().get_next()
# return dataset
def evaluate_hdf5_functor(transfer='reformed_spectra_final.hdf5', select=(0, 1000), batch_size=1000):
h5_reformed = h5py.File(transfer, 'r')
if 'Spectra16' not in h5_reformed:
raise Exception('No "Spectra16" in file.')
else:
Spectra16 = h5_reformed['Spectra16']
if 'VN_coeff' not in h5_reformed:
raise Exception('No "VN_coeff" in file.')
else:
VN_coeff = h5_reformed['VN_coeff']
random_sample = np.random.random_integers(0, Spectra16.shape[0] - 1, select)
random_sorted = np.sort(random_sample)
Spectra16_select = Spectra16[select[0]:select[1], ...]
VN_coeff_select = VN_coeff[select[0]:select[1], ...]
VN_coeff_select_expand = np.concatenate((VN_coeff_select.real, VN_coeff_select.imag), axis=1)
dataset = tf.data.Dataset.from_tensor_slices((Spectra16_select, VN_coeff_select_expand))
dataset = dataset.shuffle(Spectra16_select.shape[0]).batch(batch_size)
h5_reformed.close()
return dataset.make_one_shot_iterator().get_next()
# return dataset
def predict_hdf5_functor(transfer='reformed_spectra_final.hdf5', select=(3000, 3001), batch_size=1):
h5_reformed = h5py.File(transfer, 'r')
if 'Spectra16' not in h5_reformed:
raise Exception('No "Spectra16" in file.')
else:
Spectra16 = h5_reformed['Spectra16']
#if 'VN_coeff' not in h5_reformed:
# raise Exception('No "VN_coeff" in file.')
#else:
# VN_coeff = h5_reformed['VN_coeff']
# Spectra16_select = Spectra16[select[0]:select[1], ...]
# VN_coeff_select = VN_coeff[select[0]:select[1], ...]
Spectra16_select = Spectra16[select, ...]
# randomizer = np.random.random_integers(low=0,high=Spectra16_select.shape[0]-1,size=Spectra16_select.shape[0])
# Spectra16_select = Spectra16_select + Spectra16_select[randomizer]
# VN_coeff_select_expand = np.concatenate((VN_coeff_select.real, VN_coeff_select.imag), axis=1)
dataset = tf.data.Dataset.from_tensor_slices((Spectra16_select))
dataset = dataset.batch(batch_size)
h5_reformed.close()
return dataset.make_one_shot_iterator().get_next()
# return dataset
def predict_hdf5_functor_scramble(transfer='reformed_spectra_final.hdf5', select=(3000, 3001), shuffle=((0), (1)),
batch_size=1):
h5_reformed = h5py.File(transfer, 'r')
if 'Spectra16' not in h5_reformed:
raise Exception('No "Spectra16" in file.')
else:
Spectra16 = h5_reformed['Spectra16']
#if 'VN_coeff' not in h5_reformed:
# raise Exception('No "VN_coeff" in file.')
#else:
# VN_coeff = h5_reformed['VN_coeff']
# Spectra16_select = Spectra16[select[0]:select[1], ...]
# VN_coeff_select = VN_coeff[select[0]:select[1], ...]
Spectra16_select = Spectra16[select, ...]
# randomizer = np.random.random_integers(low=0,high=Spectra16_select.shape[0]-1,size=Spectra16_select.shape[0])
for shuff in shuffle:
Spectra16_select = Spectra16_select + Spectra16_select[shuff, ...]
Spectra16_select = Spectra16_select/shuffle.shape[0]
# VN_coeff_select_expand = np.concatenate((VN_coeff_select.real, VN_coeff_select.imag), axis=1)
dataset = tf.data.Dataset.from_tensor_slices((Spectra16_select))
dataset = dataset.batch(batch_size)
h5_reformed.close()
return dataset.make_one_shot_iterator().get_next()
# return dataset
def input_hdf5_functor_map(data, labels, batch_size):
data_shape = data.shape
labels_shape = labels.shape
data.sort() # limit 300 entries max per cookie.
labels = np.reshape(labels, (labels_shape[0], labels_shape[1] * labels_shape[2]))
dataset = tf.data.Dataset.from_tensor_slices((data[:, :, -300:-1], labels))
dataset = dataset.shuffle(data_shape[0]).repeat().batch(batch_size)
return dataset.make_one_shot_iterator().get_next()
# return dataset
def map_function_hdf5():
energies = tf.linspace(start=0, stop=100, num=100)
# this is done so much easier in numpy..
def CNNmodel(features, labels, mode, params):
cookie_list = []
for cookie in range(params['NUM_COOKIES']):
net = tf.reshape(features[:, cookie, ...], shape=(-1, 100, 1))
if cookie == 0:
for filters, window in params['CNN']:
net = tf.layers.conv1d(inputs=net,
filters=filters,
kernel_size=window,
activation=tf.nn.tanh,
name='conv1D_{}'.format(filters),
reuse=False)
net = tf.layers.max_pooling1d(inputs=net,
strides=params['POOL'][0],
pool_size=params['POOL'][1]
)
cookie_list.append(tf.layers.flatten(net))
else:
for filters, window in params['CNN']:
net = tf.layers.conv1d(inputs=net,
filters=filters,
kernel_size=window,
activation=tf.nn.tanh,
name='conv1D_{}'.format(filters),
reuse=True)
net = tf.layers.max_pooling1d(inputs=net,
strides=params['POOL'][0],
pool_size=params['POOL'][1]
)
cookie_list.append(tf.layers.flatten(net))
cookie_box = tf.concat(values=cookie_list, axis=1)
for nodes in params['DENSE']:
net = tf.layers.dense(inputs=cookie_box, units=nodes, activation=tf.nn.tanh)
net = tf.layers.dense(inputs=net, units=params['OUT'], activation=tf.nn.tanh)
net = tf.layers.flatten(net)
norm_mag = tf.reduce_max(tf.abs(net[:, 0:100]), axis=1, keepdims=True)
mag = net[:, 0:100]
# norm_phase = tf.reduce_max(tf.abs(net[:, 100:200]), axis=1, keepdims=True)
phase_scale_factor = 600 * np.pi # sized to capture accumulated phase. Scales the magnitude to even error.
phase = phase_scale_factor * net[:, 100:200]
output = tf.concat((phase_scale_factor * mag, phase), axis=1)
predict = tf.concat((mag, phase), axis=1)
# norm = tf.reduce_max(tf.sqrt(net[:, 0:100] ** 2 + net[:, 100:200] ** 2), axis=1, keepdims=True)
# net = net / norm # normalize explicitly
# output = net
############### Prediction mode.
# predicted_classes = tf.argmax(logits, 1)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'output': predict,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
# alter the input labels
mag_labels = tf.cast(labels, dtype=tf.float32)[:, 0:100]
phase_labels = tf.cast(labels, dtype=tf.float32)[:, 100:200]
# phase_reshape = tf.concat((tf.cast(labels, dtype=tf.float32)[:, 100:101],
# tf.cast(labels, dtype=tf.float32)[:, 101:200] - tf.cast(labels, dtype=tf.float32)[:,100:199]), axis=1)
# phase_labels = tf.cumsum(phase_reshape*mag_labels, axis=1)
labels = tf.concat(
(phase_scale_factor * mag_labels, phase_labels * mag_labels),
axis=1)
loss = tf.losses.mean_squared_error(labels=labels, predictions=output)
accuracy = tf.metrics.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
######## Evaluation mode
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(
mode, loss=loss, eval_metric_ops={'accuracy': accuracy})
######## Train mode
# optimizer = tf.train.AdagradOptimizer(learning_rate=.01)
optimizer = tf.train.AdadeltaOptimizer(learning_rate=.01)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
if mode == tf.estimator.ModeKeys.TRAIN:
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
def CNNmodelMagPhase(features, labels, mode, params):
cookie_list = []
for cookie in range(params['NUM_COOKIES']):
net = features[:, cookie, ...]
# for filters, window in params['CNN']:
# net = tf.layers.conv1d(inputs=net, filters=filters, kernel_size=window, activation=tf.nn.relu)
# net = tf.layers.max_pooling1d(inputs=net, strides=1, pool_size=params['POOL'])
for nodes in params['COOKIE_DENSE']:
net = tf.layers.dense(inputs=net, units=nodes, activation=tf.nn.tanh)
# kernel_initializer=tf.initializers.random_uniform
cookie_list.append(net)
cookie_box = tf.concat(values=cookie_list, axis=1)
for nodes in params['DENSE']:
net = tf.layers.dense(inputs=cookie_box, units=nodes, activation=tf.nn.tanh)
net = tf.layers.dense(inputs=net, units=params['OUT'], activation=tf.nn.tanh)
net = tf.layers.flatten(net)
# normalize magnitude and phase
norm_mag = tf.reduce_max(tf.abs(net[:, 0:100]), axis=1, keepdims=True)
mag = net[:, 0:100] / norm_mag
norm_phase = tf.reduce_max(tf.abs(net[:, 0:100]), axis=1, keepdims=True)
phase = np.pi * net[:, 100:200]
# print(net)
output = tf.concat((mag, phase), axis=1)
############### Prediction mode.
# predicted_classes = tf.argmax(logits, 1)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'output': output,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
loss = tf.losses.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
accuracy = tf.metrics.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
######## Evaluation mode
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(
mode, loss=loss, eval_metric_ops={'accuracy': accuracy})
######## Train mode
optimizer = tf.train.AdagradOptimizer(learning_rate=1.1)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
if mode == tf.estimator.ModeKeys.TRAIN:
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
def FC_large_sym_model(features, labels, mode, params):
# bias and kernel clipping constraints
kernel_clip_top = 100.0051
kernel_clip_bot = -100.0051
bias_clip_top = .000001
bias_clip_bot = -.000001
cookie_list = []
for cookie in range(params['NUM_COOKIES']):
net = features[:, cookie, ...]
if cookie == 0:
for name, nodes in enumerate(params['COOKIE_DENSE']):
net = tf.layers.dense(inputs=net, units=nodes, activation=tf.nn.tanh,
name='layer_{}'.format(name),
reuse=False,
kernel_constraint=lambda x: tf.clip_by_value(x, clip_value_min=kernel_clip_bot,
clip_value_max=kernel_clip_top),
bias_constraint=lambda x: tf.clip_by_value(x, clip_value_min=bias_clip_bot,
clip_value_max=bias_clip_top))
# kernel_initializer=tf.initializers.random_uniform
cookie_list.append(net)
else:
for name, nodes in enumerate(params['COOKIE_DENSE']):
net = tf.layers.dense(inputs=net, units=nodes, activation=tf.nn.tanh,
name='layer_{}'.format(name),
reuse=True,
kernel_constraint=lambda x: tf.clip_by_value(x, clip_value_min=kernel_clip_bot,
clip_value_max=kernel_clip_top),
bias_constraint=lambda x: tf.clip_by_value(x, clip_value_min=bias_clip_bot,
clip_value_max=bias_clip_top))
# kernel_initializer=tf.initializers.random_uniform
cookie_list.append(net)
cookie_box = tf.concat(values=cookie_list, axis=1)
for nodes in params['DENSE']:
net = tf.layers.dense(inputs=cookie_box,
units=nodes,
activation=tf.nn.tanh,
kernel_constraint=lambda x: tf.clip_by_value(x, clip_value_min=kernel_clip_bot,
clip_value_max=kernel_clip_top),
bias_constraint=lambda x: tf.clip_by_value(x, clip_value_min=bias_clip_bot,
clip_value_max=bias_clip_top))
net = tf.layers.dense(inputs=net,
units=params['OUT'],
activation=tf.nn.tanh,
kernel_constraint=lambda x: tf.clip_by_value(x, clip_value_min=kernel_clip_bot,
clip_value_max=kernel_clip_top),
bias_constraint=lambda x: tf.clip_by_value(x, clip_value_min=bias_clip_bot,
clip_value_max=bias_clip_top))
net = tf.layers.flatten(net)
# normalize magnitude and phase
norm_mag = tf.reduce_max(tf.abs(net[:, 0:100]), axis=1, keepdims=True)
mag = net[:, 0:100] / norm_mag
norm_phase = tf.reduce_max(tf.abs(net[:, 100:200]), axis=1, keepdims=True)
phase = np.pi * (net[:, 100:200] / norm_phase - 1)
# print(net)
output = tf.concat((mag, phase), axis=1)
# loss_vec = tf.concat((mag, phase*mag))
# output = tf.cast(tf.complex(net[:, 0:100], net[:, 100:200]), dtype=tf.complex128)
############### Prediction mode.
# predicted_classes = tf.argmax(logits, 1)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'output': output,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
loss = tf.losses.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
accuracy = tf.metrics.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
######## Evaluation mode
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(mode, loss=loss, eval_metric_ops={'accuracy': accuracy})
######## Train mode
optimizer = tf.train.AdagradOptimizer(learning_rate=.01)
# optimizer = tf.train.AdamOptimizer(learning_rate=.1, epsilon=1.0)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
if mode == tf.estimator.ModeKeys.TRAIN:
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)
def FC_sym_model_ri(features, labels, mode, params):
cookie_list = []
for cookie in range(params['NUM_COOKIES']):
net = features[:, cookie, ...]
if cookie == 0:
for name, nodes in enumerate(params['COOKIE_DENSE']):
net = tf.layers.dense(inputs=net, units=nodes, activation=tf.nn.tanh, name='layer_{}'.format(name),
reuse=False)
# kernel_initializer=tf.initializers.random_uniform
cookie_list.append(net)
else:
for name, nodes in enumerate(params['COOKIE_DENSE']):
net = tf.layers.dense(inputs=net, units=nodes, activation=tf.nn.tanh, name='layer_{}'.format(name),
reuse=True)
# kernel_initializer=tf.initializers.random_uniform
cookie_list.append(net)
cookie_box = tf.concat(values=cookie_list, axis=1)
for nodes in params['DENSE']:
net = tf.layers.dense(inputs=cookie_box, units=nodes, activation=tf.nn.tanh)
net = tf.layers.dense(inputs=net, units=params['OUT'], activation=tf.nn.tanh)
net = tf.layers.flatten(net)
# norm = tf.reduce_max(tf.sqrt(net[:, 0:100] ** 2 + net[:, 100:200] ** 2), axis=1, keepdims=True)
# net = net / norm # normalize explicitly
output = net
############### Prediction mode.
# predicted_classes = tf.argmax(logits, 1)
if mode == tf.estimator.ModeKeys.PREDICT:
predictions = {
'output': output,
}
return tf.estimator.EstimatorSpec(mode, predictions=predictions)
loss = tf.losses.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
accuracy = tf.metrics.mean_squared_error(labels=tf.cast(labels, dtype=tf.float32), predictions=output)
######## Evaluation mode
if mode == tf.estimator.ModeKeys.EVAL:
return tf.estimator.EstimatorSpec(
mode, loss=loss, eval_metric_ops={'accuracy': accuracy})
######## Train mode
optimizer = tf.train.AdagradOptimizer(learning_rate=.0001)
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step())
if mode == tf.estimator.ModeKeys.TRAIN:
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op)