Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Keras model at specific epochs

I am using Keras to do some training on my dataset and it is time consuming to keep running every time to locate the number of epochs needed to get the best results. I tried using callbacks to get the best model, but it just does not work and usually stops too early. Also, saving every N epochs is not an option for me.

What I am trying to do is save the model after some specific epochs are done. Let's say for example, after epoch = 150 is over, it will be saved as model.save(model_1.h5) and after epoch = 152, it will be saved as model.save(model_2.h5) etc... for few specific epochs.

Is there a way to implement this in Keras ? I already searched for a method but no luck so far.

Thank you for any help/suggestion.

like image 554
Wazaki Avatar asked Jan 23 '19 09:01

Wazaki


People also ask

How do you save a model for each epoch?

We can use the Keras callback keras. callbacks. ModelCheckpoint() to save the model at its best performing epoch.

How do you save weights in Keras after every epoch?

To save weights every epoch, you can use something known as callbacks in Keras. checkpoint = ModelCheckpoint(.....) , assign the argument 'period' as 1 which assigns the periodicity of epochs. This should do it.

How do I save a Keras model for later use?

Save Your Neural Network Model to JSON This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification. The weights are saved directly from the model using the save_weights() function and later loaded using the symmetrical load_weights() function.


1 Answers

checkpoint = keras.callbacks.ModelCheckpoint('model{epoch:08d}.h5', period=5) 
model.fit(X_train, Y_train, callbacks=[checkpoint])

Did you try checkpoint? period=5 means model is saved after 5 epoch

More details here

Hope this help :)

like image 143
Tony Avatar answered Oct 16 '22 23:10

Tony