What is the difference between these two ways of saving keras machine learning model weights?

你说的曾经没有我的故事 提交于 2020-03-18 04:48:13

问题


I saw two ways of saving the weights of a keras model.

First way;

checkpointer = ModelCheckpoint(filepath="weights.hdf5", verbose=1, save_best_only=True)
model.fit(x_train, y_train,
                    nb_epoch=number_of_epoch,
                    batch_size=128,
                    verbose=1,
                    validation_data=(x_test, y_test),
                    callbacks=[reduce_lr, checkpointer],
                    shuffle=True)

Second way;

model.save_weights("model_weights.h5")

What is the difference between the two ways? Any difference in prediction performance between loading weights.hdf5 and model_weights.h5?


回答1:


No, there is no difference performance-wise. These are just two different ways of how and especially when the model shall be saved. Using model.save_weights requires to especially call this function whenever you want to save the model, e.g. after the training or parts of the training are done. Using ModelCheckpoint is much more convenient if you are still developing a model. Using this way, keras can save a checkpoint of your model after each training epoch, so that you can restore the different models; or you can set save_best_only=True so that keras will overwrite the latest checkpoint only if the performance has improved, so that you end with the best performing model.

To summarize it: these are just two different ways of doing two different things. It depends on your use case and needs, what's the best.



来源:https://stackoverflow.com/questions/51923048/what-is-the-difference-between-these-two-ways-of-saving-keras-machine-learning-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!