Unexpected NaN values during training with TensorFlow 2.9.1 and Keras
I can't seem to get I'm currently building a neural network for a regression task using TensorFlow 2.9.1 and Keras, but I've encountered a perplexing scenario where my loss values become NaN during training... I’ve defined a simple model as follows: ```python from tensorflow import keras from tensorflow.keras import layers model = keras.Sequential([ layers.Dense(64, activation='relu', input_shape=(input_dim,)), layers.Dense(64, activation='relu'), layers.Dense(1) ]) model.compile(optimizer='adam', loss='mean_squared_error') ``` I'm using the Adam optimizer with default parameters. My dataset, which consists of normalized features, is loaded from a CSV file using pandas. The loss function returns NaN after just a few epochs, and I’m not sure if it’s an scenario with my data preprocessing or the model configuration. Here’s a snippet of how I preprocess my data: ```python import pandas as pd # Load data data = pd.read_csv('data.csv') X = data[['feature1', 'feature2']].values Y = data['target'].values # Normalize features X = (X - X.mean(axis=0)) / X.std(axis=0) Y = (Y - Y.mean()) / Y.std() ``` I’ve also tried reducing the learning rate to 0.0001, but the scenario continues. I’ve checked for any NaN or infinite values in `X` and `Y` before the training, and everything looks fine. I’m using a batch size of 32. The unexpected behavior occurs at approximately the 5th epoch, where I see the loss printout: ``` Epoch 5/50 1/1 [==============================] - 0s 15ms/step - loss: nan ``` Could this be related to exploding gradients, or is there something else I might be overlooking? Any suggestions on how to debug this or prevent NaN values during training would be greatly appreciated! My team is using Python for this web app.