1515import os
1616import secrets
1717import pandas as pd
18- from keras import Sequential
19- from keras .layers import LSTM , Dropout , Dense
2018import tensorflow as tf
2119from sklearn .preprocessing import MinMaxScaler
2220import datetime
2321import numpy as np
2422import yfinance as yf
23+
24+ from stock_prediction_lstm import LongShortTermMemory
2525from stock_prediction_plotter import Plotter
2626os .environ ["PATH" ] += os .pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin/'
2727
@@ -32,41 +32,6 @@ def data_verification(train):
3232 print ('min' , train .min ())
3333 print ('Std dev:' , train .std (axis = 0 ))
3434
35-
36- def create_long_short_term_memory_model (x_train ):
37- model = Sequential ()
38- # 1st layer with Dropout regularisation
39- # * units = add 100 neurons is the dimensionality of the output space
40- # * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
41- # * input_shape => Shape of the training dataset
42- model .add (LSTM (units = 100 , return_sequences = True , input_shape = (x_train .shape [1 ], 1 )))
43- # 20% of the layers will be dropped
44- model .add (Dropout (0.2 ))
45- # 2nd LSTM layer
46- # * units = add 50 neurons is the dimensionality of the output space
47- # * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
48- model .add (LSTM (units = 50 , return_sequences = True ))
49- # 20% of the layers will be dropped
50- model .add (Dropout (0.2 ))
51- # 3rd LSTM layer
52- # * units = add 50 neurons is the dimensionality of the output space
53- # * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
54- model .add (LSTM (units = 50 , return_sequences = True ))
55- # 50% of the layers will be dropped
56- model .add (Dropout (0.5 ))
57- # 4th LSTM layer
58- # * units = add 50 neurons is the dimensionality of the output space
59- model .add (LSTM (units = 50 ))
60- # 50% of the layers will be dropped
61- model .add (Dropout (0.5 ))
62- # Dense layer that specifies an output of one unit
63- model .add (Dense (units = 1 ))
64- model .summary ()
65- tf .keras .utils .plot_model (model , to_file = os .path .join (project_folder , 'model_lstm.png' ), show_shapes = True ,
66- show_layer_names = True )
67- return model
68-
69-
7035def load_data_transform (time_steps , min_max , training_data , test_data ):
7136 train_scaled = min_max .fit_transform (training_data )
7237 data_verification (train_scaled )
@@ -114,7 +79,8 @@ def train_LSTM_network(start_date, ticker, validation_date):
11479
11580 (x_train , y_train ), (x_test , y_test ) = load_data_transform (60 , min_max , training_data , test_data )
11681
117- model = create_long_short_term_memory_model (x_train )
82+ lstm = LongShortTermMemory (project_folder )
83+ model = lstm .create_model (x_train )
11884
11985 defined_metrics = [
12086 tf .keras .metrics .MeanSquaredError (name = 'MSE' )
0 commit comments