-
Notifications
You must be signed in to change notification settings - Fork 69
/
build.py
392 lines (326 loc) · 15.3 KB
/
build.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
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
# Python Build for training, testing and exporting model
# Importing Libraries
import numpy as np
import pandas as pd
import tensorflow as tf
import pickle
from enum import Enum
from datetime import datetime
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM, CuDNNLSTM, GRU, CuDNNGRU, Bidirectional
from keras.optimizers import SGD, RMSprop, Adam, Adagrad
from keras.losses import mean_squared_error
from keras.models import load_model
from keras import backend as K
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import os
# Defining Enums
class Architecture(Enum):
LSTM = 0
GRU = 1
BidirectionalLSTM = 2
BidirectionalGRU = 3
class Optimizer(Enum):
RMSProp = 0
SGD = 1
Adam = 2
Adagrad = 3
class Loss(Enum):
MSE = 0
R2 = 1
# Just disables the warning, doesn't enable AVX/FMA
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Suppressing deprecated warnings
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# Allowing Cudnn for LSTM
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config)
window_size = 60
def r2_score(y_true, y_pred):
SS_res = K.sum(K.square(y_true - y_pred))
SS_tot = K.sum(K.square(y_true - K.mean(y_true)))
return ( 1 - SS_res/(SS_tot + K.epsilon()) )
def getOptimizer(optimizer, lr, momentum):
if optimizer == Optimizer.RMSProp.value:
return RMSprop(lr=lr)
elif optimizer == Optimizer.SGD.value:
return SGD(lr=lr, momentum=momentum)
elif optimizer == Optimizer.Adam.value:
return Adam(lr=lr)
elif optimizer == Optimizer.Adagrad.value:
return Adagrad(lr=lr)
def getLoss(loss):
if loss == Loss.MSE.value:
return 'mean_squared_error'
elif loss == Loss.R2.value:
return r2_score
def getModel(X_train, architecture, isCuda):
if architecture == Architecture.LSTM.value:
if isCuda:
# The LSTM architecture
regressor = Sequential()
# First LSTM layer with Dropout regularisation
regressor.add(CuDNNLSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressor.add(Dropout(0.2))
# Second LSTM layer
regressor.add(CuDNNLSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
# Third LSTM layer
regressor.add(CuDNNLSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
# Fourth LSTM layer
regressor.add(CuDNNLSTM(units=50))
regressor.add(Dropout(0.2))
# The output layer
regressor.add(Dense(units=1))
return regressor
else:
# The LSTM architecture
regressor = Sequential()
# First LSTM layer with Dropout regularisation
regressor.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressor.add(Dropout(0.2))
# Second LSTM layer
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
# Third LSTM layer
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
# Fourth LSTM layer
regressor.add(LSTM(units=50))
regressor.add(Dropout(0.2))
# The output layer
regressor.add(Dense(units=1))
return regressor
elif architecture == Architecture.GRU.value:
if isCuda:
# The GRU architecture
regressorGRU = Sequential()
# First GRU layer with Dropout regularisation
regressorGRU.add(CuDNNGRU(units=50, return_sequences=True, input_shape=(X_train.shape[1])))
regressorGRU.add(Dropout(0.2))
# Second GRU layer
regressorGRU.add(CuDNNGRU(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressorGRU.add(Dropout(0.2))
# Third GRU layer
regressorGRU.add(CuDNNGRU(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressorGRU.add(Dropout(0.2))
# Fourth GRU layer
regressorGRU.add(CuDNNGRU(units=50))
regressorGRU.add(Dropout(0.2))
# The output layer
regressorGRU.add(Dense(units=1))
return regressorGRU
else:
# The GRU architecture
regressorGRU = Sequential()
# First GRU layer with Dropout regularisation
regressorGRU.add(GRU(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressorGRU.add(Dropout(0.2))
# Second GRU layer
regressorGRU.add(GRU(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressorGRU.add(Dropout(0.2))
# Third GRU layer
regressorGRU.add(GRU(units=50, return_sequences=True, input_shape=(X_train.shape[1],1)))
regressorGRU.add(Dropout(0.2))
# Fourth GRU layer
regressorGRU.add(GRU(units=50))
regressorGRU.add(Dropout(0.2))
# The output layer
regressorGRU.add(Dense(units=1))
return regressorGRU
elif architecture == Architecture.BidirectionalLSTM.value:
if isCuda:
# Bidirectional Model
regressorBidirection = Sequential()
# First Bidirectional LSTM Layer
regressorBidirection.add(Bidirectional(CuDNNLSTM(units=50, return_sequences=True), input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Second Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(CuDNNLSTM(units=50, return_sequences=True)))
regressorBidirection.add(Dropout(0.2))
# Third Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(CuDNNLSTM(units=50, return_sequences=True)))
regressorBidirection.add(Dropout(0.2))
# Fourth Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(CuDNNLSTM(units=50)))
regressorBidirection.add(Dropout(0.2))
# The output layer
regressorBidirection.add(Dense(units=1))
return regressorBidirection
else:
# Bidirectional Model
regressorBidirection = Sequential()
# First Bidirectional LSTM Layer
regressorBidirection.add(Bidirectional(LSTM(units=50, return_sequences=True), input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Second Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(LSTM(units=50, return_sequences=True)))
regressorBidirection.add(Dropout(0.2))
# Third Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(LSTM(units=50, return_sequences=True)))
regressorBidirection.add(Dropout(0.2))
# Fourth Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(LSTM(units=50)))
regressorBidirection.add(Dropout(0.2))
# The output layer
regressorBidirection.add(Dense(units=1))
return regressorBidirection
elif architecture == Architecture.BidirectionalGRU.value:
if isCuda:
# Bidirectional Model
regressorBidirection = Sequential()
# First Bidirectional LSTM Layer
regressorBidirection.add(Bidirectional(CuDNNGRU(units=50, return_sequences=True),input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Second Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(CuDNNGRU(units=50, return_sequences=True),input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Third Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(CuDNNGRU(units=50, return_sequences=True),input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Fourth Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(CuDNNGRU(units=50)))
regressorBidirection.add(Dropout(0.2))
# The output layer
regressorBidirection.add(Dense(units=1))
return regressorBidirection
else:
# Bidirectional Model
regressorBidirection = Sequential()
# First Bidirectional LSTM Layer
regressorBidirection.add(Bidirectional(GRU(units=50, return_sequences=True),input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Second Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(GRU(units=50, return_sequences=True),input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Third Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(GRU(units=50, return_sequences=True),input_shape=(X_train.shape[1],1)))
regressorBidirection.add(Dropout(0.2))
# Fourth Bidirectional LSTM layer
regressorBidirection.add(Bidirectional(GRU(units=50)))
regressorBidirection.add(Dropout(0.2))
# The output layer
regressorBidirection.add(Dense(units=1))
return regressorBidirection
def getScaledData(training_set, scale, file_name):
sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(training_set)
pickle_out = open(file_name + '_scaler.pickle', 'wb')
pickle.dump(sc, pickle_out)
pickle_out.close()
# creating a data structure with window_size timesteps and 1 output
# for each element of training set, we have window_size previous training set elements
X_train = []
Y_train = []
for i in range(window_size,training_set_scaled.shape[0]):
X_train.append(training_set_scaled[i-window_size:i,0])
Y_train.append(training_set_scaled[i,0])
X_train, Y_train = np.array(X_train), np.array(Y_train)
# Reshaping X_train for efficient modelling
X_train = np.reshape(X_train, (X_train.shape[0],X_train.shape[1],1))
return X_train, Y_train
def save_plot(test,predicted, file_name):
plt.plot(test, color='red',label='Real Stock Price')
plt.plot(predicted, color='blue',label='Predicted Stock Price')
plt.title('Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Stock Price')
plt.legend()
plt.savefig(file_name + '.jpg')
def train(training_set, date, lr, scale, epochs, momentum, optimizer, loss, file_name, architecture, cuda):
if(type(training_set) == list and type(date) == list):
# Constructing a pandas dataframe for reusability and reference
df = pd.DataFrame(data = training_set, columns = ['Feature'], index = pd.to_datetime(date))
df.index.names = ['Date']
df.index = pd.to_datetime(df.index)
df.to_csv(file_name + '.csv')
training_set = df.values
# Scaling and preprocessing the training set
X_train, Y_train = getScaledData(training_set, scale, file_name)
# Constructing a stacked LSTM Sequential Model
regressor = getModel(X_train, architecture, tf.test.is_gpu_available() if cuda else False)
# Compiling the RNN
regressor.compile(optimizer=getOptimizer(optimizer, lr, momentum), loss=getLoss(loss), metrics=['mse',r2_score])
# Fitting to the training set
hist = regressor.fit(X_train, Y_train,epochs = epochs, batch_size=32)
#Saving trained model
regressor.save(file_name + '.h5')
pickle_out = open(file_name + '_trainhist.pickle', 'wb')
pickle.dump(hist.history, pickle_out)
pickle_out.close()
#Deleting model instance
del regressor
return 100
else:
return 110
def test(testing_set, date, file_name):
if(type(testing_set) == list and type(date) == list):
# Constructing a pandas dataframe for reusability and reference
df = pd.DataFrame(data = testing_set, columns = ['Feature'], index = date)
df.index.names = ['Date']
df.index = pd.to_datetime(df.index)
test_set = df['Feature'].values
prev_dataset = pd.read_csv(file_name + '.csv', index_col = 'Date', parse_dates=['Date'])
regressor = load_model(file_name + '.h5', custom_objects={'r2_score':r2_score})
file = open(file_name + '_scaler.pickle', 'rb')
scaler = pickle.load(file)
file.close()
# Now to get the test set ready in a similar way as the training set.
dataset_total = pd.concat((prev_dataset, df),axis=0, sort=False)
dataset_total.to_csv(file_name + '.csv')
inputs = dataset_total[len(dataset_total)-len(testing_set) - window_size:]['Feature'].values
inputs = inputs.reshape(-1,1)
inputs = scaler.transform(inputs)
# Preparing X_test and predicting the prices
X_test = []
for i in range(window_size, inputs.shape[0]):
X_test.append(inputs[i - window_size:i,0])
X_test = np.array(X_test)
X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1))
predicted_stock_price = regressor.predict(X_test)
predicted_stock_price = scaler.inverse_transform(predicted_stock_price)
#save_plot(test_set, predicted_stock_price, file_name)
eval = regressor.evaluate(X_test, scaler.transform(test_set.reshape(-1,1)))
pickle_out = open(file_name + '_testhist.pickle', 'wb')
pickle.dump(eval, pickle_out)
pickle_out.close()
# Deleting model instance
del regressor
return 100
else:
return 110
def evaluate(file_name, testing_weight):
file = open(file_name + '_trainhist.pickle', 'rb')
trainHistory = pickle.load(file)
file.close()
file = open(file_name + '_testhist.pickle', 'rb')
testScores = pickle.load(file)
file.close()
trainScores = [trainHistory[key][-1] for key in trainHistory.keys()]
scoreList = [(trainScores[i] * (1 - testing_weight/100) + testScores[i] * (testing_weight/100))/2 for i in range(len(trainScores))]
return scoreList
def predict(file_name, bars):
if(bars < window_size):
prev_dataset = pd.read_csv(file_name + '.csv', index_col = 'Date', parse_dates=['Date'])
regressor = load_model(file_name + '.h5', custom_objects={'r2_score':r2_score})
file = open(file_name + '_scaler.pickle', 'rb')
scaler = pickle.load(file)
file.close()
inputs = prev_dataset[len(prev_dataset) - bars - window_size:]['Feature'].values
inputs = inputs.reshape(-1,1)
inputs = scaler.transform(inputs)
# Preparing X_pred and predicting the prices
X_pred = []
for i in range(window_size, inputs.shape[0]):
X_pred.append(inputs[i - window_size:i,0])
X_pred = np.array(X_pred)
X_pred = np.reshape(X_pred, (X_pred.shape[0],X_pred.shape[1],1))
predicted_stock_price = regressor.predict(X_pred)
predicted_stock_price = scaler.inverse_transform(predicted_stock_price)
return predicted_stock_price.reshape(predicted_stock_price.shape[0]).tolist()
else:
return -1