I faced the same issue as this question:
Predict the training dataset final_model.predict(train_imgs_fea)
gets this:
Note that my train_imgs_fea
has shape (7500, 16, 16, 512)
I have tried two things as suggested in Keras Model predicts NaN:
- Change optimiser to RMSprop -> result in the same scenario
- Check whether there are infinite values in
train_imgs_fea
by:False in np.isfinite(train_imgs_fea)
, which returnsFalse
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.