Home > Software design >  Keras model predict has nan loss and predict nan values
Keras model predict has nan loss and predict nan values

Time:10-09

I faced the same issue as this question: enter image description here

Predict the training dataset final_model.predict(train_imgs_fea) gets this:

enter image description here

Note that my train_imgs_fea has shape (7500, 16, 16, 512)

I have tried two things as suggested in Keras Model predicts NaN:

  1. Change optimiser to RMSprop -> result in the same scenario
  2. Check whether there are infinite values in train_imgs_fea by: False in np.isfinite(train_imgs_fea), which returns False

Any helps are appreciated!

CodePudding user response:

try this

class_count=10 # number of classes
img_shape=(16,16,512)
base_model=tf.keras.applications.VGG19(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max') 
x=base_model.output
x=keras.layers.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001 )(x)
x = Dense(256, kernel_regularizer = regularizers.l2(l = 0.016),activity_regularizer=regularizers.l1(0.006),
                bias_regularizer=regularizers.l1(0.006) ,activation='relu')(x)
x=Dropout(rate=.45, seed=123)(x)        
output=Dense(class_count, activation='softmax')(x)
final_model=Model(inputs=base_model.input, outputs=output)
final_model.compile(Adamax(lr=.001), loss=keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) 

You are using SparseCategoricalCrossentropy so your labels should be integers.

CodePudding user response:

I tried to close my jupyter notebook and restart my kernel, then it magically worked.

  • Related