我在学习Keras中的模型保存,发现我的模型检查点对象似乎没有创建指定的目录。以下是代码:
from tensorflow.keras.callbacks import ModelCheckpointcheckpoint_5000_path = \
'model_checkpoints_5000/checkpoint_{epoch:02d}_{batch:04d}'checkpoint_5000 = ModelCheckpoint(filepath=checkpoint_5000_path,
save_freq=5000,
save_weights_only=True,
verbose=2)model = get_new_model()model.fit(x_train,y_train,epochs=10,validation_split=0.15,callbacks=[checkpoint_5000],verbose=2)
当我尝试访问该目录时,
! ls -lh model_checkpoints_5000
出现了以下错误:
ls: cannot access 'model_checkpoints_5000': No such file or directory
这可能是什么原因呢?顺便提一下,我并不是在本地机器上操作的,而是在使用Google Colab。
回答:
Keras中的ModelCheckpoint
不会创建任何目录。你需要在训练模型之前创建model_checkpoints_5000
文件夹,否则在完成一个epoch后保存模型时会失败,并返回类似于以下的错误:
tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a directory: model_checkpoints_5000/XXX; No such file or directory