-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmixins.py
372 lines (310 loc) · 12 KB
/
mixins.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
# coding: utf-8
"""
Mixin classes to build ML models
"""
from __future__ import annotations
import law
# import order as od
from columnflow.types import Union
from columnflow.util import maybe_import, DotDict
from hbw.util import log_memory, call_func_safe
np = maybe_import("numpy")
ak = maybe_import("awkward")
logger = law.logger.get_logger(__name__)
def loop_dataset(data, max_count=10000):
for i, x in enumerate(data):
if i % int(max_count / 100) == 0:
print(i)
if i == max_count:
break
class DenseModelMixin(object):
"""
Mixin that provides an implementation for `prepare_ml_model`
"""
activation: str = "relu"
layers: tuple[int] = (64, 64, 64)
dropout: float = 0.50
learningrate: float = 0.00050
def cast_ml_param_values(self):
"""
Cast the values of the parameters to the correct types
"""
super().cast_ml_param_values()
self.activation = str(self.activation)
self.layers = tuple(int(n_nodes) for n_nodes in self.layers)
self.dropout = float(self.dropout)
self.learningrate = float(self.learningrate)
def prepare_ml_model(
self,
task: law.Task,
):
import tensorflow.keras as keras
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization
from hbw.ml.tf_util import cumulated_crossentropy
n_inputs = len(set(self.input_features))
n_outputs = len(self.processes)
# define the DNN model
model = Sequential()
# BatchNormalization layer with input shape
model.add(BatchNormalization(input_shape=(n_inputs,)))
activation_settings = DotDict({
"elu": ("ELU", "he_uniform", "Dropout"),
"relu": ("ReLU", "he_uniform", "Dropout"),
"prelu": ("PReLU", "he_normal", "Dropout"),
"selu": ("selu", "lecun_normal", "AlphaDropout"),
"tanh": ("tanh", "glorot_normal", "Dropout"),
"softmax": ("softmax", "glorot_normal", "Dropout"),
})
keras_act_name, init_name, dropout_layer = activation_settings[self.activation]
# hidden layers
for n_nodes in self.layers:
model.add(Dense(
units=n_nodes,
activation=keras_act_name,
))
# Potentially add dropout layer after each hidden layer
if self.dropout:
Dropout = getattr(keras.layers, dropout_layer)
model.add(Dropout(self.dropout))
# output layer
model.add(Dense(n_outputs, activation="softmax"))
# compile the network
optimizer = keras.optimizers.Adam(
learning_rate=self.learningrate, beta_1=0.9, beta_2=0.999,
epsilon=1e-6, amsgrad=False,
)
model.compile(
loss=cumulated_crossentropy,
# NOTE: we'd preferrably use the Keras CCE, but it does not work when assigning one event
# to multiple classes (target with multiple entries != 0)
# loss ="categorical_crossentropy",
# loss=tf.keras.losses.CategoricalCrossentropy(reduction=tf.keras.losses.Reduction.NONE),
optimizer=optimizer,
metrics=["categorical_accuracy"],
weighted_metrics=["categorical_accuracy"],
)
return model
class CallbacksBase(object):
""" Base class that handles parametrization of callbacks """
callbacks: set[str] = {
"backup", "checkpoint", "reduce_lr",
# "early_stopping",
}
remove_backup: bool = True
# NOTE: we could remove these parameters since they can be implemented via reduce_lr_kwargs
reduce_lr_factor: float = 0.8
reduce_lr_patience: int = 3
# custom callback kwargs
checkpoint_kwargs: dict = {}
backup_kwargs: dict = {}
early_stopping_kwargs: dict = {}
reduce_lr_kwargs: dict = {}
def cast_ml_param_values(self):
"""
Cast the values of the parameters to the correct types
"""
super().cast_ml_param_values()
self.callbacks = set(self.callbacks)
self.remove_backup = bool(self.remove_backup)
self.reduce_lr_factor = float(self.reduce_lr_factor)
self.reduce_lr_patience = int(self.reduce_lr_patience)
def get_callbacks(self, output):
import tensorflow.keras as keras
# check that only valid options have been requested
callback_options = {"backup", "checkpoint", "reduce_lr", "early_stopping"}
if diff := self.callbacks.difference(callback_options):
logger.warning(f"Callbacks '{diff}' have been requested but are not properly implemented")
# list of callbacks to be returned at the end
callbacks = []
# output used for BackupAndRestore callback (not deleted by --remove-output)
# NOTE: does that work when running remote?
# TODO: we should also save the parameters + input_features in the backup to ensure that they
# are equivalent (delete backup if not)
backup_output = output["mlmodel"].sibling(f"backup_{output['mlmodel'].basename}", type="d")
if self.remove_backup:
backup_output.remove()
#
# for each requested callback, merge default kwargs with custom callback kwargs
#
if "backup" in self.callbacks:
backup_kwargs = dict(
backup_dir=backup_output.path,
)
backup_kwargs.update(self.backup_kwargs)
callbacks.append(keras.callbacks.BackupAndRestore(**backup_kwargs))
if "checkpoint" in self.callbacks:
checkpoint_kwargs = dict(
filepath=output["checkpoint"].path,
save_weights_only=False,
monitor="val_loss",
mode="auto",
save_best_only=True,
)
checkpoint_kwargs.update(self.checkpoint_kwargs)
callbacks.append(keras.callbacks.ModelCheckpoint(**checkpoint_kwargs))
if "early_stopping" in self.callbacks:
early_stopping_kwargs = dict(
monitor="val_loss",
min_delta=0,
patience=max(min(50, int(self.epochs / 5)), 10),
verbose=1,
restore_best_weights=True,
start_from_epoch=max(min(50, int(self.epochs / 5)), 10),
)
early_stopping_kwargs.update(self.early_stopping_kwargs)
callbacks.append(keras.callbacks.EarlyStopping(**early_stopping_kwargs))
if "reduce_lr" in self.callbacks:
reduce_lr_kwargs = dict(
monitor="val_loss",
factor=self.reduce_lr_factor,
patience=self.reduce_lr_patience,
verbose=1,
mode="auto",
min_delta=0,
min_lr=0,
)
reduce_lr_kwargs.update(self.reduce_lr_kwargs)
callbacks.append(keras.callbacks.ReduceLROnPlateau(**reduce_lr_kwargs))
if len(callbacks) != len(self.callbacks):
logger.warning(
f"{len(self.callbacks)} callbacks have been requested but only {len(callbacks)} are returned",
)
return callbacks
class ClassicModelFitMixin(CallbacksBase):
"""
Mixin to run ML Training with "classic" training loop.
TODO: this will require a different reweighting
"""
callbacks: set = {
"backup", "checkpoint", "reduce_lr",
# "early_stopping",
}
remove_backup: bool = True
reduce_lr_factor: float = 0.8
reduce_lr_patience: int = 3
epochs: int = 200
batchsize: int = 2 ** 12
def cast_ml_param_values(self):
"""
Cast the values of the parameters to the correct types
"""
super().cast_ml_param_values()
self.epochs = int(self.epochs)
self.batchsize = int(self.batchsize)
def fit_ml_model(
self,
task: law.Task,
model,
train: DotDict[np.array],
validation: DotDict[np.array],
output,
) -> None:
"""
Training loop with normal tf dataset
"""
import tensorflow as tf
log_memory("start")
with tf.device("CPU"):
tf_train = tf.data.Dataset.from_tensor_slices(
(train["inputs"], train["target"], train["weights"]),
).batch(self.batchsize)
tf_validation = tf.data.Dataset.from_tensor_slices(
(validation["inputs"], validation["target"], validation["weights"]),
).batch(self.batchsize)
log_memory("init")
# set the kwargs used for training
model_fit_kwargs = {
"validation_data": tf_validation,
"epochs": self.epochs,
"verbose": 2,
"callbacks": self.get_callbacks(output),
}
logger.info("Starting training...")
model.fit(
tf_train,
**model_fit_kwargs,
)
log_memory("loop")
# delete tf datasets to clear memory
del tf_train
del tf_validation
log_memory("del")
class ModelFitMixin(CallbacksBase):
# parameters related to callbacks
callbacks: set = {
"backup", "checkpoint", "reduce_lr",
# "early_stopping",
}
remove_backup: bool = True
reduce_lr_factor: float = 0.8
reduce_lr_patience: int = 3
epochs: int = 200
batchsize: int = 2 ** 12
# either set steps directly or use attribute from the MultiDataset
steps_per_epoch: Union[int, str] = "iter_smallest_process"
def cast_ml_param_values(self):
"""
Cast the values of the parameters to the correct types
"""
super().cast_ml_param_values()
self.epochs = int(self.epochs)
self.batchsize = int(self.batchsize)
if isinstance(self.steps_per_epoch, float):
self.steps_per_epoch = int(self.steps_per_epoch)
else:
self.steps_per_epoch = str(self.steps_per_epoch)
def fit_ml_model(
self,
task: law.Task,
model,
train: DotDict[np.array],
validation: DotDict[np.array],
output,
) -> None:
"""
Training loop but with custom dataset
"""
import tensorflow as tf
from hbw.ml.tf_util import MultiDataset
from hbw.ml.plotting import plot_history
log_memory("start")
with tf.device("CPU"):
tf_train = MultiDataset(data=train, batch_size=self.batchsize, kind="train", buffersize=0)
tf_validation = tf.data.Dataset.from_tensor_slices(
(validation.features, validation.target, validation.train_weights),
).batch(self.batchsize)
log_memory("init")
# determine the requested steps_per_epoch
if isinstance(self.steps_per_epoch, str):
steps_per_epoch = getattr(tf_train, self.steps_per_epoch)
else:
steps_per_epoch = int(self.steps_per_epoch)
if not isinstance(steps_per_epoch, int):
raise Exception(
f"steps_per_epoch is {self.steps_per_epoch} but has to be either an integer or"
"a string corresponding to an integer attribute of the MultiDataset",
)
logger.info(f"Training will be done with {steps_per_epoch} steps per epoch")
# set the kwargs used for training
model_fit_kwargs = {
"validation_data": tf_validation,
"epochs": self.epochs,
"verbose": 2,
"steps_per_epoch": steps_per_epoch,
"callbacks": self.get_callbacks(output),
}
# start training by iterating over the MultiDataset
iterator = (x for x in tf_train)
logger.info("Starting training...")
model.fit(
iterator,
**model_fit_kwargs,
)
# create history plots
for metric, ylabel in (
("loss", "Loss"),
("categorical_accuracy", "Accuracy"),
("weighted_categorical_accuracy", "Weighted Accuracy"),
):
call_func_safe(plot_history, model.history.history, output["plots"], metric, ylabel)