save keras model every epoch code example
Example 1: export keras model at specific epoch
import keras
class CustomSaver(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if epoch == 2:
self.model.save("model_{}.hd5".format(epoch))
Example 2: export keras model at specific epoch
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",)
X = np.random.normal(0, 1, (1000, 10))
y = np.random.randint(0, 2, 1000)
saver = CustomSaver()
model.fit(X, y, callbacks=[saver], epochs=5)