-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathModels.py
More file actions
433 lines (330 loc) · 16.1 KB
/
Copy pathModels.py
File metadata and controls
433 lines (330 loc) · 16.1 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import keras
import itertools
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout
def build_LSTM(input_size, hidden_units, dropout, learning_rate):
'''
Builds the Network with LSTM hidden layers
Args:
input_size: int that defines the length
of the input sequence to be
fed to the network
hidden_units: int/list specifying the number
of hidden units in the hidden
layer/layers
dropout: boolean specifing whether to add dropout
with 0.5 rate per layer
learning_rate: learning rate of the Adam
optimization algorithm
Returns:
model: keras sequential model
'''
h = hidden_units
model = Sequential()
if isinstance(h,list):
model.add(LSTM(h[0],
batch_input_shape=(1,input_size, 1),
return_sequences=True,
stateful=True))
if dropout:
model.add(Dropout(rate=0.5))
if len(h) > 2:
#removing 1st and last units
for index, units in enumerate(h[1:-1]):
model.add(LSTM(units,
batch_input_shape=(1,h[index], 1),
return_sequences=True,
stateful=True))
if dropout:
model.add(Dropout(rate=0.5))
model.add(LSTM(h[-1],
batch_input_shape=(1,h[-2], 1),
return_sequences=False,
stateful=True))
if dropout:
model.add(Dropout(rate=0.5))
else:
model.add(LSTM(h,
batch_input_shape=(1,input_size, 1),
return_sequences=False,
stateful=True))
if dropout:
model.add(Dropout(rate=0.5))
model.add(Dense(1))
adam = keras.optimizers.Adam(lr=learning_rate)
model.compile(loss='mse', optimizer=adam)
return model
def predict_ahead(model,X_test,n_ahead):
'''
Makes predictions based on the last available sequence
Args:
model: keras sequential model
X_test: the last available sequence
n_ahead: number of predictions to make
Returns:
predictions: numpy array containing the predicted values
'''
predictions = np.zeros(n_ahead)
predictions[0] = model.predict(X_test,batch_size = 1)
if n_ahead > 1:
for i in range(1,n_ahead):
x_new = np.append(X_test[0][1:],predictions[i-1])
X_test = x_new.reshape(1,x_new.shape[0],1)
predictions[i] = model.predict(X_test,batch_size = 1)
return predictions
def FitForecast(X_train, y_train, X_test, n_ahead, input_size,
hidden_units, dropout, val_split, learning_rate,
epochs, trained_model):
'''
Fits a model and returns the predicted values.
Optionally weights from another network can be passed
Args:
X_train: input dataset for training
y_train: output dataset for training
X_test: the last available sequence
n_ahead: number of predictions to make
time_series: the time series of interest
input_size: int that defines the length
of the input sequence to be
fed to the network
hidden_units: int/list specifying the number
of hidden units in the hidden
layer/layers
dropout: boolean specifing whether to add dropout
with 0.5 rate per layer
learning_rate: learning rate of the Adam
optimization algorithm
epochs: int that defines the number of
training phases through the
training dataset
trained_model: already trained keras sequential
model
Returns:
model: keras sequential model
predictions: numpy array containing the predicted values
history: training and validation loss history
'''
model = build_LSTM(input_size,hidden_units,dropout, learning_rate)
if trained_model is not None:
model.set_weights(weights = trained_model.get_weights())
history = model.fit(x=X_train, y=y_train,
batch_size=1, epochs=epochs,
verbose=1, validation_split=val_split,
shuffle=False)
predictions = predict_ahead(model,X_test,n_ahead)
return model, predictions, history
def FitEvaluate(time_series,params):
'''
Calls the pipeline to fit an LSTM model to the
given time series
Args:
time_series: the time series of interest
params: a dictionary specifying parameters
{input_size, hidden_units, dropout,
learning_rate, n_ahead, val_split,
epochs, verbose, plot}
Returns:
model: keras sequential model
mse: mean squared error of the prediction
history: training and validation loss history
'''
for k in params.keys():
globals()[k] = params[k]
scaled_series, scaler = preprocessing(time_series)
series, y_test, n_test = getSeries(scaled_series,0.8)
X_train,y_train,X_test = getInputOutput(series,input_size)
# show only n_ahead number of actual values
y_test = y_test[np.arange(n_ahead)]
new_model, predictions, history = FitForecast(X_train,y_train,X_test,n_ahead,
input_size,hidden_units,dropout, val_split,
learning_rate,epochs,trained_model=None)
# rescaling
series = inverse_transform(series, scaler)
y_test = inverse_transform(y_test, scaler)
predictions = inverse_transform(predictions, scaler)
mse = mean_squared_error(y_true=y_test,y_pred=predictions)
if verbose:
print('\n')
print('======== Prediction Evaluation =========')
print('MSE is {}'.format(round(mse,4)))
if plot:
ViewLoss(history)
view_predictions(series,predictions,y_test,'Actual vs Forecast')
return new_model, mse, history
def TransferLearning(time_series,params,model):
'''
Calls the pipeline to fit an LSTM model to the
given time series with and without knowledge
transfer
Args:
time_series: the time series of interest
params: a dictionary specifying parameters
{input_size, hidden_units, dropout,
learning_rate, n_ahead, val_split,
epochs, verbose, plot}
model: already trained keras sequential
model
Returns:
mean squared errors of the predictions
and 2 plots
'''
for k in params.keys():
globals()[k] = params[k]
val_split = 0
scaled_series, scaler = preprocessing(time_series)
series, y_test, n_test = getSeries(scaled_series,0.8)
X_train,y_train,X_test = getInputOutput(series,input_size)
# show only n_ahead number of actual values
y_test = y_test[np.arange(n_ahead)]
print('*** Fitting a model without knowledge transfer ***')
model_noTransfer, predictions_noTransfer, _ = FitForecast(X_train,y_train,
X_test,n_ahead,
input_size,hidden_units,
dropout,val_split,
learning_rate,
epochs,
trained_model=None)
print('\n')
print('*** Fitting a model with knowledge transfer ***')
model_withTransfer, predictions_withTransfer, _ = FitForecast(X_train,y_train,
X_test,n_ahead,
input_size,hidden_units,
dropout,val_split,
learning_rate,
epochs,
trained_model=model)
# rescaling
series = inverse_transform(series, scaler)
y_test = inverse_transform(y_test, scaler)
predictions_noTransfer = inverse_transform(predictions_noTransfer, scaler)
predictions_withTransfer = inverse_transform(predictions_withTransfer, scaler)
mse_noTransfer = mean_squared_error(y_true=y_test,y_pred=predictions_noTransfer)
mse_withTransfer = mean_squared_error(y_true=y_test,y_pred=predictions_withTransfer)
print('\n')
print('======== Results for no knowledge transfer =========')
print('The RMSE is {}'.format(round(np.sqrt(mse_noTransfer),4)))
print('\n')
print('======== Results for knowledge transfer =========')
print('The RMSE is {}'.format(round(np.sqrt(mse_withTransfer),4)))
view_predictions(series,predictions_noTransfer,y_test,title='Without Transfer')
view_predictions(series,predictions_withTransfer,y_test,title='With Transfer')
def GridSearch(series,params_grid):
'''
Runs a grid search over specified parameter ranges
Args:
series: the time series of interest
params_grid: a dictionary specifying parameters
{input_size, hidden_units, dropout,
learning_rate, n_ahead, val_split,
epochs, verbose, plot} and their
possible value ranges
Returns:
model: the model with the lowest MSE
logs: logs of all combinations
'''
param_names = list(params_grid.keys())
param_values = list(params_grid.values())
combinations = list(itertools.product(*param_values))
logs = pd.DataFrame(combinations,columns=param_names)
mse_prev = 1
for index, comb in enumerate(combinations):
print('Fitting {}/{} model'.format(index+1,len(combinations)))
params = dict(zip(param_names,comb))
model, mse, history = FitEvaluate(series,params)
train_loss = history.history['loss']
val_loss = history.history['val_loss']
if mse < mse_prev:
mse_prev = mse
best_model = model
logs.at[index,'mse'] = mse
logs.at[index,'mean_training_loss'] = np.mean(train_loss)
logs.at[index,'std_training_loss'] = np.std(train_loss)
logs.at[index,'mean_val_loss'] = np.mean(val_loss)
logs.at[index,'std_val_loss'] = np.std(val_loss)
logs.to_csv('results.csv',index=False)
view_predictions(series,best_predictions,y_test,'Actual vs Forecast')
return best_model, logs
def generalTuning(series1, series2, params):
'''
Fitting 3 models trained on
{general domain,general+in-domain,in-domain only}
and comparing them
Args:
series1: target related time series
series2: target time series
params: a dictionary specifying parameters
{input_size, hidden_units, dropout,
learning_rate, n_ahead, val_split,
epochs, verbose, plot}
Returns:
mean squared errors of the predictions
and 3 plots
'''
time_series = np.concatenate([series1,series2])
for k in params.keys():
globals()[k] = params[k]
val_split = 0
# preprocessing general series
scaled_general, scaler_general = preprocessing(time_series)
series_general, _, __ = getSeries(scaled_general,0.9)
X_train_general,y_train_general,___ = getInputOutput(series_general,input_size)
# preprocessing the target series
scaled_target, scaler_target = preprocessing(series2)
series_target, y_test_target, n_test = getSeries(scaled_target,0.8)
X_train_target,y_train_target,X_test_target = getInputOutput(series_target,input_size)
# comparing predictions with only n_ahead number of actual values
y_test_target = y_test_target[np.arange(n_ahead)]
# build and train a model on the general domain (time_series)
print('*** Fitting a model on general domain ***')
model_general, predictions_pre_tuned, hist = FitForecast(X_train_general,y_train_general,
X_test_target,n_ahead,
input_size, hidden_units,
dropout, val_split,
learning_rate,
epochs,
trained_model=None)
# initiallize a model for target
model_tuned = build_LSTM(input_size, hidden_units, dropout, learning_rate)
# transfer the knowledge from the pre-trained model
# and tune it only on the target domain (series2)
model_tuned.set_weights(weights=model_general.get_weights())
print('\n *** Tuning a model on target domain ***')
model_tuned.fit(x=X_train_target, y=y_train_target,
batch_size=1, epochs=epochs,
verbose=1, validation_data=None,
shuffle=False)
predictions_tuned = predict_ahead(model_tuned,X_test_target,n_ahead)
print('\n *** Fitting a model on target domain only ***')
model_target, predictions_target,hist2 = FitForecast(X_train_target,y_train_target,
X_test_target,n_ahead,
input_size, hidden_units,
dropout, val_split,
learning_rate,
2 * epochs,
trained_model=None)
series_target = inverse_transform(series_target,scaler_target)
y_test_target = inverse_transform(y_test_target,scaler_target)
series_general = inverse_transform(series_general,scaler_general)
predictions_pre_tuned = inverse_transform(predictions_pre_tuned,scaler_target)
mse_pre_tuned = mean_squared_error(y_true=y_test_target,y_pred=predictions_pre_tuned)
predictions_tuned = inverse_transform(predictions_tuned,scaler_target)
mse_tuned = mean_squared_error(y_true=y_test_target,y_pred=predictions_tuned)
predictions_target = inverse_transform(predictions_target,scaler_target)
mse_target = mean_squared_error(y_true=y_test_target,y_pred=predictions_target)
print('\n')
print('======== Results for pre_tuned model =========')
print('The RMSE is {}'.format(round(np.sqrt(mse_pre_tuned),4)))
print('\n')
print('======== Results for tuned model =========')
print('The RMSE is {}'.format(round(np.sqrt(mse_tuned),4)))
print('\n')
print('======== Results for target model only =========')
print('The RMSE is {}'.format(round(np.sqrt(mse_target),4)))
view_predictions([series_general,series_target],predictions_pre_tuned,y_test_target,title='Pre-tuned')
view_predictions([series_general,series_target],predictions_tuned,y_test_target,title='Tuned')
view_predictions(series_target,predictions_target,y_test_target,title='Target only')