-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCN_run.py
207 lines (163 loc) · 7.06 KB
/
TCN_run.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
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from tensorflow.keras import Input, Model
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Dense
from keras.utils import np_utils
from kerastuner import Hyperband, HyperModel
from tcn import TCN, tcn_full_summary
from LSTM_run import prepare_data
class MyHyperModel(HyperModel):
""" MYHYPERMODEL Custom Hypermodel with vector shapes
A custom hypermodel that includes the shapes of the vectors in its
definition to create the layers with the right sizes.
"""
def __init__(self, X_timesteps, X_features, Y_shape):
""" __INIT__ Creates the hypermodel
Parameters
----------
X_timesteps : int
Number of timesteps in the X dataset given.
X_features : int
Number of features in the X dataset given.
Y_shape : int
dimensions of the Y vector. Corresponds to number of classes
Returns
-------
None.
"""
self.X_timesteps = X_timesteps
self.X_features = X_features
self.Y_shape = Y_shape
def build(self, hp):
""" Build the custom HyperModel
Define a TCN model-building function.
It takes an hp argument from which you can sample hyperparameters
Parameters
----------
hp : Hyperparameter list
A list containing the hyperparameters to try.
Returns
-------
model : Hypermodel
The custom hypermodel.
"""
# select the parameters to tune
# common values
dropout = hp.Float('dropout_rate', default=0.0,
min_value=0.0, max_value=0.7, step=0.1)
if self.X_timesteps < 50:
# choice for models for 2 or 10 days in memory.
filters = hp.Int('nb_filters', default=64,
min_value=8, max_value=64, step=8)
kern_sz = hp.Int('kernel_size', default=2,
min_value=2, max_value=7, step=1)
else:
# choice for models with 50, 100 or 150 days in memory.
filters = hp.Int('nb_filters', default=64,
min_value=64, max_value=64)
kern_sz = hp.Int('kernel_size', min_value=2, max_value=3, step=1)
# setup the input layer
i = Input(shape=(self.X_timesteps, self.X_features))
# build the model with different hyperparameters choice
tcn_layer = TCN(nb_filters=filters,
kernel_size=kern_sz,
nb_stacks=1,
dilations=(1, 2, 4, 8, 16,32,64),
padding='causal',
dropout_rate=dropout)
#the tcn layers are here
o = tcn_layer(i)
#build the dense layer at the end
o = Dense(self.Y_shape, activation='softmax')(o)
model = Model(inputs=[i], outputs=[o])
#compile the model
model.compile('adam','categorical_crossentropy', metrics=['acc'])
#show receptive field and ckeck if you have full history coverage
print(tcn_layer.receptive_field)
if tcn_layer.receptive_field >= self.X_timesteps:
print('full history coverage assured')
#detailed summary with the 5 residual blocks
tcn_full_summary(model, expand_residual_blocks=False)
return model
def tcn_predict(hyperparam_opt, history_window):
""" TCN_PREDICT Performs an TCN prediction on the given data
Performs an TCN prediction with or without hyperparameter optimization
Returns the predictions and the test data that was used
Parameters
----------
hyperparam_opt : boolean
Whether or not to perform hyperparameter optimization.
history_window : int
The number of days to take in memory for the prediction.
Returns
-------
y_pred : numpy array
The predicted labels of the test data.
Y_test_val : numpy array
The true labels of the test data..
"""
# define input data path
in_file_path = './Data/preprocessed.csv'
# define the model path
filepath="./Models/TCN/model-tcn-{}mem.hdf5".format(history_window)
# load the data
df = pd.read_csv(in_file_path,index_col=0)
# shape the data properly
X_train, Y_train, X_test, Y_test = prepare_data(df, history_window)
if hyperparam_opt:
#perform hyperparam optimization
epoch_num = 25
#define the tcn model
hypermodel = MyHyperModel(X_train.shape[1],
X_train.shape[2],
Y_train.shape[1])
#define the optimizer
tuner = Hyperband(hypermodel,
objective='val_acc',
max_epochs=epoch_num,
factor=3, directory='./Models/TCN/',
project_name='tuning_{}mem'.format(history_window),
overwrite=True)
# perform the hyperparameter optimization
tuner.search_space_summary()
# perform the tuning of the parameters on the model
# and then return the output of the tuned model
tuner.search(X_train,
Y_train,
epochs=epoch_num,
validation_data=(X_test, Y_test))
# tuner.search(X_train,
# Y_train,
# epochs=30,
# validation_data=(X_test, Y_test),
# callbacks=[tf.keras.callbacks.EarlyStopping(patience=1)])
tuner.results_summary()
#get best hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials = 1)[0]
# build the best model
best_model = hypermodel.build(best_hps)
#fit data to model
best_model.fit(X_train, Y_train, epochs=epoch_num)
# print the best hyperparameters. done here after the fitting
# so it remains on the console and doesn't get lost
print(f"""
The hyperparameter search is complete.\n
Number of filters: {best_hps.get('nb_filters')}\n
Kernel size: {best_hps.get('kernel_size')}\n
Dropout rate: {best_hps.get('dropout_rate')}\n
""")
# save the model
best_model.save(filepath)
else:
#don't perform hyperparameter tuning, just directly load best model
best_model = load_model(filepath, custom_objects={'TCN': TCN})
# predict
prediction = best_model.predict(X_test, verbose=0)
# un-encode the y data
y_pred = np.argmax(prediction,axis=1)
Y_test_val = np.argmax(Y_test,axis=1)
return y_pred, Y_test_val
if __name__ == "__main__":
y_pred, Y_test_val = tcn_predict(True, 150)