-
Notifications
You must be signed in to change notification settings - Fork 36
/
seq2point_train.py
176 lines (144 loc) · 8.73 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
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from data_feeder import TrainSlidingWindowGenerator
from model_structure import create_model, save_model
#tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
class Trainer():
""" Used to train a seq2point model with or without pruning applied Supports
various alternative architectures.
Parameters:
__appliance (string): The target appliance.
__network_type (string): The architecture of the model.
__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, batch_size, crop, network_type,
training_directory, validation_directory, save_model_dir,
epochs=10, input_window_length=599, validation_frequency = 1,
patience=3, min_delta=1e-6, verbose=1):
self.__appliance = appliance
self.__algorithm = network_type
self.__network_type = network_type
self.__crop = crop
self.__batch_size = batch_size
self.__epochs = epochs
self.__patience = patience
self.__min_delta = min_delta
self.__verbose = verbose
self.__loss = "mse"
self.__metrics = ["mse", "msle", "mae"]
self.__learning_rate = 0.001
self.__beta_1=0.9
self.__beta_2=0.999
self.__save_model_dir = save_model_dir
self.__input_window_length = input_window_length
self.__window_size = 2+self.__input_window_length
self.__window_offset = int((0.5 * self.__window_size) - 1)
self.__max_chunk_size = 5 * 10 ** 2
self.__validation_frequency = validation_frequency
self.__ram_threshold=5*10**5
self.__skip_rows_train=10000000
self.__validation_steps=100
self.__skip_rows_val = 0
# Directories of the training and validation files. Always has the structure
# ./dataset_management/refit/{appliance_name}/{appliance_name}_training_.csv for training or
# ./dataset_management/refit/{appliance_name}/{appliance_name}_validation_.csv
self.__training_directory = training_directory
self.__validation_directory = validation_directory
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=self.__skip_rows_train,
offset=self.__window_offset,
ram_threshold=self.__ram_threshold)
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=self.__skip_rows_val,
offset=self.__window_offset,
ram_threshold=self.__ram_threshold)
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)
steps_per_training_epoch = np.round(int(self.__training_chunker.total_num_samples / self.__batch_size), decimals=0)
model = create_model(self.__input_window_length)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=self.__learning_rate, beta_1=self.__beta_1, beta_2=self.__beta_2), loss=self.__loss, metrics=self.__metrics)
early_stopping = tf.keras.callbacks.EarlyStopping(monitor="val_loss", min_delta=self.__min_delta, patience=self.__patience, verbose=self.__verbose, mode="auto")
## can use checkpoint ###############################################
# checkpoint_filepath = "checkpoint/housedata/refit/"+ self.__appliance + "/"
# model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
# filepath = checkpoint_filepath,
# monitor='val_loss',
# verbose=0,
# save_best_only=True,
# save_weights_only=False,
# mode='auto',
# save_freq='epoch')
#callbacks=[early_stopping, model_checkpoint_callback]
###################################################################
callbacks=[early_stopping]
training_history = self.default_train(model, callbacks, 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.__algorithm,
self.__appliance, self.__save_model_dir)
self.plot_training_results(training_history)
def default_train(self, model, callbacks, 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.
"""
# ########### this is retired ##############################
# training_history = model.fit_generator(self.__training_chunker.load_dataset(),
# steps_per_epoch=steps_per_training_epoch,
# epochs=1,
# verbose=1,
# validation_data = self.__validation_chunker.load_dataset(),
# validation_steps=100,
# validation_freq=self.__validation_frequency,
# callbacks=[early_stopping])
############################################################
training_history = model.fit(self.__training_chunker.load_dataset(),
steps_per_epoch=steps_per_training_epoch,
epochs = self.__epochs,
verbose = self.__verbose,
callbacks=callbacks,
validation_data = self.__validation_chunker.load_dataset(),
validation_freq=self.__validation_frequency,
validation_steps=self.__validation_steps)
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)