1

I'm currently learning about forecasting time series using a very simple dataset with 8 columns (All numbers) with a total of 795 samples for training and 89 for testing (No NaN values)

The data has been normalized using:

scaler = MinMaxScaler(feature_range=(0,1))
scaled_data = scaler.fit_transform(dataset_to_train)

Later, after separating the target from the features, I used Keras to create a LSTM (x_train is scaled_data without the target column):

lstm_net = Sequential()
lstm_net.add(LSTM(20, input_shape=(x_train.shape[1],1), return_sequences=True))
lstm_net.add(LSTM(10,return_sequences=False))
lstm_net.add(Dense(5))
lstm_net.add(Dense(1))

And for the compiling (Have tried other compilers like adam or rmsprop with no change at all):

lstm_net.compile(optimizer = 'sgd', loss = 'mean_squared_error', metrics=['accuracy'])

And for the fitting:

lstm_net.fit(x_train,y_train, epochs=50 , batch_size=1)

It shows this:

Epoch 30/50
795/795 [==============================] - 2s 2ms/step - loss: nan - accuracy: 0.0474

Which has very low accuracy and nan loss all the time.

To get the final predictions:

predictions = lstm_net.predict(x_test)
print(predictions)

And it prints a whole array of [nan]

Any advice on how can I try to solve this? Have tried everything I have found on other posts or forums and most people specify it might be due to a gradient problem, and even tried different scalers with no results so I'm lost.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • try predicting before fitting – Andrey Jan 30 '21 at 06:11
  • Is it not supposed to train it before using it? For some reason it works, but I still don't understand how or why (Thanks for the answer by the way) – Javier Romero. Jan 30 '21 at 15:26
  • yes, it should work but predicting wrong values. Are there nan values within predictions ? – Andrey Jan 30 '21 at 15:32
  • If I train it, only returns nan values. Otherwise returns numbers but all extremely similar – Javier Romero. Jan 30 '21 at 20:05
  • Can you post a sample of the data? This is most likely an issue with what is fed into the network – Gilthans Aug 15 '21 at 10:41
  • Following this [link](https://stackoverflow.com/questions/37232782/nan-loss-when-training-regression-network) as reference might helpful to resolve this issue or try by changing the optimizer from 'sgd' to 'adam'. –  Oct 12 '21 at 04:16
  • *@Javier Romero*: really, Adaptive Moment Estimation(Adam) will help as of your problem's description in comments – JeeyCi Jun 04 '22 at 15:57
  • BTW, if your model is not a classification - metric 'accuracy' is insufficient, use *metrics=keras.metrics.mean_absolute_error* for regression – JeeyCi Jun 04 '22 at 16:01

0 Answers0