17
$\begingroup$

Can someone please post a straightforward example of Keras using a callback to save a model after every epoch? I can find examples of saving weights, but I want to be able to save a completely functioning model after every training epoch.

$\endgroup$
23
$\begingroup$

Setting 'save_weights_only' to False in the Keras callback 'ModelCheckpoint' will save the full model; this example taken from the link above will save a full model every epoch, regardless of performance:

keras.callbacks.ModelCheckpoint(filepath, monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)

Some more examples are found here, including saving only improved models and loading the saved models.

| improve this answer | |
$\endgroup$
10
$\begingroup$

Make sure to include epoch variable in your filepath. Otherwise your saved model will be replaced after every epoch.

filepath = "saved-model-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=False, mode='max')

For more examples, check here.

| improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ Welcome to the site! And thanks, I appreciate that addition to the answer $\endgroup$ – I_Play_With_Data Feb 28 '19 at 13:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.