我正在使用keras训练一个CNN模型。
在每个epoch结束后,如果验证准确率有所提高,我会保存权重作为检查点。
from keras.callbacks import ModelCheckpointcheckpoint = ModelCheckpoint(checkpoint_path, monitor='val_accuracy', mode='max', save_best_only=True, verbose=1)callbacks = [checkpoint]#load checkpoints if existingimport osnum_of_epochs = 65epochs_done = 0if(os.path.exists(checkpoint_path)): model.load_weights(checkpoint_path) num_of_epochs = num_of_epochs - epochs_done print('checkpoints loaded')
当我停止后重新开始训练时,我的第一个epoch的输出看起来是这样的。
Epoch 1/65425/425 [==============================] - 224s 526ms/step - loss: 2.1739 - accuracy: 0.2939 - val_loss: 2.1655 - val_accuracy: 0.2985Epoch 00001: val_accuracy improved from -inf to 0.29846, saving model to checkpoints-finetuning.hdf5
我注意到每次重新开始训练时,第一个epoch都会发生这种情况。为什么会这样?每次我重新开始时,我的检查点文件是否会被更差的准确度权重覆盖?
回答:
这是因为每次运行脚本时回调实例都会被重新创建;它不会与模型一起保存。因此,第一个epoch总是从默认值开始,根据这里来看,默认值是np.Inf
或-np.Inf
。