checkpoint_save_path = "./checkpoint/spot.ckpt"
if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(
                                            monitor='val_loss',
                                            filepath=checkpoint_save_path,
                                                 save_weights_only=True,
                                                 save_best_only=True
                                            )
history = model.fit(x_train,y_train,batch_size=32,epochs=5,validation_data=(x_test,y_test),validation_freq=2,callbacks=[cp_callback])
# for key in history.history:
#     print(key)                                      

执行结果:

出现这种结果可能是monitor的值不对,此时可以执行上面注释的两行代码,查看key的值,这里是sparse_categorical_accuracy,val_sparse_categorical_accuracy,loss, val_loss四种指标,所以这里的val_loss是没有问题的

还有可能是没有验证集数据,再仔细看执行结果,只有1,3,5次迭代时才有警告,2,4次迭代有这四个参数,那么就可能是validation_freq=2的问题,将2改成1后,果然解决了这一问题。

更多推荐

解决Warning: Can save best model only with val_loss available, skipping问题