简体   繁体   English

Keras保存检查站

[英]Keras save checkpoints

I am following this blog and I am having trouble to implement saving checkpoints as it's used in linked blog. 我正在关注这个博客 ,我很难实现保存检查点,因为它在链接博客中使用。 At line 23 it used: 第23行使用:

filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5" . filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"

So I tried to tweak the code a little bit to be more dynamic: 所以我尝试稍微调整代码以使其更具动态性:

filepath = '{0}/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'.format(directory) . filepath = '{0}/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'.format(directory)

Where I want to store all checkpoints of given architecture in 1 directory, eg: ./architecture1/checkpoints/ 我想在1个目录中存储给定体系结构的所有检查点,例如: ./architecture1/checkpoints/

But I get the following error: KeyError: 'epoch' . 但是我收到以下错误: KeyError: 'epoch' What am I doing wrong here? 我在这做错了什么?

PS: filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5" works, but it saves all checkpoints in 1 directory which I don't want. PS: filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5" checkpoint/checkpoint- filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5" : filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5" : filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5"有效,但它将所有检查点保存在我不想要的1个目录中。

If you want to use format , the proper way is to escape the brackets like this: 如果你想使用format ,正确的方法是逃避括号,如下所示:

filepath = '{0}/checkpoints/checkpoint-{{epoch:02d}}-{{val_loss:.2f}}.hdf5'.format(directory)

So if directory = 'weights' , filepath would be 'weights/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5' . 因此,如果directory = 'weights'filepath将是'weights/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'

(Take care if directory contains {} ) (如果directory包含{}请注意)

The problem is that you are using format on a format eligible string but supply only one of the keys - and that causes an error. 问题是您在符合format字符串上使用format但只提供其中一个键 - 这会导致错误。

What you are doing is 你在做什么

"{0} some text here {epoch:02d}".format("text")

and that is causing an error, because it looks for the second key and can't find it. 这导致错误,因为它查找第二个键,但找不到它。

If you want your code to be dynamic, what I would do is: 如果您希望您的代码是动态的,我会做的是:

"{0}".format(directory) + "/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM