如何为每个时期保存keras模型的权重? [英] How to save weights of keras model for each epoch?
问题描述
我想保存keras模型,并且想保存每个历元的权重以具有最佳权重.我该怎么做?
I want to save keras model and I want to save weights of each epoch to have best weights. How I do that?
任何帮助将不胜感激.
代码:
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()和LambdaCallback函数:
You should use model.get_weights() and LambdaCallback function together:
-
model.get_weights():以Numpy数组的形式返回模型中所有权重张量的列表.
model.get_weights(): Returns a list of all weight tensors in the model, as Numpy arrays.
model = Sequential()
weights = model.get_weights()
LambdaCallback :此回调由匿名函数构成,将在适当的时间调用
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])
考虑代码时,您应该编写 callback 函数并将其添加到您的 model 中:
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])
此代码将所有图层的所有权重写入json文件.如果要在特定图层中保存权重,只需使用
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()
这篇关于如何为每个时期保存keras模型的权重?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!