Keras save checkpoints

I am following on this blog and am having trouble implementing savings breakpoints as I did on the linked blog. On line 23, he used:

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

...

So, I tried to tweak the code a bit to be more dynamic:

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

...

Where I want to store all the breakpoints of a given architecture in 1 directory like: ./architecture1/checkpoints/

But I get the following error: KeyError: 'epoch'

. What am I doing wrong here?

PS: filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5"

works, but it keeps all breakpoints in 1 directory, which I don't want.

+3


source to share


3 answers


If you want to use format

, the correct way is to escape the parentheses, for example:

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

      



So, if directory = 'weights'

, it filepath

will be 'weights/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'

.

(Be careful if it directory

contains {}

)

+1


source


The problem is that you are using format

in a format

suitable string, but only supply one of the keys - and that throws an error.

What you do is

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

      



and this throws an error as it is looking for the second key and cannot find it.

If you want your code to be dynamic I would do the following:

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

      

+1


source


I found out that regular string concatenation works. This means it works:

filepath = directory + '/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'

I have no idea why it .format()

doesn't work and if anyone can develop I would be glad to hear the reason.

-1


source







All Articles