-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning.py
More file actions
81 lines (65 loc) · 2.78 KB
/
Copy pathlearning.py
File metadata and controls
81 lines (65 loc) · 2.78 KB
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
import keras_tuner
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
class FluxMaskLayer(layers.Layer):
"""
A layer that incorporates the information on flux signal (its use is optional)
"""
def __init__(self, flux_signals, **kwargs):
super(FluxMaskLayer, self).__init__(**kwargs)
self.flux_mask = tf.constant(flux_signals, dtype=tf.float32)
def get_config(self):
config = super().get_config()
config.update({"flux_signals": self.flux_mask.numpy().tolist()})
return config
def call(self, inputs):
return tf.abs(inputs) * self.flux_mask
class NE_Loss(tf.keras.losses.Loss):
"""
Normalized Error Loss
"""
def __init__(self,reduction='auto'):
super().__init__()
self.reduction = reduction
def call(self, y_true, y_pred):
loss = tf.norm(y_true - y_pred, axis=1) / tf.norm(y_true, axis=1)
return tf.where(tf.math.is_nan(loss), tf.zeros_like(loss), loss)
class HyperModel(keras_tuner.HyperModel):
"""
HyperModel class that defines the search space for our deep learning architectures and respective hyperoptimization
"""
def __init__(self, output_shape, flux_mask):
super(HyperModel, self).__init__()
self.output_shape = output_shape
self.flux_mask = flux_mask
def build(self, hp):
model = keras.Sequential()
activation = hp.Choice("activation", ["relu",
"tanh",
"elu",
"linear",
"selu",
"sigmoid",
"softmax",
"swish"])
regularizer = hp.Choice("regularizer", ["l1", "l2"])
for i in range(hp.Int('num_layers', 1, 5)):
model.add(layers.Dense(units=hp.Int('units_' + str(i),
min_value=5,
max_value=50,
step=5),
activation=activation,
kernel_regularizer=regularizer))
if hp.Boolean("dropout"):
model.add(layers.Dropout(rate=hp.Float('dropout_rate',
min_value=0.1,
max_value=0.5,
sampling="linear")))
model.add(layers.Dense(self.output_shape, kernel_regularizer=regularizer, activation="relu"))
if self.flux_mask:
model.add(FluxMaskLayer(self.flux_mask))
learning_rate = hp.Float("lr", min_value=1e-3, max_value=1e-2, sampling="log")
model.compile(optimizer=keras.optimizers.Adam(learning_rate=learning_rate),
loss = NE_Loss())
return model