kryox64 commited on
Commit
0356a8c
1 Parent(s): 6201f7a

Change app.py

Browse files

Signed-off-by: Aadhitya A <[email protected]>

Files changed (1) hide show
  1. app.py +1 -343
app.py CHANGED
@@ -74,340 +74,6 @@ ACCELERATOR = "cpu"
74
  w = 7
75
  prax = [0 for x in range(w)]
76
 
77
- # %%
78
- # Objective function for Optuna (CNN-LSTM)
79
- def objective(trial, X_train, y_train, X_test, y_test):
80
- model = tf.keras.Sequential()
81
-
82
- # Creating the Neural Network model here...
83
- # CNN layers
84
- model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(X_train.shape[1], 1)))
85
- # model.add(Dense(5, kernel_regularizer=L2(0.01)))
86
-
87
- # LSTM layers
88
- model.add(Bidirectional(LSTM(trial.suggest_int("lstm_units_1", 32, 256), return_sequences=True)))
89
- model.add(Dropout(trial.suggest_float("dropout_1", 0.1, 0.5)))
90
- model.add(Bidirectional(LSTM(trial.suggest_int("lstm_units_2", 32, 256), return_sequences=False)))
91
- model.add(Dropout(trial.suggest_float("dropout_2", 0.1, 0.5)))
92
-
93
- #Final layers
94
- model.add(Dense(1, activation='relu'))
95
- model.compile(optimizer='adam', loss='mse', metrics=['mse'])
96
-
97
- # Train the model
98
- pruning_callback = TFKerasPruningCallback(trial, "val_loss")
99
- history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=15, batch_size=32, verbose=0, callbacks=[pruning_callback])
100
-
101
- # Evaluate the model
102
- loss = model.evaluate(X_test, y_test, verbose=0)[0]
103
-
104
- return loss
105
-
106
- # %%
107
- # Function to train the model (CNN-LSTM)
108
- def modelCNNLSTM(csv_file, prax):
109
- # Read the data
110
- df = csv_file
111
- #df = df['Date/Time'].values.astype("float64")
112
- temp_data = df.iloc[0:len(df)-100, 1:21]
113
- trek = df.iloc[len(df)-100:,1:21]
114
- #print(temp_data)
115
- data = temp_data
116
- sc = MinMaxScaler()
117
- # Split the data into training and testing sets
118
- train_size = int(len(data) * 0.8)
119
- train_data, test_data = data[:train_size], data[train_size:]
120
- # Separate the input features and target variable
121
- X_train, y_train = train_data, train_data['Close']
122
- X_test, y_test = test_data, test_data['Close']
123
-
124
- X_train = X_train[0:len(X_train)-1]
125
- y_train = y_train[1:len(y_train)]
126
- X_test = X_test[0:len(X_test)-1]
127
- y_test = y_test[1:len(y_test)]
128
-
129
- Xt = X_train
130
- Xts = X_test
131
- Yt = y_train
132
- Yts = y_test
133
-
134
- y_train = y_train.values.reshape(-1,1)
135
- y_test = y_test.values.reshape(-1,1)
136
-
137
- X_train = sc.fit_transform(X_train)
138
- y_train = sc.fit_transform(y_train)
139
- X_test = sc.fit_transform(X_test)
140
- y_test = sc.fit_transform(y_test)
141
-
142
- x_tr=pd.DataFrame(X_train, index = Xt.index, columns = Xt.columns)
143
- y_tr=pd.DataFrame(y_train, index = Yt.index)
144
- x_te=pd.DataFrame(X_test, index = Xts.index, columns = Xts.columns)
145
- y_te=pd.DataFrame(y_test, index = Yts.index)
146
-
147
- # Reshape the data for the CNN-LSTM model
148
- X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
149
- X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))
150
-
151
- study = optuna.create_study(direction="minimize", pruner=optuna.pruners.MedianPruner(n_min_trials=4, n_startup_trials=4))
152
- fn = lambda trial: objective(trial, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test)
153
- study.optimize(fn, n_trials=5)
154
-
155
- best_params = study.best_params
156
- #print(f"Best params: {best_params}")
157
-
158
- model = tf.keras.Sequential()
159
-
160
- # Creating the Neural Network model here...
161
- # CNN layers
162
- model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(X_train.shape[1], 1)))
163
- # model.add(Dense(5, kernel_regularizer=L2(0.01)))
164
-
165
- # LSTM layers
166
- model.add(Bidirectional(LSTM(best_params["lstm_units_1"], return_sequences=True)))
167
- model.add(Dropout(best_params["dropout_1"]))
168
- model.add(Bidirectional(LSTM(best_params["lstm_units_2"], return_sequences=False)))
169
- model.add(Dropout(best_params["dropout_2"]))
170
-
171
- #Final layers
172
- model.add(Dense(1, activation='relu'))
173
- model.compile(optimizer='adam', loss='mse', metrics=['mse'])
174
-
175
- # Train the model
176
- history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=32, verbose=0)
177
-
178
- # Evaluate the model
179
- loss = model.evaluate(X_test, y_test, verbose=0)[0]
180
-
181
- print(f"Final loss (without KFold): {loss}")
182
-
183
- kfold = KFold(n_splits=10, shuffle=True)
184
-
185
- inputs = np.concatenate((X_train, X_test), axis=0)
186
- targets = np.concatenate((y_train, y_test), axis=0)
187
- acc_per_fold = []
188
- loss_per_fold = []
189
- xgb_res = []
190
- num_epochs = 10
191
- batch_size = 32
192
-
193
- fold_no = 1
194
- print('------------------------------------------------------------------------')
195
- print("Training for 10 folds... Standby")
196
- for train, test in kfold.split(inputs, targets):
197
- #print('------------------------------------------------------------------------')
198
- #print(f'Training for fold {fold_no} ...')
199
- history = model.fit(inputs[train], targets[train],
200
- batch_size=32,
201
- epochs=15,
202
- verbose=0)
203
-
204
- scores = model.evaluate(inputs[test], targets[test], verbose=0)
205
- #print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')
206
- acc_per_fold.append(scores[1] * 100)
207
- loss_per_fold.append(scores[0])
208
- fold_no = fold_no + 1
209
-
210
-
211
- print('------------------------------------------------------------------------')
212
- #print('Score per fold')
213
- #for i in range(0, len(acc_per_fold)):
214
- # print('------------------------------------------------------------------------')
215
- # print(f'> Fold {i+1} - Loss: {loss_per_fold[i]} - Loss%: {acc_per_fold[i]}%')
216
- #print('------------------------------------------------------------------------')
217
- #print('Average scores for all folds:')
218
- #print(f'> Possible Loss %: {np.mean(acc_per_fold)} (+- {np.std(acc_per_fold)})')
219
- #print(f'> Loss: {np.mean(loss_per_fold)}')
220
- #print('------------------------------------------------------------------------')
221
-
222
- trek = df.iloc[0:len(df), 1:21]
223
- Y = trek[0:len(trek)]
224
- YP = trek[1:len(trek)]
225
- Y1 = Y['Close']
226
- Y2 = YP['Close']
227
- Yx = pd.DataFrame(YP, index=YP.index, columns=YP.columns)
228
- #X = sc.fit_transform(X.reshape(-1,22))
229
- Y = np.array(Y)
230
- Y1 = np.array(Y1)
231
- Y = sc.fit_transform(Y)
232
- Y1 = Y1.reshape(-1,1)
233
- Y1 = sc.fit_transform(Y1)
234
-
235
- train_X = Y.reshape(Y.shape[0],Y.shape[1],1)
236
- #Y = Y.reshape(-1,1)
237
- pred = model.predict(train_X, verbose=0)
238
- pred = np.array(pred).reshape(-1,1)
239
- var2 = max_error(pred.reshape(-1,1), Y1)
240
- print('Max Error: %f' % var2)
241
- prax[5] = float(var2)
242
- pred = sc.inverse_transform(pred)
243
-
244
- print(pred[-2], pred[-1])
245
- prax[3] = pred[-2]
246
- prax[4] = pred[-1]
247
- if(pred[-1]-pred[-2]>0):
248
- prax[6] = 1
249
- elif(pred[-1]-pred[-2]==0):
250
- prax[6] = 0
251
- else:
252
- prax[6] = -1
253
-
254
- # %%
255
- # Function to train the model (CNN-LSTM)
256
- def modelCNNLSTM_OpenGap(csv_file, prax):
257
- # Read the data
258
- df = csv_file
259
- datLength = len(df)
260
- df['O-C'] = 0
261
- for i in range(datLength):
262
- if i == 0:
263
- df['O-C'][i] = 0
264
- continue
265
- else:
266
- df['O-C'][i] = df['Open'][i] - df['Close'][i-1]
267
- temp_data = df.iloc[0:datLength-100, 1:22]
268
- trek = df.iloc[datLength-100:,1:22]
269
- #print(temp_data)
270
- data = temp_data
271
- #data = data.values.astype("float64")
272
- sc = MinMaxScaler()
273
- # Split the data into training and testing sets
274
- train_size = int(len(data) * 0.8)
275
- train_data, test_data = data[:train_size], data[train_size:]
276
-
277
- # Separate the input features and target variable
278
- X_train, y_train = train_data, train_data['Close']
279
- X_test, y_test = test_data, test_data['Close']
280
-
281
- X_train = X_train[0:len(X_train)-1]
282
- y_train = y_train[1:len(y_train)]
283
- X_test = X_test[0:len(X_test)-1]
284
- y_test = y_test[1:len(y_test)]
285
-
286
- Xt = X_train
287
- Xts = X_test
288
- Yt = y_train
289
- Yts = y_test
290
-
291
- y_train = y_train.values.reshape(-1,1)
292
- y_test = y_test.values.reshape(-1,1)
293
-
294
- X_train = sc.fit_transform(X_train)
295
- y_train = sc.fit_transform(y_train)
296
- X_test = sc.fit_transform(X_test)
297
- y_test = sc.fit_transform(y_test)
298
-
299
- x_tr=pd.DataFrame(X_train, index = Xt.index, columns = Xt.columns)
300
- y_tr=pd.DataFrame(y_train, index = Yt.index)
301
- x_te=pd.DataFrame(X_test, index = Xts.index, columns = Xts.columns)
302
- y_te=pd.DataFrame(y_test, index = Yts.index)
303
-
304
- # Reshape the data for the CNN-LSTM model
305
- X_train = X_train.reshape((X_train.shape[0], X_train.shape[1], 1))
306
- X_test = X_test.reshape((X_test.shape[0], X_test.shape[1], 1))
307
-
308
- study = optuna.create_study(direction="minimize", pruner=optuna.pruners.MedianPruner(n_min_trials=2, n_startup_trials=2))
309
- fn = lambda trial: objective(trial, X_train=X_train, X_test=X_test, y_train=y_train, y_test=y_test)
310
- study.optimize(fn, n_trials=5)
311
-
312
- best_params = study.best_params
313
- #print(f"Best params: {best_params}")
314
-
315
- model = tf.keras.Sequential()
316
-
317
- # Creating the Neural Network model here...
318
- # CNN layers
319
- model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(X_train.shape[1], 1)))
320
- # model.add(Dense(5, kernel_regularizer=L2(0.01)))
321
-
322
- # LSTM layers
323
- model.add(Bidirectional(LSTM(best_params["lstm_units_1"], return_sequences=True)))
324
- model.add(Dropout(best_params["dropout_1"]))
325
- model.add(Bidirectional(LSTM(best_params["lstm_units_2"], return_sequences=False)))
326
- model.add(Dropout(best_params["dropout_2"]))
327
-
328
- #Final layers
329
- model.add(Dense(1, activation='relu'))
330
- model.compile(optimizer='adam', loss='mse', metrics=['mse'])
331
-
332
- # Train the model
333
- history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=32, verbose=0)
334
-
335
- # Evaluate the model
336
- loss = model.evaluate(X_test, y_test, verbose=0)[0]
337
-
338
- print(f"Final loss (without KFold): {loss}")
339
-
340
- kfold = KFold(n_splits=10, shuffle=True)
341
-
342
- inputs = np.concatenate((X_train, X_test), axis=0)
343
- targets = np.concatenate((y_train, y_test), axis=0)
344
- acc_per_fold = []
345
- loss_per_fold = []
346
- xgb_res = []
347
- num_epochs = 10
348
- batch_size = 32
349
-
350
- fold_no = 1
351
- print('------------------------------------------------------------------------')
352
- print("Training for 10 folds... Standby")
353
- for train, test in kfold.split(inputs, targets):
354
- #print('------------------------------------------------------------------------')
355
- #print(f'Training for fold {fold_no} ...')
356
- history = model.fit(inputs[train], targets[train],
357
- batch_size=32,
358
- epochs=15,
359
- verbose=0)
360
-
361
- scores = model.evaluate(inputs[test], targets[test], verbose=0)
362
- #print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')
363
- acc_per_fold.append(scores[1] * 100)
364
- loss_per_fold.append(scores[0])
365
- fold_no = fold_no + 1
366
-
367
-
368
- print('------------------------------------------------------------------------')
369
- #print('Score per fold')
370
- #for i in range(0, len(acc_per_fold)):
371
- # print('------------------------------------------------------------------------')
372
- # print(f'> Fold {i+1} - Loss: {loss_per_fold[i]} - Loss%: {acc_per_fold[i]}%')
373
- #print('------------------------------------------------------------------------')
374
- #print('Average scores for all folds:')
375
- #print(f'> Possible Loss %: {np.mean(acc_per_fold)} (+- {np.std(acc_per_fold)})')
376
- #print(f'> Loss: {np.mean(loss_per_fold)}')
377
- #print('------------------------------------------------------------------------')
378
-
379
- trek = df.iloc[0:len(df), 1:22]
380
- Y = trek[0:len(trek)]
381
- YP = trek[1:len(trek)]
382
- Y1 = Y['Close']
383
- Y2 = YP['Close']
384
- Yx = pd.DataFrame(YP, index=YP.index, columns=YP.columns)
385
- #X = sc.fit_transform(X.reshape(-1,22))
386
- Y = np.array(Y)
387
- Y1 = np.array(Y1)
388
- Y = sc.fit_transform(Y)
389
- Y1 = Y1.reshape(-1,1)
390
- Y1 = sc.fit_transform(Y1)
391
-
392
- train_X = Y.reshape(Y.shape[0],Y.shape[1],1)
393
- #Y = Y.reshape(-1,1)
394
- pred = model.predict(train_X, verbose=0)
395
- pred = np.array(pred).reshape(-1,1)
396
- var2 = max_error(pred.reshape(-1,1), Y1)
397
- print('Max Error: %f' % var2)
398
- prax[5] = float(var2)
399
- pred = sc.inverse_transform(pred)
400
-
401
- print(pred[-2], pred[-1])
402
- prax[3] = pred[-2]
403
- prax[4] = pred[-1]
404
- if(pred[-1]-pred[-2]>0):
405
- prax[6] = 1
406
- elif(pred[-1]-pred[-2]==0):
407
- prax[6] = 0
408
- else:
409
- prax[6] = -1
410
-
411
  # %%
