-
Notifications
You must be signed in to change notification settings - Fork 20
/
Transformer.py
176 lines (135 loc) · 5.7 KB
/
Transformer.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
from datetime import datetime
from time import time
import json
import logging
import tensorflow as tf
import keras
from keras import layers
from keras.models import Model
from keras.models import load_model
from keras.models import Sequential
from keras.callbacks import EarlyStopping, TensorBoard, ModelCheckpoint
from keras.callbacks import Callback
from kerastuner.tuners import RandomSearch
from sklearn.metrics import r2_score
from utils import rmse, coeff_determination, smape
class Transformer(object):
""" Building the Recurrent Neural Network for Multivariate time series forecasting
"""
def __init__(self):
""" Initialization of the object
"""
with open("parameters.json") as f:
parameters = json.load(f)
# Get model hyperparameters
self.look_back = parameters["look_back"]
self.n_features = parameters["n_features"]
self.horizon = parameters["horizon"]
# Get directories name
self.log_dir = parameters["log_dir"]
self.checkpoint_dir = parameters["checkpoint_dir"]
self.head_size=256
self.num_heads=4
self.ff_dim=4
self.num_transformer_blocks=4
self.mlp_units=[128]
self.mlp_dropout=0.4
self.dropout=0.25
def transformer_encoder(self,
inputs):
# Normalization and Attention
x = layers.LayerNormalization(epsilon=1e-6)(inputs)
x = layers.MultiHeadAttention(
key_dim=self.head_size, num_heads=self.num_heads, dropout=self.dropout)(x, x)
x = layers.Dropout(self.dropout)(x)
res = x + inputs
# Feed Forward Part
x = layers.LayerNormalization(epsilon=1e-6)(res)
x = layers.Conv1D(filters=self.ff_dim, kernel_size=1, activation="relu")(x)
x = layers.Dropout(self.dropout)(x)
x = layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
return x + res
def build(self):
""" Build the model architecture
"""
inputs = keras.Input(shape=(self.look_back, self.n_features))
x = inputs
for _ in range(self.num_transformer_blocks):
x = self.transformer_encoder(x)
x = layers.GlobalAveragePooling1D(data_format="channels_first")(x)
for dim in self.mlp_units:
x = layers.Dense(dim, activation="relu")(x)
x = layers.Dropout(self.mlp_dropout)(x)
# output layer
outputs = layers.Dense(self.horizon)(x)
return keras.Model(inputs, outputs)
def restore(self,
filepath):
""" Restore a previously trained model
"""
# Load the architecture
self.best_model = load_model(filepath, custom_objects={'smape': smape,
#'mape': mape,
'rmse' : rmse,
'coeff_determination' : coeff_determination})
## added cause with TF 2.4, custom metrics are not recognize custom metrics with only load-model
self.best_model.compile(
optimizer='adam',
loss = ['mse'],
metrics=[rmse, 'mae', smape, coeff_determination])
def train(self,
X_train,
y_train,
epochs=200,
batch_size=64):
""" Training the network
:param X_train: training feature vectors [#batch,#number_of_timesteps,#number_of_features]
:type 3-D Numpy array of float values
:param Y_train: training target vectors
:type 2-D Numpy array of float values
:param epochs: number of training epochs
:type int
:param batch_size: size of batches used at each forward/backward propagation
:type int
:return -
:raises: -
"""
self.model = self.build()
self.model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),
loss = ['mse'],
metrics=[rmse, 'mae', smape, coeff_determination],
)
print(self.model.summary())
# Stop training if error does not improve within 50 iterations
early_stopping_monitor = EarlyStopping(patience=50, restore_best_weights=True)
# Save the best model ... with minimal error
filepath = self.checkpoint_dir+"/Transformer.best"+datetime.now().strftime('%d%m%Y_%H:%M:%S')+".hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callback_history = self.model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,
#validation_split=0.2,
verbose=1,
callbacks=[early_stopping_monitor, checkpoint])
#callbacks=[PlotLossesKeras(), early_stopping_monitor, checkpoint])
def evaluate(self,
X_test,
y_test):
""" Evaluating the network
:param X_test: test feature vectors [#batch,#number_of_timesteps,#number_of_features]
:type 3-D Numpy array of float values
:param Y_test: test target vectors
:type 2-D Numpy array of int values
:return Evaluation losses
:rtype 5 Float tuple
:raise -
"""
y_pred = self.model.predict(X_test)
# Print accuracy if ground truth is provided
"""
if y_test is not None:
loss_ = session.run(
self.loss,
feed_dict=feed_dict)
"""
_, rmse_result, mae_result, smape_result, _ = self.model.evaluate(X_test, y_test)
r2_result = r2_score(y_test.flatten(),y_pred.flatten())
return _, rmse_result, mae_result, smape_result, r2_result