-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.py
293 lines (239 loc) · 11 KB
/
Model.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
# Standard packages
import time
import importlib as il
import sys
import os
import pickle
import random
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# External packages
import tensorflow as tf
import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats
#import tensorflow_probability as tfp
# Self made modules
import config
from WindowGenerator import *
#from WindowClassificationGenerator import *
from ReportGenerator import ReportGenerator
tf.get_logger().setLevel('ERROR')
class Model(): # Methods and features shared across all predictive models
def __init__(self,settings):
self.settings = settings
#self.name = settings.name
self.report_generator = ReportGenerator(settings)
def detect_outliers(self): # Calculates p-value for residual
statistic , pvalue = stats.normaltest(self.residual)
np.set_printoptions(precision=4)
def plot_outliers(self): # Plots histogram of predictions an a scatterplot
fig, (ax1,ax2) = plt.subplots(nrows=1,ncols=2,figsize=config.figsize)
ax1.hist(
self.residual.numpy()[:,0,0],
bins = 100)
ax1.set_title('Prediction residuals')
ax1.set_ylabel(f'Prediction error for {self.settings_model.plot_targets}')
ax2.hist(
self.prediction.flatten(),
bins = 100)
ax2.set_title('Predictions')
plt.show()
def remember_dates(self,data):
return {
'train_start' : data.train_df['ts'].iloc[0],
'train_end' : data.train_df['ts'].iloc[-1],
'test_start' : data.test_df['ts'].iloc[0],
'test_end' : data.test_df['ts'].iloc[-1],
'eval_start' : data.val_df['ts'].iloc[0],
'eval_end' : data.val_df['ts'].iloc[-1],
}
def print_shape(self):
rand = random.randint(0,1000000)
try:
for example_inputs, example_labels in self.time_seriess[rand%len(self.time_seriess)].train.take(1):
print(f'Inputs shape (batch, time, features): {example_inputs.shape}')
print(f'Labels shape (batch, time, features): {example_labels.shape}')
except ValueError:
for example_inputs, example_labels in self.time_seriess[rand%len(self.time_seriess)].train.test(1):
print(f'Inputs shape (batch, time, features): {example_inputs.shape}')
print(f'Labels shape (batch, time, features): {example_labels.shape}')
def save_dataset(self):
with open(f"{config.dataset_path}{self.settings.dataset_name}.json",'wb') as f:
pickle.dump(self.time_series, f)
def load_dataset(self):
self.time_series = pickle.load(open(f"{config.dataset_path}{self.settings.dataset_name}.json",'rb'))
def inspect_dataset(self):
print(len(list(self.time_series.train.as_numpy_iterator())))
class StatModel(Model): # Purely statistical model to be used as baseline
def __init__(self,settings,data):
super().__init__(settings)
self.data = data
def setup(self):
sys.path.append(config.preset_path)
module = il.import_module(self.settings.preset) # Import the specified model from file with same name
self.model = module.Model(self.data)
class NeuralNet(Model): # Methods and features shared among all Keras Neural Nets
def __init__(self,settings):
super().__init__(settings)
def make_timeseries_dataset(self, data):
time_seriess = []
cols = list(set(self.settings_model.features+self.settings_model.targets))
for i in range(len(data.dfs)):
time_seriess.append(WindowGenerator(
input_width = self.settings_model.input_time_steps,
label_width = self.settings_model.target_time_steps,
shift = self.settings_model.shift,
train_df = data.train_dfs[i][cols],
val_df = data.val_dfs[i][cols],
test_df = data.test_dfs[i][cols],
feature_columns = self.settings_model.features,
label_columns = self.settings_model.targets,
train_batch_size = self.settings_train.batch_size,
eval_batch_size = self.settings_eval.batch_size,
test_batch_size = self.settings_test.batch_size)
)
self.time_seriess = time_seriess
try:
self.dates = data.dates
except AttributeError:
self.dates = 'missing'
def setup(self):
sys.path.append(config.preset_path)
module = il.import_module(self.settings.preset) # Import the specified model from file with same name
# Record settings from the neural net module
self.settings_model = module.Settings_nn()
self.settings_train = module.Settings_train()
self.settings_eval = module.Settings_eval()
self.settings_test = module.Settings_test()
self.nn = module.set_up_model(self.settings_model)
# learning rate decay
# learning algortihm
# other stuff
def compile_model(self):
self.nn.compile( # compile the net
loss = self.settings_train.loss,
optimizer = self.settings_train.optimizer,
metrics = self.settings_train.metrics)
self.loaded = False
def plot_model(self):
tf.keras.utils.plot_model(
self.nn,
config.saved_path+self.settings.name+'/model_plot.jpeg',
show_shapes = True,
show_layer_names = True,
dpi = 150)
def set_up_classifier(self):
sys.path.append(config.classifier_path)
module = il.import_module(self.settings.classifier)
self.classifier = module.Classifier(self.classifier_parameters)
def train(self): # Train the neural net
train = self.time_seriess[0].train
val = self.time_seriess[0].val
for time_series in self.time_seriess:
train = train.concatenate(time_series.train)
val = val.concatenate(time_series.val)
for example_inputs, example_labels in time_series.train.take(1):
print(f'Inputs shape (batch, time, features): {example_inputs.shape}')
print(f'Labels shape (batch, time, features): {example_labels.shape}')
tic = time.time()
callbacks = []
if self.settings_train.early_stopping:
callbacks.append(tf.keras.callbacks.EarlyStopping(
monitor = self.settings_train.early_stopping_monitor,
min_delta = self.settings_train.early_stopping_min_delta,
patience = self.settings_train.early_stopping_patience,
verbose = self.settings_train.early_stopping_verbose,
mode = self.settings_train.early_stopping_mode
))
self.history = self.nn.fit(
train,
epochs = self.settings_train.epochs,
batch_size = self.settings_train.batch_size,
validation_data = val,
callbacks = callbacks,
shuffle = self.settings_train.shuffle,
verbose = self.settings_model.verbose)
self.toc = time.time() - tic
if self.loaded:
for key in self.history.history.keys():
self.history.history[key].extend(self.earlier_history[key])
self.nn.summary()
self.training_report = self.report_generator.generate_training_report(self)
def evaluate(self): # Evaluate the neural net
test = self.time_seriess[0].test
for time_series in self.time_seriess:
test = test.concatenate(time_series.test)
self.loss = self.nn.evaluate(
test,
batch_size = self.settings_eval.batch_size,
verbose = self.settings_model.verbose
)
self.eval_report = self.report_generator.generate_eval_report(self)
def test(self): # Test the neural net
test = self.time_seriess[0].test
for time_series in self.time_seriess:
test = test.concatenate(time_series.test)
prediction = self.nn.predict(
test,
batch_size = self.settings_test.batch_size,
verbose = self.settings_model.verbose
)
#print(repr(time_series))
shape = tf.shape(prediction)
ground_truth = tf.concat([y for x, y in test], axis=0)
self.residual = tf.math.subtract(prediction,ground_truth,name='residual')
self.prediction = prediction
def train_classifier_parameters(self):
prediction = self.nn.predict(
self.time_series.test,
batch_size = self.settings_test.batch_size,
verbose = self.settings_model.verbose
)
shape = tf.shape(prediction)
ground_truth = tf.concat([y for x, y in self.time_series.test], axis=0)
self.residual = tf.math.subtract(prediction,ground_truth,name='residual')
tensorized_residual = tf.reshape(self.residual,shape)
self.classifier_parameters = tf.nn.moments(
x = tensorized_residual,
axes = [0]
)
def classify(self): # Feed newly fetched data
prediction = self.nn.predict(
self.time_series.test
)
statistic, pvalue = self.classifier.classify(prediction)
print(f"t-statistic: {statistic}\npvalue: {pvalue}\n")
# Write a report
def save_nn(self,overwrite=False):
name = self.get_name()
path = config.saved_path+name
self.nn.save(
filepath = path,
overwrite = overwrite,
include_optimizer = True,
save_format = 'tf')
with open(path+'/history.json','wb') as f:
pickle.dump(self.history.history, f)
f = open(config.saved_path+name+'/train_report.txt','a')
f.write(self.training_report)
f.close()
f = open(config.saved_path+name+'/eval_report.txt','a')
f.write(self.eval_report)
f.close()
def load_nn(self):
name = self.get_name()
if name not in os.listdir(config.saved_path):
print(f'No saved model named {name}')
#raise Exception('No module to load')
else:
print(config.saved_path+name,os.listdir(config.saved_path))
loaded_nn = tf.keras.models.load_model(
filepath = config.saved_path+name,
compile = True)
self.nn = loaded_nn
print(f"Loaded {name}")
self.earlier_history = pickle.load(open(config.saved_path+name+'/history.json','rb'))
self.loaded = True
def get_name(self):
s = self.settings_model
return f"{s.kind}_{s.input_time_steps}_{s.target_time_steps}_{s.shift}_nodes_{'_'.join([str(x) for x in s.layer_widths])}_in_{'-'.join(s.features)}_out_{'-'.join(s.targets)}"