首页 > 解决方案 > 模型检查点不创建目录

问题描述

我正在学习如何在 Keras 中保存模型,看起来我的模型检查点对象没有创建指定的目录。这是代码:

from tensorflow.keras.callbacks import ModelCheckpoint
checkpoint_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

这可能是什么原因?顺便说一句,我没有在我的本地机器上这样做。我使用谷歌 Colab。

标签: pythonmachine-learningkeras

解决方案


ModelCheckpointkeras 中不会创建任何目录。您必须model_checkpoints_5000在训练模型之前创建文件夹,否则在完成一个 epoch 后将无法保存模型,返回如下错误:

tensorflow.python.framework.errors_impl.NotFoundError: Failed to create a directory: model_checkpoints_5000/XXX; No such file or directory

推荐阅读