Request for clarification in one argument in keras callback function

Hello all,

I am using Keras on R and I wish to save weights after each epochs for later diagnosis.

I am unable to understand the argument filepath from the documentation

According to the documentation - filepath can contain named formatting options, which will be filled the value of epoch and keys in logs (passed in on_epoch_end). For example: if filepath is weights.{epoch:02d}-{val_loss:.2f}.hdf5, then the model checkpoints will be saved with the epoch number and the validation loss in the filename.

Can someone please explain the expression - weights.{epoch:02d}-{val_loss:.2f}.hdf5 .

a. What are the meaning of all the special characters here?
b. What does :02d and :.2f mean?
c. In R, we use $ or [ ] or [[ ]] or ( ) to extract elements, what does the . (dot) signify?


Hi @SiD,

a+b: The :02d and :.2f are special pieces of code that indicate how to format those values as strings when interpolating them into the file name. In other words, when saving the model to an HDF5 files, the epoch value should be presented as an integer (d) and padded with a zero if less than 2 digits (02). For val_loss, it is a float/numeric (f) value, and should have 2 decimal precision (.2).

As an example, for epoch 5 and val_loss of 1.23456, the file would be named:

c: I believe the . between weights and epoch is literal. So literally put a . between these words in the file name. The . after : are part of the special formatting syntax described above.

You can learn a lot more about this type of special string formatting by checking out the help documentation for ?sprintf (see Details section). Hope this was helpful.

sprintf("%02d", 5) # Epoch 5
#> [1] "05"
sprintf("%.2f", 1.23456) # Loss of 1.23456
#> [1] "1.23"
1 Like

Thank you @mattwarkentin, it was helpful

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.