Simple Sequence Prediction With LSTM

Simple Sequence Prediction With LSTM

We are going to learn about sequence prediction withLSTM model. We will pass an input sequence, predict the next value in the sequence.

What is LSTM?

Long short-term memory (LSTM) is an artificial recurrent neural network (RNN) architecture used in the field of deep learning. Unlike standard feedforward neural networks, LSTM has feedback connections. It can not only process single data points (such as images), but also entire sequences of data (such as speech or video).

For example, LSTM is applicable to tasks such as unsegmented, connected handwriting recognition, speech recognition and anomaly detection in network traffic or IDSs (intrusion detection systems).

LSTM networks are well-suited to classifying, processing and making predictions based on time series data, since there can be lags of unknown duration between important events in a time series.

A common LSTM unit is composed of a cell, an input gate, an output gate and a forget gate. The cell remembers values over arbitrary time intervals and the three gates regulate the flow of information into and out of the cell.

Create sequence data

In [1]:
data = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]

print(data)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]

Split data into X and y

We are going to split sequence like below:

X,                        y
10, 20, 30, 40, 50        60
20, 30, 40, 50, 60        70
30, 40, 50, 60, 70        80
40, 50, 60, 70, 80        90
50, 60, 70, 80, 90        100
60, 70, 80, 90, 100       110
70, 80, 90, 100, 110      120
...

Create a function

In [2]:
import numpy as np
In [3]:
def splitSequence(seq, n_steps):
    
    #Declare X and y as empty list
    X = []
    y = []
    
    for i in range(len(seq)):
        #get the last index
        lastIndex = i + n_steps
        
        #if lastIndex is greater than length of sequence then break
        if lastIndex > len(seq) - 1:
            break
            
        #Create input and output sequence
        seq_X, seq_y = seq[i:lastIndex], seq[lastIndex]
        
        #append seq_X, seq_y in X and y list
        X.append(seq_X)
        y.append(seq_y)

        pass

    #Convert X and y into numpy array
    X = np.array(X)
    y = np.array(y)
    
    return X,y 
    
    pass

Call the function

In [4]:
n_steps = 5
X, y = splitSequence(data, n_steps = 5)
In [5]:
print(X)
[[ 10  20  30  40  50]
 [ 20  30  40  50  60]
 [ 30  40  50  60  70]
 [ 40  50  60  70  80]
 [ 50  60  70  80  90]
 [ 60  70  80  90 100]
 [ 70  80  90 100 110]
 [ 80  90 100 110 120]
 [ 90 100 110 120 130]
 [100 110 120 130 140]
 [110 120 130 140 150]
 [120 130 140 150 160]
 [130 140 150 160 170]
 [140 150 160 170 180]
 [150 160 170 180 190]]
In [6]:
print(y)
[ 60  70  80  90 100 110 120 130 140 150 160 170 180 190 200]
In [7]:
for i in range(len(X)):
    print(X[i], y[i])
[10 20 30 40 50] 60
[20 30 40 50 60] 70
[30 40 50 60 70] 80
[40 50 60 70 80] 90
[50 60 70 80 90] 100
[ 60  70  80  90 100] 110
[ 70  80  90 100 110] 120
[ 80  90 100 110 120] 130
[ 90 100 110 120 130] 140
[100 110 120 130 140] 150
[110 120 130 140 150] 160
[120 130 140 150 160] 170
[130 140 150 160 170] 180
[140 150 160 170 180] 190
[150 160 170 180 190] 200

Reshape data

For tf.keras.layers.LSTM model we need inputs with shape [batch, timesteps, feature]. Model required data in this format only. For that we need to reshape our X data.

In [8]:
# reshape from [samples, timesteps] into [samples, timesteps, features]
n_features = 1
X = X.reshape((X.shape[0], X.shape[1], n_features))
print(X[:2])
[[[10]
  [20]
  [30]
  [40]
  [50]]

 [[20]
  [30]
  [40]
  [50]
  [60]]]

Create model

In [9]:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

Sequential model

A Sequential model is a plain stack of layers where each layer has exactly one input tensor and one output tensor.

We are adding LSTM layers in Sequential model via the add() method.

In [10]:
model = tf.keras.Sequential()
model.add(layers.LSTM(50, activation='relu', input_shape=(n_steps, n_features)))
model.add(layers.Dense(1))

