我正在关注this blog,我无法实施保存检查点,因为它已在链接博客中使用。第23行使用:
filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
。
所以我尝试稍微调整代码以使其更具动态性:
filepath = '{0}/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'.format(directory)
。
我希望将给定体系结构的所有检查点存储在1个目录中,例如:./architecture1/checkpoints/
但是我收到以下错误:KeyError: 'epoch'
。我在这里做错了什么?
P.S。:filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5"
有效,但它将所有检查点保存在我不想要的1个目录中。
答案 0 :(得分:2)
如果您想使用format
,正确的方法是转义括号,如下所示:
filepath = '{0}/checkpoints/checkpoint-{{epoch:02d}}-{{val_loss:.2f}}.hdf5'.format(directory)
因此,如果directory = 'weights'
,则filepath
为'weights/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'
。
(注意directory
包含{}
)
答案 1 :(得分:1)
问题是您在format
符合条件的字符串上使用format
但只提供其中一个键 - 这会导致错误。
你在做什么
"{0} some text here {epoch:02d}".format("text")
这会导致错误,因为它会查找第二个密钥但无法找到它。
如果您希望您的代码是动态的,我会做的是:
"{0}".format(directory) + "/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5"