Python - Returning Nan When Trying to Predict With Keras

Keras Model predicts NaN

This usually happens because of NaNs/infinity in your dataset. You should consider dropping such rows during pre-processing.

The below code will return True if all the values are finite.

df = df[np.isfinite(df).all(1)]

If it returns False you might have to drop NaN/infinity

# Replacing infinite with nan 
df.replace([np.inf, -np.inf], np.nan, inplace=True)

# Dropping all the rows with nan values
df.dropna(inplace=True)

# Printing df
df

Sometimes Changing the optimizer to RMSprop solves the issue

Python Keras Prediction returning nan

Not clear what is train_labels. If it's the same as labels then you'll need to have output of the last layer to be 21 and not 20, since in keras labels start from 0. Or you can redefine your labels to be from 0 to 19. Otherwise your code is ok and it's working on my pc. I've got 100% accuracy after ~1900 epochs

Keras model predict NaNs after save/load

Turns out this was because some of the values in the training dataset where nan.

As a result, the weights in some of the layers were also nan.

The surprising bit is that running model.predict() on GPU was perfectly fine, while on CPU it resulted in all nan predictions.

I was using the fitted model directly on GPU, and the saved model on CPU, hence I believe it had something to do with model saving, but not at all. Purely GPU versus CPU dichotomy.

I ended up cleaning the nan values from the training dataset and now the model is exempt from nan weights and runs fine both on CPU and GPU.



Related Topics



Leave a reply



Submit