View model layers

In [11]:
model.layers
Out[11]:
[<keras.layers.recurrent_v2.LSTM at 0x7fb46baebf10>,
 <keras.layers.core.dense.Dense at 0x7fb46bebd990>]

Model summary

In [12]:
model.summary()
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 (None, 50)                10400     
                                                                 
 dense (Dense)               (None, 1)                 51        
                                                                 
=================================================================
Total params: 10,451
Trainable params: 10,451
Non-trainable params: 0
_________________________________________________________________

Compile the model

Once the model is created, we can config the model with losses and metrics with model.compile().

In [13]:
model.compile(optimizer=tf.keras.optimizers.Adam(0.01), loss=tf.keras.losses.MeanSquaredError(),
              metrics=['accuracy'])

Train the model

We can train the model with model.fit()

In [14]:
model.fit(X, y, epochs=200, verbose=1)
Epoch 1/200
1/1 [==============================] - 2s 2s/step - loss: 10413.0127 - accuracy: 0.0000e+00
Epoch 2/200
1/1 [==============================] - 0s 6ms/step - loss: 7116.9512 - accuracy: 0.0000e+00
Epoch 3/200
1/1 [==============================] - 0s 3ms/step - loss: 2812.4146 - accuracy: 0.0000e+00
Epoch 4/200
1/1 [==============================] - 0s 3ms/step - loss: 3834.2087 - accuracy: 0.0000e+00
Epoch 5/200
1/1 [==============================] - 0s 3ms/step - loss: 1304.7417 - accuracy: 0.0000e+00
Epoch 6/200
1/1 [==============================] - 0s 3ms/step - loss: 217.0670 - accuracy: 0.0000e+00
Epoch 7/200
1/1 [==============================] - 0s 3ms/step - loss: 646.7045 - accuracy: 0.0000e+00
Epoch 8/200
1/1 [==============================] - 0s 3ms/step - loss: 731.9691 - accuracy: 0.0000e+00
Epoch 9/200
1/1 [==============================] - 0s 3ms/step - loss: 155.1982 - accuracy: 0.0000e+00
Epoch 10/200
1/1 [==============================] - 0s 3ms/step - loss: 630.9560 - accuracy: 0.0000e+00
Epoch 11/200
1/1 [==============================] - 0s 3ms/step - loss: 93.0730 - accuracy: 0.0000e+00
Epoch 12/200
1/1 [==============================] - 0s 3ms/step - loss: 163.5246 - accuracy: 0.0000e+00
Epoch 13/200
1/1 [==============================] - 0s 3ms/step - loss: 225.3926 - accuracy: 0.0000e+00
Epoch 14/200
1/1 [==============================] - 0s 3ms/step - loss: 76.3336 - accuracy: 0.0000e+00
Epoch 15/200
1/1 [==============================] - 0s 3ms/step - loss: 854.5501 - accuracy: 0.0000e+00
Epoch 16/200
1/1 [==============================] - 0s 3ms/step - loss: 54.8484 - accuracy: 0.0000e+00
Epoch 17/200
1/1 [==============================] - 0s 3ms/step - loss: 167.2720 - accuracy: 0.0000e+00
Epoch 18/200
1/1 [==============================] - 0s 3ms/step - loss: 125.8325 - accuracy: 0.0000e+00
Epoch 19/200
1/1 [==============================] - 0s 3ms/step - loss: 97.3226 - accuracy: 0.0000e+00
Epoch 20/200
1/1 [==============================] - 0s 4ms/step - loss: 108.3110 - accuracy: 0.0000e+00
Epoch 21/200
1/1 [==============================] - 0s 3ms/step - loss: 121.1308 - accuracy: 0.0000e+00
Epoch 22/200
1/1 [==============================] - 0s 3ms/step - loss: 109.1596 - accuracy: 0.0000e+00
Epoch 23/200
1/1 [==============================] - 0s 3ms/step - loss: 82.1777 - accuracy: 0.0000e+00
Epoch 24/200
1/1 [==============================] - 0s 3ms/step - loss: 83.9420 - accuracy: 0.0000e+00
Epoch 25/200
1/1 [==============================] - 0s 3ms/step - loss: 112.6196 - accuracy: 0.0000e+00
Epoch 26/200
1/1 [==============================] - 0s 3ms/step - loss: 118.2512 - accuracy: 0.0000e+00
Epoch 27/200
1/1 [==============================] - 0s 3ms/step - loss: 136.9690 - accuracy: 0.0000e+00
Epoch 28/200
1/1 [==============================] - 0s 3ms/step - loss: 100.8480 - accuracy: 0.0000e+00
Epoch 29/200
1/1 [==============================] - 0s 3ms/step - loss: 98.6653 - accuracy: 0.0000e+00
Epoch 30/200
1/1 [==============================] - 0s 3ms/step - loss: 36.5257 - accuracy: 0.0000e+00
Epoch 31/200
1/1 [==============================] - 0s 4ms/step - loss: 493.9827 - accuracy: 0.0000e+00
Epoch 32/200
1/1 [==============================] - 0s 3ms/step - loss: 338.6204 - accuracy: 0.0000e+00
Epoch 33/200
1/1 [==============================] - 0s 3ms/step - loss: 2106.4126 - accuracy: 0.0000e+00
Epoch 34/200
1/1 [==============================] - 0s 3ms/step - loss: 1638.2826 - accuracy: 0.0000e+00
Epoch 35/200
1/1 [==============================] - 0s 3ms/step - loss: 5541.6514 - accuracy: 0.0000e+00
Epoch 36/200
1/1 [==============================] - 0s 3ms/step - loss: 2852.5166 - accuracy: 0.0000e+00
Epoch 37/200
1/1 [==============================] - 0s 3ms/step - loss: 124.8457 - accuracy: 0.0000e+00
Epoch 38/200
1/1 [==============================] - 0s 3ms/step - loss: 955.6233 - accuracy: 0.0000e+00
Epoch 39/200
1/1 [==============================] - 0s 3ms/step - loss: 2476.3389 - accuracy: 0.0000e+00
Epoch 40/200
1/1 [==============================] - 0s 3ms/step - loss: 1772.1570 - accuracy: 0.0000e+00
Epoch 41/200
1/1 [==============================] - 0s 3ms/step - loss: 425.1154 - accuracy: 0.0000e+00
Epoch 42/200
1/1 [==============================] - 0s 3ms/step - loss: 128.4906 - accuracy: 0.0000e+00
Epoch 43/200
1/1 [==============================] - 0s 3ms/step - loss: 638.3139 - accuracy: 0.0000e+00
Epoch 44/200
1/1 [==============================] - 0s 3ms/step - loss: 1158.2805 - accuracy: 0.0000e+00
Epoch 45/200
1/1 [==============================] - 0s 4ms/step - loss: 1189.1686 - accuracy: 0.0000e+00
Epoch 46/200
1/1 [==============================] - 0s 3ms/step - loss: 796.1451 - accuracy: 0.0000e+00
Epoch 47/200
1/1 [==============================] - 0s 3ms/step - loss: 323.4146 - accuracy: 0.0000e+00
Epoch 48/200
1/1 [==============================] - 0s 4ms/step - loss: 116.7890 - accuracy: 0.0000e+00
Epoch 49/200
1/1 [==============================] - 0s 4ms/step - loss: 224.7191 - accuracy: 0.0000e+00
Epoch 50/200
1/1 [==============================] - 0s 4ms/step - loss: 445.8188 - accuracy: 0.0000e+00
Epoch 51/200
1/1 [==============================] - 0s 3ms/step - loss: 453.2119 - accuracy: 0.0000e+00
Epoch 52/200
1/1 [==============================] - 0s 3ms/step - loss: 356.7904 - accuracy: 0.0000e+00
Epoch 53/200
1/1 [==============================] - 0s 3ms/step - loss: 226.2921 - accuracy: 0.0000e+00
Epoch 54/200
1/1 [==============================] - 0s 4ms/step - loss: 131.0345 - accuracy: 0.0000e+00
Epoch 55/200
1/1 [==============================] - 0s 3ms/step - loss: 124.1155 - accuracy: 0.0000e+00
Epoch 56/200
1/1 [==============================] - 0s 3ms/step - loss: 171.2433 - accuracy: 0.0000e+00
Epoch 57/200
1/1 [==============================] - 0s 4ms/step - loss: 219.0449 - accuracy: 0.0000e+00
Epoch 58/200
1/1 [==============================] - 0s 3ms/step - loss: 233.4140 - accuracy: 0.0000e+00
Epoch 59/200
1/1 [==============================] - 0s 3ms/step - loss: 205.3197 - accuracy: 0.0000e+00
Epoch 60/200
1/1 [==============================] - 0s 3ms/step - loss: 151.4307 - accuracy: 0.0000e+00
Epoch 61/200
1/1 [==============================] - 0s 3ms/step - loss: 101.4590 - accuracy: 0.0000e+00
Epoch 62/200
1/1 [==============================] - 0s 3ms/step - loss: 111.1217 - accuracy: 0.0000e+00
Epoch 63/200
1/1 [==============================] - 0s 4ms/step - loss: 107.8686 - accuracy: 0.0000e+00
Epoch 64/200
1/1 [==============================] - 0s 3ms/step - loss: 124.0910 - accuracy: 0.0000e+00
Epoch 65/200
1/1 [==============================] - 0s 3ms/step - loss: 130.6553 - accuracy: 0.0000e+00
Epoch 66/200
1/1 [==============================] - 0s 3ms/step - loss: 120.5773 - accuracy: 0.0000e+00
Epoch 67/200
1/1 [==============================] - 0s 3ms/step - loss: 102.8656 - accuracy: 0.0000e+00
Epoch 68/200
1/1 [==============================] - 0s 4ms/step - loss: 89.4421 - accuracy: 0.0000e+00
Epoch 69/200
1/1 [==============================] - 0s 3ms/step - loss: 85.9206 - accuracy: 0.0000e+00
Epoch 70/200
1/1 [==============================] - 0s 3ms/step - loss: 89.5462 - accuracy: 0.0000e+00
Epoch 71/200
1/1 [==============================] - 0s 3ms/step - loss: 92.6883 - accuracy: 0.0000e+00
Epoch 72/200
1/1 [==============================] - 0s 3ms/step - loss: 89.5906 - accuracy: 0.0000e+00
Epoch 73/200
1/1 [==============================] - 0s 3ms/step - loss: 79.4609 - accuracy: 0.0000e+00
Epoch 74/200
1/1 [==============================] - 0s 3ms/step - loss: 66.3644 - accuracy: 0.0000e+00
Epoch 75/200
1/1 [==============================] - 0s 3ms/step - loss: 55.4820 - accuracy: 0.0000e+00
Epoch 76/200
1/1 [==============================] - 0s 3ms/step - loss: 49.4842 - accuracy: 0.0000e+00
Epoch 77/200
1/1 [==============================] - 0s 3ms/step - loss: 47.1161 - accuracy: 0.0000e+00
Epoch 78/200
1/1 [==============================] - 0s 3ms/step - loss: 45.3359 - accuracy: 0.0000e+00
Epoch 79/200
1/1 [==============================] - 0s 3ms/step - loss: 43.9611 - accuracy: 0.0000e+00
Epoch 80/200
1/1 [==============================] - 0s 3ms/step - loss: 38.6174 - accuracy: 0.0000e+00
Epoch 81/200
1/1 [==============================] - 0s 3ms/step - loss: 26.0606 - accuracy: 0.0000e+00
Epoch 82/200
1/1 [==============================] - 0s 3ms/step - loss: 19.8124 - accuracy: 0.0000e+00
Epoch 83/200
1/1 [==============================] - 0s 3ms/step - loss: 19.3433 - accuracy: 0.0000e+00
Epoch 84/200
1/1 [==============================] - 0s 3ms/step - loss: 19.8854 - accuracy: 0.0000e+00
Epoch 85/200
1/1 [==============================] - 0s 3ms/step - loss: 17.2370 - accuracy: 0.0000e+00
Epoch 86/200
1/1 [==============================] - 0s 3ms/step - loss: 10.5664 - accuracy: 0.0000e+00
Epoch 87/200
1/1 [==============================] - 0s 3ms/step - loss: 6.8405 - accuracy: 0.0000e+00
Epoch 88/200
1/1 [==============================] - 0s 4ms/step - loss: 9.4045 - accuracy: 0.0000e+00
Epoch 89/200
1/1 [==============================] - 0s 3ms/step - loss: 4.9071 - accuracy: 0.0000e+00
Epoch 90/200
1/1 [==============================] - 0s 4ms/step - loss: 2.0464 - accuracy: 0.0000e+00
Epoch 91/200
1/1 [==============================] - 0s 4ms/step - loss: 2.7967 - accuracy: 0.0000e+00
Epoch 92/200
1/1 [==============================] - 0s 4ms/step - loss: 3.2682 - accuracy: 0.0000e+00
Epoch 93/200
1/1 [==============================] - 0s 4ms/step - loss: 3.4411 - accuracy: 0.0000e+00
Epoch 94/200
1/1 [==============================] - 0s 3ms/step - loss: 3.7020 - accuracy: 0.0000e+00
Epoch 95/200
1/1 [==============================] - 0s 4ms/step - loss: 4.1227 - accuracy: 0.0000e+00
Epoch 96/200
1/1 [==============================] - 0s 3ms/step - loss: 4.3677 - accuracy: 0.0000e+00
Epoch 97/200
1/1 [==============================] - 0s 3ms/step - loss: 4.1275 - accuracy: 0.0000e+00
Epoch 98/200
1/1 [==============================] - 0s 3ms/step - loss: 3.5450 - accuracy: 0.0000e+00
Epoch 99/200
1/1 [==============================] - 0s 3ms/step - loss: 3.0127 - accuracy: 0.0000e+00
Epoch 100/200
1/1 [==============================] - 0s 3ms/step - loss: 2.6720 - accuracy: 0.0000e+00
Epoch 101/200
1/1 [==============================] - 0s 3ms/step - loss: 2.3487 - accuracy: 0.0000e+00
Epoch 102/200
1/1 [==============================] - 0s 4ms/step - loss: 1.9205 - accuracy: 0.0000e+00
Epoch 103/200
1/1 [==============================] - 0s 3ms/step - loss: 1.5489 - accuracy: 0.0000e+00
Epoch 104/200
1/1 [==============================] - 0s 3ms/step - loss: 1.4313 - accuracy: 0.0000e+00
Epoch 105/200
1/1 [==============================] - 0s 4ms/step - loss: 1.4754 - accuracy: 0.0000e+00
Epoch 106/200
1/1 [==============================] - 0s 3ms/step - loss: 1.4557 - accuracy: 0.0000e+00
Epoch 107/200
1/1 [==============================] - 0s 4ms/step - loss: 1.3159 - accuracy: 0.0000e+00
Epoch 108/200
1/1 [==============================] - 0s 4ms/step - loss: 1.1845 - accuracy: 0.0000e+00
Epoch 109/200
1/1 [==============================] - 0s 3ms/step - loss: 1.1369 - accuracy: 0.0000e+00
Epoch 110/200
1/1 [==============================] - 0s 3ms/step - loss: 1.0835 - accuracy: 0.0000e+00
Epoch 111/200
1/1 [==============================] - 0s 3ms/step - loss: 0.9451 - accuracy: 0.0000e+00
Epoch 112/200
1/1 [==============================] - 0s 4ms/step - loss: 0.7972 - accuracy: 0.0000e+00
Epoch 113/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7499 - accuracy: 0.0000e+00
Epoch 114/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7689 - accuracy: 0.0000e+00
Epoch 115/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7510 - accuracy: 0.0000e+00
Epoch 116/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7126 - accuracy: 0.0000e+00
Epoch 117/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7327 - accuracy: 0.0000e+00
Epoch 118/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7792 - accuracy: 0.0000e+00
Epoch 119/200
1/1 [==============================] - 0s 4ms/step - loss: 0.7648 - accuracy: 0.0000e+00
Epoch 120/200
1/1 [==============================] - 0s 3ms/step - loss: 0.7042 - accuracy: 0.0000e+00
Epoch 121/200
1/1 [==============================] - 0s 3ms/step - loss: 0.6644 - accuracy: 0.0000e+00
Epoch 122/200
1/1 [==============================] - 0s 3ms/step - loss: 0.6311 - accuracy: 0.0000e+00
Epoch 123/200
1/1 [==============================] - 0s 3ms/step - loss: 0.5590 - accuracy: 0.0000e+00
Epoch 124/200
1/1 [==============================] - 0s 3ms/step - loss: 0.4786 - accuracy: 0.0000e+00
Epoch 125/200
1/1 [==============================] - 0s 3ms/step - loss: 0.4360 - accuracy: 0.0000e+00
Epoch 126/200
1/1 [==============================] - 0s 3ms/step - loss: 0.4118 - accuracy: 0.0000e+00
Epoch 127/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3825 - accuracy: 0.0000e+00
Epoch 128/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3536 - accuracy: 0.0000e+00
Epoch 129/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3573 - accuracy: 0.0000e+00
Epoch 130/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3652 - accuracy: 0.0000e+00
Epoch 131/200
1/1 [==============================] - 0s 4ms/step - loss: 0.3575 - accuracy: 0.0000e+00
Epoch 132/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3463 - accuracy: 0.0000e+00
Epoch 133/200
1/1 [==============================] - 0s 4ms/step - loss: 0.3423 - accuracy: 0.0000e+00
Epoch 134/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3317 - accuracy: 0.0000e+00
Epoch 135/200
1/1 [==============================] - 0s 3ms/step - loss: 0.3063 - accuracy: 0.0000e+00
Epoch 136/200
1/1 [==============================] - 0s 3ms/step - loss: 0.2788 - accuracy: 0.0000e+00
Epoch 137/200
1/1 [==============================] - 0s 3ms/step - loss: 0.2586 - accuracy: 0.0000e+00
Epoch 138/200
1/1 [==============================] - 0s 3ms/step - loss: 0.2376 - accuracy: 0.0000e+00
Epoch 139/200
1/1 [==============================] - 0s 3ms/step - loss: 0.2131 - accuracy: 0.0000e+00
Epoch 140/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1950 - accuracy: 0.0000e+00
Epoch 141/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1858 - accuracy: 0.0000e+00
Epoch 142/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1770 - accuracy: 0.0000e+00
Epoch 143/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1667 - accuracy: 0.0000e+00
Epoch 144/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1601 - accuracy: 0.0000e+00
Epoch 145/200
1/1 [==============================] - 0s 4ms/step - loss: 0.1556 - accuracy: 0.0000e+00
Epoch 146/200
1/1 [==============================] - 0s 4ms/step - loss: 0.1471 - accuracy: 0.0000e+00
Epoch 147/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1363 - accuracy: 0.0000e+00
Epoch 148/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1276 - accuracy: 0.0000e+00
Epoch 149/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1192 - accuracy: 0.0000e+00
Epoch 150/200
1/1 [==============================] - 0s 3ms/step - loss: 0.1089 - accuracy: 0.0000e+00
Epoch 151/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0998 - accuracy: 0.0000e+00
Epoch 152/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0938 - accuracy: 0.0000e+00
Epoch 153/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0885 - accuracy: 0.0000e+00
Epoch 154/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0831 - accuracy: 0.0000e+00
Epoch 155/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0795 - accuracy: 0.0000e+00
Epoch 156/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0774 - accuracy: 0.0000e+00
Epoch 157/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0746 - accuracy: 0.0000e+00
Epoch 158/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0715 - accuracy: 0.0000e+00
Epoch 159/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0693 - accuracy: 0.0000e+00
Epoch 160/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0672 - accuracy: 0.0000e+00
Epoch 161/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0643 - accuracy: 0.0000e+00
Epoch 162/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0616 - accuracy: 0.0000e+00
Epoch 163/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0596 - accuracy: 0.0000e+00
Epoch 164/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0575 - accuracy: 0.0000e+00
Epoch 165/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0553 - accuracy: 0.0000e+00
Epoch 166/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0535 - accuracy: 0.0000e+00
Epoch 167/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0520 - accuracy: 0.0000e+00
Epoch 168/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0503 - accuracy: 0.0000e+00
Epoch 169/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0487 - accuracy: 0.0000e+00
Epoch 170/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0473 - accuracy: 0.0000e+00
Epoch 171/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0460 - accuracy: 0.0000e+00
Epoch 172/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0444 - accuracy: 0.0000e+00
Epoch 173/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0429 - accuracy: 0.0000e+00
Epoch 174/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0415 - accuracy: 0.0000e+00
Epoch 175/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0400 - accuracy: 0.0000e+00
Epoch 176/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0384 - accuracy: 0.0000e+00
Epoch 177/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0371 - accuracy: 0.0000e+00
Epoch 178/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0358 - accuracy: 0.0000e+00
Epoch 179/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0346 - accuracy: 0.0000e+00
Epoch 180/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0335 - accuracy: 0.0000e+00
Epoch 181/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0326 - accuracy: 0.0000e+00
Epoch 182/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0318 - accuracy: 0.0000e+00
Epoch 183/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0309 - accuracy: 0.0000e+00
Epoch 184/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0302 - accuracy: 0.0000e+00
Epoch 185/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0295 - accuracy: 0.0000e+00
Epoch 186/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0287 - accuracy: 0.0000e+00
Epoch 187/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0280 - accuracy: 0.0000e+00
Epoch 188/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0274 - accuracy: 0.0000e+00
Epoch 189/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0267 - accuracy: 0.0000e+00
Epoch 190/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0261 - accuracy: 0.0000e+00
Epoch 191/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0256 - accuracy: 0.0000e+00
Epoch 192/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0251 - accuracy: 0.0000e+00
Epoch 193/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0245 - accuracy: 0.0000e+00
Epoch 194/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0241 - accuracy: 0.0000e+00
Epoch 195/200
1/1 [==============================] - 0s 4ms/step - loss: 0.0236 - accuracy: 0.0000e+00
Epoch 196/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0231 - accuracy: 0.0000e+00
Epoch 197/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0226 - accuracy: 0.0000e+00
Epoch 198/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0221 - accuracy: 0.0000e+00
Epoch 199/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0217 - accuracy: 0.0000e+00
Epoch 200/200
1/1 [==============================] - 0s 3ms/step - loss: 0.0212 - accuracy: 0.0000e+00
Out[14]:
<keras.callbacks.History at 0x7fb468110c90>
In [15]:
import pickle
In [16]:
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
INFO:tensorflow:Assets written to: ram://e2570ef8-7625-4889-84b0-4a45d29ac9a6/assets
WARNING:absl:<keras.layers.recurrent.LSTMCell object at 0x7fb46ba9a410> has the same name 'LSTMCell' as a built-in Keras object. Consider renaming <class 'keras.layers.recurrent.LSTMCell'> to avoid naming conflicts when loading with `tf.keras.models.load_model`. If renaming is not possible, pass the object in the `custom_objects` parameter of the load function.
In [17]:
loaded_model = pickle.load(open(filename, 'rb'))
In [ ]:
 

