Making predictions using the LSTM model

Now we will start making predictions on the test set:

predicted_output = []
i = 0
while i+batch_size <= len(X_test):

output = session.run([y_hat],feed_dict={input:X_test[i:i+batch_size]})
i += batch_size
predicted_output.append(output)

Print the predicted output:

predicted_output[0]

We will get the result as shown:

[[array([[-0.60426176]], dtype=float32),
  array([[-0.60155034]], dtype=float32),
  array([[-0.60079575]], dtype=float32),
  array([[-0.599668]], dtype=float32),
  array([[-0.5991149]], dtype=float32),
  array([[-0.6008351]], dtype=float32),
  array([[-0.5970466]], dtype=float32)]]

As you can see, the values of test predictions are in a nested list, so we will flatten them:

predicted_values_test = []
for i in range(len(predicted_output)):
for j in range(len(predicted_output[i][0])):
predicted_values_test.append(predicted_output[i][0][j])

Now, if we print the predicted values, they are no longer in a nested list:

predicted_values_test[0]

array([[-0.60426176]], dtype=float32)

As we took the first 1000 points as a training set, we make predictions for time steps greater than 1000:

predictions = []
for i in range(1280):
if i >= 1000:
predictions.append(predicted_values_test[i-1019])
else:
predictions.append(None)

We plot and see how well the predicted value matches the actual value:

plt.figure(figsize=(16, 7))
plt.plot(data, label='Actual')
plt.plot(predictions, label='Predicted')
plt.legend()
plt.xlabel('Days')
plt.ylabel('Price')
plt.grid()
plt.show()

As you can see in the following plot, the actual value is shown in red and the predicted value is shown in blue. As we are making predictions for time steps greater than 1000, you can see after time step 1000, the red and blue lines into each other, which implies that our model has correctly predicted the actual values:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset