-
Notifications
You must be signed in to change notification settings - Fork 13
/
seq2point_train.py
282 lines (216 loc) · 12.6 KB
/
seq2point_train.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
import os
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow_model_optimization as tfmot
from tensorflow_model_optimization import sparsity
from data_feeder import TrainSlidingWindowGenerator
from model_structure import create_model, create_reduced_model, create_dropout_model, create_reduced_dropout_model, save_model
from pruning_algorithms.spp_callback import SPP
from pruning_algorithms.entropic_callback import Entropic
from pruning_algorithms.threshold_callback import Threshold
class Trainer():
""" Used to train a seq2point model with or without pruning applied Supports
various alternative architectures.
Parameters:
__appliance (string): The target appliance.
__pruning_algorithm (string): The pruning algorithm the model was trained with.
__network_type (string): The architecture of the model.
__crop (int): The maximum number of rows of data to evaluate the model with.
__batch_size (int): The number of rows per testing batch.
__window_size (int): The size of eaech sliding window
__window_offset (int): The offset of the inferred value from the sliding window.
__max_chunk_size (int): The largest possible number of row per chunk.
__validation_frequency (int): The number of epochs between model validation.
__training_directory (string): The directory of the model's training file.
__validation_directory (string): The directory of the model's validation file.
__training_chunker (TrainSlidingWindowGenerator): A sliding window provider
that returns feature / target pairs. For training use only.
__validation_chunker (TrainSlidingWindowGenerator): A sliding window provider
that returns feature / target pairs. For validation use only.
"""
def __init__(self, appliance, pruning_algorithm, batch_size, crop, network_type):
self.__appliance = appliance
self.__pruning_algorithm = pruning_algorithm
self.__network_type = network_type
self.__crop = crop
self.__batch_size = batch_size
self.__window_size = 601
self.__window_offset = int((0.5 * self.__window_size) - 1)
self.__max_chunk_size = 5 * 10 ** 2
self.__validation_frequency = 1
# Directories of the training and validation files. Always has the structure
# ./{appliance_name}/{appliance_name}_train_.csv for training or
# ./{appliance_name}/{appliance_name}_validation_.csv
self.__training_directory = "./" + self.__appliance + "/" + self.__appliance + "_training_.csv"
self.__validation_directory = "./" + self.__appliance + "/" + self.__appliance + "_validation_.csv"
self.__training_chunker = TrainSlidingWindowGenerator(file_name=self.__training_directory,
chunk_size=self.__max_chunk_size,
batch_size=self.__batch_size,
crop=self.__crop, shuffle=True,
skip_rows=10000000,
offset=self.__window_offset,
ram_threshold=5*10**5)
self.__validation_chunker = TrainSlidingWindowGenerator(file_name=self.__validation_directory,
chunk_size=self.__max_chunk_size,
batch_size=self.__batch_size,
crop=self.__crop,
shuffle=True,
skip_rows=0,
offset=self.__window_offset,
ram_threshold=5*10**5)
def train_model(self):
""" Trains an energy disaggregation model using a user-selected pruning algorithm (default is no pruning).
Plots and saves the resulting model. """
# Calculate the optimum steps per epoch.
self.__training_chunker.check_if_chunking()
steps_per_training_epoch = np.round(int(self.__training_chunker.total_size / self.__batch_size), decimals=0)
if self.__network_type == "reduced":
model = create_reduced_model()
elif self.__network_type == "dropout":
model = create_dropout_model()
elif self.__network_type == "reduced_dropout":
model = create_reduced_dropout_model()
else:
model = create_model()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999), loss="mse", metrics=["mse", "msle", "mae"])
early_stopping = tf.keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=0, patience=10, verbose=1, mode="auto")
if self.__pruning_algorithm == "tfmot":
training_history = self.tfmot_pruning(model, early_stopping, steps_per_training_epoch)
if self.__pruning_algorithm == "spp":
training_history = self.spp_pruning(model, early_stopping, steps_per_training_epoch)
if self.__pruning_algorithm == "entropic":
training_history = self.entropic_pruning(model, early_stopping, steps_per_training_epoch)
if self.__pruning_algorithm == "threshold":
training_history = self.threshold_pruning(model, early_stopping, steps_per_training_epoch)
if self.__pruning_algorithm == "default":
training_history = self.default_train(model, early_stopping, steps_per_training_epoch)
training_history.history["val_loss"] = np.repeat(training_history.history["val_loss"], self.__validation_frequency)
model.summary()
save_model(model, self.__network_type, self.__pruning_algorithm, self.__appliance)
self.plot_training_results(training_history)
def default_train(self, model, early_stopping, steps_per_training_epoch):
""" The default training method the neural network will use. No pruning occurs.
Parameters:
model (tensorflow.keras.Model): The seq2point model being trained.
early_stopping (tensorflow.keras.callbacks.EarlyStopping): An early stopping callback to
prevent overfitting.
steps_per_training_epoch (int): The number of training steps to occur per epoch.
Returns:
training_history (numpy.ndarray): The error metrics and loss values that were calculated
at the end of each training epoch.
"""
training_history = model.fit_generator(self.__training_chunker.load_dataset(),
steps_per_epoch=steps_per_training_epoch,
epochs=50,
verbose=1,
validation_data = self.__validation_chunker.load_dataset(),
validation_steps=100,
validation_freq=self.__validation_frequency,
callbacks=[early_stopping])
return training_history
def tfmot_pruning(self, model, early_stopping, steps_per_training_epoch):
""" Trains the model with TensorFlow Optimisation Tookit's prune_low_magnitude method.
Parameters:
model (tensorflow.keras.Model): The seq2point model being trained.
early_stopping (tensorflow.keras.callbacks.EarlyStopping): An early stopping callback to
prevent overfitting.
steps_per_training_epoch (int): The number of training steps to occur per epoch.
Returns:
training_history (numpy.ndarray): The error metrics and loss values that were calculated
at the end of each training epoch.
"""
pruning_params = {
'pruning_schedule': sparsity.keras.ConstantSparsity(0.70, 0),
'block_size': (1, 1),
'block_pooling_type': 'AVG'
}
model = sparsity.keras.prune_low_magnitude(model, **pruning_params)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999), loss="mse", metrics=["mse", "mae"])
training_history = model.fit_generator(self.__training_chunker.load_dataset(),
steps_per_epoch=steps_per_training_epoch,
epochs=50,
verbose=1,
validation_data = self.__validation_chunker.load_dataset(),
validation_steps=10,
validation_freq=self.__validation_frequency,
callbacks=[early_stopping, sparsity.keras.UpdatePruningStep()])
model = sparsity.keras.strip_pruning(model)
return training_history
def spp_pruning(self, model, early_stopping, steps_per_training_epoch):
""" Trains the model with Wang et al.'s structured probabilistic pruning method.
Parameters:
model (tensorflow.keras.Model): The seq2point model being trained.
early_stopping (tensorflow.keras.callbacks.EarlyStopping): An early stopping callback to
prevent overfitting.
steps_per_training_epoch (int): The number of training steps to occur per epoch.
Returns:
training_history (numpy.ndarray): The error metrics and loss values that were calculated
at the end of each training epoch.
"""
spp = SPP()
training_history = model.fit_generator(self.__training_chunker.load_dataset(),
steps_per_epoch=steps_per_training_epoch,
epochs=50,
verbose=1,
validation_data = self.__validation_chunker.load_dataset(),
validation_steps=10,
validation_freq=self.__validation_frequency,
callbacks=[early_stopping, spp])
return training_history
def entropic_pruning(self, model, early_stopping, steps_per_training_epoch):
""" Trains the model with the entropic pruning method.
Parameters:
model (tensorflow.keras.Model): The seq2point model being trained.
early_stopping (tensorflow.keras.callbacks.EarlyStopping): An early stopping callback to
prevent overfitting.
steps_per_training_epoch (int): The number of training steps to occur per epoch.
Returns:
training_history (numpy.ndarray): The error metrics and loss values that were calculated
at the end of each training epoch.
"""
entropic = Entropic()
training_history = model.fit_generator(self.__training_chunker.load_dataset(),
steps_per_epoch=steps_per_training_epoch,
epochs=50,
verbose=1,
validation_data = self.__validation_chunker.load_dataset(),
validation_steps=10,
validation_freq=self.__validation_frequency,
callbacks=[early_stopping, entropic])
return training_history
def threshold_pruning(self, model, early_stopping, steps_per_training_epoch):
""" Trains the model with Ashouri et al.'s threshold-based pruning method.
Parameters:
model (tensorflow.keras.Model): The seq2point model being trained.
early_stopping (tensorflow.keras.callbacks.EarlyStopping): An early stopping callback to
prevent overfitting.
steps_per_training_epoch (int): The number of training steps to occur per epoch.
Returns:
training_history (numpy.ndarray): The error metrics and loss values that were calculated
at the end of each training epoch.
"""
threshold = Threshold()
training_history = model.fit_generator(self.__training_chunker.load_dataset(),
steps_per_epoch=steps_per_training_epoch,
epochs=50,
verbose=1,
validation_data = self.__validation_chunker.load_dataset(),
validation_steps=10,
validation_freq=self.__validation_frequency,
callbacks=[early_stopping, threshold])
return training_history
def plot_training_results(self, training_history):
""" Plots and saves a graph of training loss against epoch.
Parameters:
training_history (numpy.ndarray): A timeseries of loss against epoch count.
"""
plt.plot(training_history.history["loss"], label="MSE (Training Loss)")
plt.plot(training_history.history["val_loss"], label="MSE (Validation Loss)")
plt.title('Training History')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend()
file_name = "./" + self.__appliance + "/saved_models/" + self.__appliance + "_" + self.__pruning_algorithm + "_" + self.__network_type + "_training_results.png"
plt.savefig(fname=file_name)