Search code examples
pythontensorflowmachine-learningkerasdeep-learning

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

Solution

  • 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()