Skip to content

Commit c5c1e89

Browse files
Update README.md
1 parent c7759b9 commit c5c1e89

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,5 +447,66 @@ LSTM network needs the data imported as a 3D array. To translate this 2D array i
447447
To create this model, you will need to have TensorFlow, TensorFlow-Gpu and Keras install in order for this to run. The code for this model can be seen below and the explanation for each layer is also defined below:
448448

449449
```python
450+
def create_long_short_term_memory_model(x_train):
451+
model = Sequential()
452+
# 1st layer with Dropout regularisation
453+
# * units = add 100 neurons is the dimensionality of the output space
454+
# * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
455+
# * input_shape => Shape of the training dataset
456+
model.add(LSTM(units=100, return_sequences=True, input_shape=(x_train.shape[1], 1)))
457+
# 20% of the layers will be dropped
458+
model.add(Dropout(0.2))
459+
# 2nd LSTM layer
460+
# * units = add 50 neurons is the dimensionality of the output space
461+
# * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
462+
model.add(LSTM(units=50, return_sequences=True))
463+
# 20% of the layers will be dropped
464+
model.add(Dropout(0.2))
465+
# 3rd LSTM layer
466+
# * units = add 50 neurons is the dimensionality of the output space
467+
# * return_sequences = True to stack LSTM layers so the next LSTM layer has a three-dimensional sequence input
468+
model.add(LSTM(units=50, return_sequences=True))
469+
# 50% of the layers will be dropped
470+
model.add(Dropout(0.5))
471+
# 4th LSTM layer
472+
# * units = add 50 neurons is the dimensionality of the output space
473+
model.add(LSTM(units=50))
474+
# 50% of the layers will be dropped
475+
model.add(Dropout(0.5))
476+
# Dense layer that specifies an output of one unit
477+
model.add(Dense(units=1))
478+
model.summary()
479+
tf.keras.utils.plot_model(model, to_file=os.path.join(project_folder, 'model_lstm.png'), show_shapes=True,
480+
show_layer_names=True)
481+
return model
482+
```
483+
484+
The rendered model can be seen in the image below, producing a model with more than 100k trainable parameters.
485+
![](https://github.com/JordiCorbilla/stock-prediction-deep-neural-learning/raw/master/model_lstm.png)
450486

487+
```cmd
488+
Layer (type) Output Shape Param #
489+
=================================================================
490+
lstm_1 (LSTM) (None, 60, 100) 40800
491+
_________________________________________________________________
492+
dropout_1 (Dropout) (None, 60, 100) 0
493+
_________________________________________________________________
494+
lstm_2 (LSTM) (None, 60, 50) 30200
495+
_________________________________________________________________
496+
dropout_2 (Dropout) (None, 60, 50) 0
497+
_________________________________________________________________
498+
lstm_3 (LSTM) (None, 60, 50) 20200
499+
_________________________________________________________________
500+
dropout_3 (Dropout) (None, 60, 50) 0
501+
_________________________________________________________________
502+
lstm_4 (LSTM) (None, 50) 20200
503+
_________________________________________________________________
504+
dropout_4 (Dropout) (None, 50) 0
505+
_________________________________________________________________
506+
dense_1 (Dense) (None, 1) 51
507+
=================================================================
508+
Total params: 111,451
509+
Trainable params: 111,451
510+
Non-trainable params: 0
451511
```
512+

0 commit comments

Comments
 (0)