简体   繁体   中英

Keras Model predicts NaN

I am trying to train a model for my binary classification problem.

My data has 91 columns and 50 thousand rows. One of the columns is my binary target variable and all the others are also numeric. Here is how I tried to train;

model = Sequential()
model.add(Dense(20, input_dim=90, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(20, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train_scaled, y_train, epochs=20)

After a fast training, I tried to predict with my test set and saw that all predictions are NaN

+ model.predict(x_test_scaled)

- array([[nan],
       [nan],
       [nan],
       ...,
       [nan],
       [nan],
       [nan]], dtype=float32)

By the way, I also didn't see loss score during training. There was also written 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM