NaNs in predictions with LSTM

I have an LSTM model that I have trained and tested it with a dataset. Now I want to test it to an other dataset and I use the following snippet:

from keras.models import load_model
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_percentage_error

model = load_model('lstm.h5')
df = pd.read_csv('datasets/Residential_4.csv')
data = df['energy_kWh'].values
data = data.reshape((-1,1))
scaler = MinMaxScaler(feature_range=(0,1))
data = scaler.fit_transform(data)

lookback = 7 * 24
prediction_horizon = 24

X_test, Y_test   = Create_Dataset(data, lookback, prediction_horizon)
X_test  = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

test_predict  = model.predict(X_test)

test_predict  = scaler.inverse_transform(test_predict)
Y_test = scaler.inverse_transform(Y_test)

def Create_Dataset(df, lookback=1, prediction_horizon=1):
    X, Y = [], []
    for i in range(lookback, len(df)-lookback):
        X.append(df[i-lookback : i, 0])
        Y.append(df[i : i + prediction_horizon, 0])
    return np.array(X), np.array(Y)

The problem however is that the test_predict has NaN values after the row 96. Any idea of why is this happening?

Topic lstm keras pandas python

Category Data Science

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.