Predict

In [18]:
test_data = np.array([90, 100, 110, 120, 130])
test_data = test_data.reshape((1, n_steps, n_features))
test_data
Out[18]:
array([[[ 90],
        [100],
        [110],
        [120],
        [130]]])
In [19]:
predictNextNumber = loaded_model.predict(test_data, verbose=1)
print(predictNextNumber)
1/1 [==============================] - 0s 144ms/step
[[139.98471]]
In [20]:
predictNextNumber = model.predict(test_data, verbose=1)
print(predictNextNumber)
1/1 [==============================] - 0s 111ms/step
[[139.98471]]
dict_pickle_in = open("/home/jupyter-thakur/private-docs/blog/output/trained_model.pickle","rb")trained_model = pickle.load(dict_pickle_in)
In [21]:
predictNextNumber = model.predict(test_data, verbose=1)
print(predictNextNumber)
1/1 [==============================] - 0s 13ms/step
[[139.98471]]

Predict again with different test data

In [22]:
test_data = np.array([160, 170, 180, 190, 200])
test_data = test_data.reshape((1, n_steps, n_features))
test_data
Out[22]:
array([[[160],
        [170],
        [180],
        [190],
        [200]]])
In [23]:
predictNextNumber = model.predict(test_data, verbose=1)
print(predictNextNumber)
1/1 [==============================] - 0s 13ms/step
[[210.318]]
In [ ]:
 
In [24]:
test_data = np.array([200, 210, 220, 230, 240])
test_data = test_data.reshape((1, 5, 1))
test_data
Out[24]:
array([[[200],
        [210],
        [220],
        [230],
        [240]]])
In [25]:
predictNextNumber = model.predict(test_data, verbose=1)
print(predictNextNumber)
1/1 [==============================] - 0s 13ms/step
[[252.85214]]
In [ ]:
 

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing