'NaN for loss and measuring metrics - Keras

I am using Keras to implement neural network models to predict stock time series data. The code was fine from the tutorial, but loss and all metrics values turn to NaN when I use with my stock market data. I tried changing with many optimizers (SGD, RMSprop, Adam, Adadelta, Adagrad, Adamax, Nadam, Ftrl), adding L1/L2 in the kernel_regularizer, also adding dropout. But all doesn't help. Here is my code.

def compile_and_fit(model, window, patience=2):
    early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', 
                                                      patience=5, 
                                                      mode='min')

    model.compile(loss=tf.losses.MeanSquaredError(), 
                  optimizer=tf.optimizers.RMSprop(0.01), 
                  metrics=[
                           tf.metrics.MeanAbsoluteError(),
#                            tf.metrics.MeanAbsolutePercentageError(),
#                            tf.metrics.MeanSquaredError(),
#                            tf.metrics.RootMeanSquaredError(),
                          ])
    
    history = model.fit(window.train, 
                        epochs = EPOCH, 
                        validation_data=window.val, 
                        callbacks=[early_stopping])
    return history

NN1 = tf.keras.Sequential([tf.keras.layers.Dense(units=32,kernel_regularizer='l2', activation='relu'),
                           tf.keras.layers.Dropout(0.2),
                           tf.keras.layers.Dense(units=1)])
history = compile_and_fit(NN1, wide_step_model)

val_performance['NN1'] = NN1.evaluate(wide_step_model.val)
performance['NN1'] = NN1.evaluate(wide_step_model.test)

This is the result I have

Epoch 1/20
11/11 [==============================] - 0s 13ms/step - loss: nan - root_mean_squared_error: nan - val_loss: nan - val_root_mean_squared_error: nan
Epoch 2/20
11/11 [==============================] - 0s 6ms/step - loss: nan - root_mean_squared_error: nan - val_loss: nan - val_root_mean_squared_error: nan
Epoch 3/20
11/11 [==============================] - 0s 5ms/step - loss: nan - root_mean_squared_error: nan - val_loss: nan - val_root_mean_squared_error: nan
Epoch 4/20
11/11 [==============================] - 0s 6ms/step - loss: nan - root_mean_squared_error: nan - val_loss: nan - val_root_mean_squared_error: nan
Epoch 5/20
11/11 [==============================] - 0s 6ms/step - loss: nan - root_mean_squared_error: nan - val_loss: nan - val_root_mean_squared_error: nan
3/3 [==============================] - 0s 4ms/step - loss: nan - root_mean_squared_error: nan
1/1 [==============================] - 0s 43ms/step - loss: nan - root_mean_squared_error: nan

I'm actually referring to the original code from https://www.tensorflow.org/tutorials/structured_data/time_series

What should I do here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source