Keras

Keras Time Series Prediction using LSTM RNN

Keras Time Series Prediction using LSTM RNN

This chapter will introduce you to LSTM (Long Short Term Memory) model that is based on RNN. This model can be used to implement the sequence analysis. 
You can follow the steps described below to create an LSTM model:

Step – 1: Import the necessary modules
from keras.preprocessing import sequence 
from keras.models import Sequential 
from keras.layers import Dense, Embedding 
from keras.layers import LSTM 
from keras.datasets import dset

Step – 2:  Load the dataset

(model_train, model2_train), (model_test, model2_test) = dset.load_data(total_words = 3000)

Step – 3: Process the dataset

model_train = sequence.pad_sequences(model_train, maxlen=90) 
model_test = sequence.pad_sequences(model_test, maxlen=90)

Step – 4: Create the model and then Compile it
Let’s create the model with the following code:

model = Sequential() 
model.add(Embedding(3000, 128)) 
model.add(LSTM(128, dropout = 0.2, recurrent_dropout = 0.2)) 
model.add(Dense(1, activation = 'sigmoid'))

Now, to compile the code, follow the code given below:

model.compile(loss = 'binary_crossentropy', 
   optimizer = 'opt', metrics = ['accuracy'])

Step – 5: Training and Evaluation of the model

model.fit(
   model_train, model2_train, 
   batch_size = 32, 
   epochs = 20, 
   validation_data = (model_test, model2_test)
)

Step – 6: Model Evaluation

score, acc = model.evaluate(model_test, model2_test, batch_size = 32) 
   
print('The total test score is:', score) 
print('The total test accuracy is:', acc)

You can simply follow the steps described above to test the results of the model.

Top course recommendations for you

    Python Practice Codes
    1 hrs
    Beginner
    8.5K+ Learners
    4.37  (435)
    VLOOKUP in Excel
    1 hrs
    Beginner
    44.8K+ Learners
    4.57  (3373)
    Blockchain Process
    1 hrs
    Beginner
    6.9K+ Learners
    4.56  (578)
    GO Programming Language
    1 hrs
    Beginner
    6.7K+ Learners
    4.48  (510)
    Conditional Formatting in Excel
    1 hrs
    Beginner
    17.4K+ Learners
    4.51  (1637)
    Fibonacci Series in Python
    1 hrs
    Beginner
    2.2K+ Learners
    4.6  (91)
    Design App
    1 hrs
    Beginner
    18.1K+ Learners
    4.44  (1180)
    Pivot Tables in Excel
    1 hrs
    Beginner
    21.4K+ Learners
    4.54  (2301)
    Divide and Conquer Algorithms
    1 hrs
    Beginner
    2.3K+ Learners
    4.57  (332)
    QR Code Generator in Python
    1 hrs
    Beginner
    4.7K+ Learners
    4.36  (210)