The Context
I'm new to the whole concept of ANNs and have only dabbled in Python for a few weeks. However, I'm interested to use the practice to build experience and understanding of the subject.
I'm currently working on a separate 'prediction' python file for a multi-variate, multi-step LSTM Keras model. The original model works fine and provides accurate predictions when tested against the test set.
However, when loading the model into a separate 'prediction' python file to predict new data, the prediction returns an array of NaN values. The code in question is as below:
def forecast(model, input_seq, n_input):
data = np.array(input_seq)
input_x = data[-n_input:, 0]
input_x = input_x.reshape((1, len(input_x), 1))
yhat = model.predict(input_x, verbose=0)
yhat = yhat[0]
return yhat
lstm_model = load_model('lstm_model')
n_input = 365
data = pd.read_csv('data.csv', header=0, parse_dates=[0], index_col=0, squeeze=True, date_parser=parser)
data = data.head(365)
data = data.values
scaler, data_scaled = scale(data) # MinMaxScaler with range of 0-1
history = [x for x in data_scaled]
predictions = list()
for i in range(len(data_scaled)):
yhat_sequence = forecast(lstm_model, input_seq, n_input)
predictions.append(yhat_sequence)
history.append(data_scaled[i, :])
predictions = np.array(predictions)
The issue specifically occurs at:
yhat = model.predict(input_x, verbose=0)
where an output of nan values are given out as:
[[nan nan nan nan nan nan nan]]
while the output of the variable input_x gives an array of normalized input sequence values.
The LSTM model is built as such:
def build_model(train, n_input):
train_x, train_y = to_supervised(train, n_input)
verbose, epochs, batch_size = 0, 1, 1
n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]
model = Sequential()
model.add(LSTM(200, activation='relu', input_shape=(n_timesteps, n_features)))
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs))
model.compile(loss='mse', optimizer='adam')
model.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)
model.save('lstm_model')
return model
The Question
How can I solve the problem with model.predict? I.e. Does the issue lie with the loaded model or does it have something to do with how the separate 'prediction' file is coded?