export keras model at specific epoch

Solutions on MaxInterview for export keras model at specific epoch by the best coders in the world

showing results for - "export keras model at specific epoch"
Darrin
08 Aug 2018
1import keras
2
3class CustomSaver(keras.callbacks.Callback):
4    def on_epoch_end(self, epoch, logs={}):
5        if epoch == 2:  # or save after some epoch, each k-th epoch etc.
6            self.model.save("model_{}.hd5".format(epoch))
Cael
13 Nov 2020
1import keras
2import numpy as np
3
4inp = keras.layers.Input(shape=(10,))
5dense = keras.layers.Dense(10, activation='relu')(inp)
6out = keras.layers.Dense(1, activation='sigmoid')(dense)
7model = keras.models.Model(inp, out)
8model.compile(optimizer="adam", loss="binary_crossentropy",)
9
10# Just a noise data for fast working example
11X = np.random.normal(0, 1, (1000, 10))
12y = np.random.randint(0, 2, 1000)
13
14# create and use callback:
15saver = CustomSaver()
16model.fit(X, y, callbacks=[saver], epochs=5)