412
  # Function to train the model (TFT)
413
  def modelTFT(csv_file, prax):
@@ -909,14 +575,6 @@ def main(files):
909
  modelTFT_OpenGap(df, prax)
910
  prax[2] = "TFT_OpenGap"
911
  generate_csv(prax)
912
- #df.set_index('Date/Time', inplace=True)
913
- #df = df.drop(['Date/Time'], axis=1)
914
- #modelCNNLSTM(df, prax)
915
- #prax[2] = "CNNLSTM"
916
- #generate_csv(prax)
917
- #modelCNNLSTM_OpenGap(df, prax)
918
- #prax[2] = "CNNLSTM_OpenGap"
919
- #generate_csv(prax)
920
  # Generate blank line
921
  prax=["","","","","","",""]
922
  generate_csv(prax)
@@ -925,7 +583,7 @@ def main(files):
925
  today = date.today().strftime("%Y_%m_%d")
926
  return f"result_{today}.csv"
927
 
928
- gradioApp = gr.Interface(fn=main, inputs=gr.File(file_count="multiple", file_type=".csv"), outputs="file")
929
 
930
  if __name__ == "__main__":
931
  # Calling main function
 
74
  w = 7
75
  prax = [0 for x in range(w)]
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  # %%
78
  # Function to train the model (TFT)
79
  def modelTFT(csv_file, prax):
 
575
  modelTFT_OpenGap(df, prax)
576
  prax[2] = "TFT_OpenGap"
577
  generate_csv(prax)
 
 
 
 
 
 
 
 
578
  # Generate blank line
579
  prax=["","","","","","",""]
580
  generate_csv(prax)
 
583
  today = date.today().strftime("%Y_%m_%d")
584
  return f"result_{today}.csv"
585
 
586
+ gradioApp = gr.Interface(fn=main, inputs=gr.File(file_count="multiple"), outputs="file")
587
 
588
  if __name__ == "__main__":
589
  # Calling main function