简体   繁体   中英

How to save weights of keras model for each epoch?

I want to save keras model and I want to save weights of each epoch to have best weights. How I do that?

Any help would be appreciated.

code :

def createModel():
    input_shape=(1, 22, 5, 3844)
    model = Sequential()
    #C1
    model.add(Conv3D(16, (22, 5, 5), strides=(1, 2, 2), padding='same',activation='relu',data_format= "channels_first", input_shape=input_shape))
    model.add(keras.layers.MaxPooling3D(pool_size=(1, 2, 2),data_format= "channels_first",  padding='same'))
    model.add(BatchNormalization())
    #C2
    model.add(Conv3D(32, (1, 3, 3), strides=(1, 1,1), padding='same',data_format= "channels_first",  activation='relu'))#incertezza se togliere padding
    model.add(keras.layers.MaxPooling3D(pool_size=(1,2, 2),data_format= "channels_first", ))
    model.add(BatchNormalization())

     #C3
    model.add(Conv3D(64, (1,3, 3), strides=(1, 1,1), padding='same',data_format= "channels_first",  activation='relu'))#incertezza se togliere padding
    model.add(keras.layers.MaxPooling3D(pool_size=(1,2, 2),data_format= "channels_first",padding='same' ))
    model.add(Dense(64, input_dim=64, kernel_regularizer=regularizers.l2(0.01), activity_regularizer=regularizers.l1(0.01)))
    model.add(BatchNormalization())

    model.add(Flatten())
    model.add(Dropout(0.5))
    model.add(Dense(256, activation='sigmoid'))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))

    opt_adam = keras.optimizers.Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
    model.compile(loss='categorical_crossentropy', optimizer=opt_adam, metrics=['accuracy'])

    return model

model.get_weights() will return a tensor as a numpy array. You can save those weights in a file with extension.npy using np.save().

To save weights every epoch, you can use something known as callbacks in Keras.

from keras.callbacks import ModelCheckpoint

before you do model.fit, define a checkpoint as below

checkpoint = ModelCheckpoint(.....) , assign the argument 'period' as 1 which assigns the periodicity of epochs. This should do it.

I am not sure it will work but you can try writing callback and inside callback you can save the weights.

Eg.

checkpoint = ModelCheckpoint("best_model.hdf5", monitor='loss', verbose=1,
    save_best_only=True, mode='auto', period=1)

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test),
          callbacks=[checkpoint])

source = https://medium.com/@italojs/saving-your-weights-for-each-epoch-keras-callbacks-b494d9648202

You should use model.get_weights() and LambdaCallback function together:

  1. model.get_weights() : Returns a list of all weight tensors in the model, as Numpy arrays.

     model = Sequential() weights = model.get_weights()
  2. LambdaCallback : This callback is constructed with anonymous functions that will be called at the appropriate time

    import json json_log = open('loss_log.json', mode='wt', buffering=1) json_logging_callback = LambdaCallback( on_epoch_end=lambda epoch, logs: json_log.write( json.dumps({'epoch': epoch, 'loss': logs['loss']}) + '\n'), on_train_end=lambda logs: json_log.close() ) model.fit(..., callbacks=[json_logging_callback])

When your code is considered, you should write callback function and add to your model :

import json
from keras.callbacks import LambdaCallback

json_log = open('loss_log.json', mode='wt', buffering=1)
json_logging_callback = LambdaCallback(
            on_epoch_end=lambda epoch, logs: json_log.write(
                json.dumps({'epoch': epoch, 
                            'loss': logs['loss'],
                            'weights': model.get_weights()}) + '\n'),
            on_train_end=lambda logs: json_log.close()
)

model.compile(loss='categorical_crossentropy',
              optimizer=opt_adam, 
              metrics=['accuracy'])

model.fit_generator(..., callbacks=[json_logging_callback])

This code write your all weights in all layers to json file. If you want to save weights in specific layer, just change the code with

model.layers[0].get_weights()

You can write a ModelCheckpoint callback using tf.keras.callbacks.ModelCheckpoint to save weights every epoch. If you are using recent Tensorflow like TF2.1 or later, then You need to use save_freq='epoch' to save weights every epoch instead of using period=1 as other answer mentioned. Please check entire example here

callback is as follows

checkpoint_path = "./training_2/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(
   checkpoint_path, verbose=1, save_weights_only=True,
   # Save weights, every epoch.
   save_freq='epoch')

calling the model training

# Create a basic model instance
model=create_model()
model.save_weights(checkpoint_path.format(epoch=0))
model.fit(x_train, y_train,
         epochs = 50, callbacks = [cp_callback],
         validation_data = (x_test,y_test),
         verbose=0)

Hope this helps. Thanks!

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