简体   繁体   中英

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.

Edit
In most cases it's enough to use name formatting suggested by @Toan Tran in his answer.

But if you need some sophisticated logic, you can use a callback , for example

import keras

class CustomSaver(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if epoch == 2:  # or save after some epoch, each k-th epoch etc.
            self.model.save("model_{}.hd5".format(epoch))

on_epoch_end is called at the end of each epoch; epoch is a number of epoch, latter argument is a logs (you can read about other callback methods in docs). Put the logic into this method (in example it's as simple as possible).

Create saver object and put it into fit method:

import keras
import numpy as np

inp = keras.layers.Input(shape=(10,))
dense = keras.layers.Dense(10, activation='relu')(inp)
out = keras.layers.Dense(1, activation='sigmoid')(dense)
model = keras.models.Model(inp, out)
model.compile(optimizer="adam", loss="binary_crossentropy",)

# Just a noise data for fast working example
X = np.random.normal(0, 1, (1000, 10))
y = np.random.randint(0, 2, 1000)

# create and use callback:
saver = CustomSaver()
model.fit(X, y, callbacks=[saver], epochs=5)

In the bash :

!ls
Out:
model_2.hd5                     

So, it works.

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 :)

Well, I can't comment on posts yet. So, I'm adding on to @Toan Tran 's answer. With the latest version of Keras, the argument period is deprecated. Instead, we can use save_freq .

In the following example, the model is saved after every epoch.

checkpoint = keras.callbacks.ModelCheckpoint(model_save_path+'/checkpoint_{epoch:02d}', save_freq='epoch')
H=model.fit(x=x_train, y=y_train,epochs=epoch_no,verbose=2, callbacks=[checkpoint])

You can find more details from keras documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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