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
How to Open Different Urls At the Same Time by Using Python Selenium
How to Check Whether All Elements of Array Are in Between Two Values
Hiding Axis Text in Matplotlib Plots
Regex to Match Digits and At Most One Space Between Them
How to Print Colored Text to the Terminal
How to Print Just the First Letters of Each Word
Python: How to Calculate the Average Word Length in a Sentence Using the .Split Command
Python Multiprocessing Pool Hangs At Join
Windowserror: [Error 126] the Specified Module Could Not Be Found
A Better Way Than Looping and Calling Functions That Loop and Call Another Functions
Is There a Short-Hand for Nth Root of X in Python
Python Searching for Partial Matches in a List
How to Get Max() to Return Variable Names Instead of Values in Python
Pandas, Remove Everything After Last '_'
Split a Large Json File into Multiple Smaller Files
Display Multiple Images in One Ipython Notebook Cell
Best Practice to Run Multiple Spark Instance At a Time in Same Jvm