简体   繁体   中英

Keras Weights Averaging

I have used two different datasets using Keras to train two instances of the same model and I have saved each weights set under HDF5 format.

How can I average the weights of these two sets (stored in files) in order to create one averaged weights set?

You can compute the average model like this :

saved_models = []
for i in range(num_models):
    model = define_model() #your model
    model.load_weights(model_name+"_"+str(i)+".hdf5") #load saved weights
    saved_models.append(model)

saved_weights = [model.get_weights() for model in saved_models]
mean_weights = list()
for weights_list_tuple in zip(*saved_weights): 
    mean_weights.append(
       np.array([np.array(w).mean(axis=0) for w in zip(*weights_list_tuple)]))
mean_model = define_model()
mean_model.set_weights(mean_weights) 

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