Skip to content

Commit 79c2f04

Browse files
committed
adding lstm model
1 parent 27fefae commit 79c2f04

File tree

2 files changed

+60
-38
lines changed

2 files changed

+60
-38
lines changed

stock_prediction_deep_learning.py

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import os
1616
import secrets
1717
import pandas as pd
18-
from keras import Sequential
19-
from keras.layers import LSTM, Dropout, Dense
2018
import tensorflow as tf
2119
from sklearn.preprocessing import MinMaxScaler
2220
import datetime
2321
import numpy as np
2422
import yfinance as yf
23+
24+
from stock_prediction_lstm import LongShortTermMemory
2525
from stock_prediction_plotter import Plotter
2626
os.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-
7035
def 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')

stock_prediction_lstm.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2020 Jordi Corbilla. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
import os
16+
import tensorflow as tf
17+
from tensorflow.python.keras import Sequential
18+
from tensorflow.python.keras.layers import Dropout, Dense, LSTM
19+
20+
21+
class LongShortTermMemory:
22+
def __init__(self, project_folder):
23+
self.project_folder = project_folder
24+
25+
def create_model(self, x_train):
26+
model = Sequential()
27+
# 1st layer with Dropout regularisation
28+
# * units = add 100 neurons is the dimensionality of the output space
29+
# * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
30+
# * input_shape => Shape of the training dataset
31+
model.add(LSTM(units=100, return_sequences=True, input_shape=(x_train.shape[1], 1)))
32+
# 20% of the layers will be dropped
33+
model.add(Dropout(0.2))
34+
# 2nd LSTM layer
35+
# * units = add 50 neurons is the dimensionality of the output space
36+
# * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
37+
model.add(LSTM(units=50, return_sequences=True))
38+
# 20% of the layers will be dropped
39+
model.add(Dropout(0.2))
40+
# 3rd LSTM layer
41+
# * units = add 50 neurons is the dimensionality of the output space
42+
# * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
43+
model.add(LSTM(units=50, return_sequences=True))
44+
# 50% of the layers will be dropped
45+
model.add(Dropout(0.5))
46+
# 4th LSTM layer
47+
# * units = add 50 neurons is the dimensionality of the output space
48+
model.add(LSTM(units=50))
49+
# 50% of the layers will be dropped
50+
model.add(Dropout(0.5))
51+
# Dense layer that specifies an output of one unit
52+
model.add(Dense(units=1))
53+
model.summary()
54+
tf.keras.utils.plot_model(model, to_file=os.path.join(self.project_folder, 'model_lstm.png'), show_shapes=True,
55+
show_layer_names=True)
56+
return model

0 commit comments

Comments
 (0)