MCPcopy Create free account
hub / github.com/VivekPa/AIAlpha / NNModel

Class NNModel

models/nnmodel.py:8–66  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6import matplotlib.pyplot as plt
7
8class NNModel:
9 def __init__(self, input_shape):
10 self.input_shape = input_shape
11
12 def make_model(self):
13 input_data = kl.Input(shape=(1, self.input_shape))
14 lstm = kl.LSTM(5, input_shape=(1, self.input_shape), return_sequences=True, activity_regularizer=regularizers.l2(0.003),
15 recurrent_regularizer=regularizers.l2(0), dropout=0.2, recurrent_dropout=0.2)(input_data)
16 perc = kl.Dense(5, activation="sigmoid", activity_regularizer=regularizers.l2(0.005))(lstm)
17 lstm2 = kl.LSTM(2, activity_regularizer=regularizers.l2(0.01), recurrent_regularizer=regularizers.l2(0.001),
18 dropout=0.2, recurrent_dropout=0.2)(perc)
19 out = kl.Dense(1, activation="sigmoid", activity_regularizer=regularizers.l2(0.001))(lstm2)
20
21 model = Model(input_data, out)
22
23 self.model = model
24
25 def train_model(self, x, y, epochs, model_name, save_model=True):
26 self.model.compile(optimizer="adam", loss="mean_squared_error", metrics=["mse", "acc"])
27
28 # load data
29
30 train_x = np.reshape(np.array(x), (len(x), 1, self.input_shape))
31 train_y = np.array(y)
32 # train_stock = np.array(pd.read_csv("train_stock.csv"))
33
34 # train model
35
36 self.model.fit(train_x, train_y, epochs=epochs)
37
38 if save_model:
39 self.model.save(f"models/saved_models/{model_name}.h5", overwrite=True, include_optimizer=True)
40
41 def test_model(self, x, y):
42 test_x = np.reshape(np.array(x), (len(x), 1, self.input_shape))
43 test_y = np.array(y)
44
45 print(self.model.evaluate(test_x, test_y))
46
47 def predict_ret(self, x, y):
48 test_x = x
49 predicted_data = []
50 for i in test_x:
51 prediction = (self.model.predict(np.reshape(i, (1, 1, self.input_shape))))
52 predicted_data.append(np.reshape(prediction, (1,)))
53 return pd.DataFrame(predicted_data)
54
55 def predict_prices(self, x, y, prices):
56 test_x = x
57 test_y = y
58 prices = prices
59 prediction_data = []
60 stock_data = []
61 for i in range(len(test_y)):
62 prediction = (self.model.predict(np.reshape(test_x[i, :], (1, 1, self.input_shape))))
63 prediction_data.append(np.reshape(prediction, (1,)))
64 pred_price = np.exp(np.reshape(prediction, (1,)))*prices[i]
65 stock_data.append(pred_price)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected