I everybody,
I want to average the weights of three identical model trained with different training sets and that create a new model with averagerd weights coming from these three models.
My models have the same structure.
I’ve tried with this code but seems the weights of the new model that i want create remanins equal to the last model that i fit.
What i should do in order to make it works?
Thanks
def load_all_models():
all_models = list()
for i in range(3):
filename = 'Conv_autoencoder_'+str(n)+'_layer_.h5'
model = load_model(filename)
all_models.append(model)
print('Loaded model %s' % filename)
return all_models
# create a model from the weights of multiple models
def model_weight_ensemble(members, weights):
n_layers = len(members[0].get_weights())
avg_model_weights = list()
for layer in range(n_layers):
layer_weights = np.array([model.get_weights()[layer] for model in members])
# weighted average of weights for this layer
avg_layer_weights = np.average(layer_weights, axis=0, weights=weights)
# store average layer weights
avg_model_weights.append(avg_layer_weights)
# create a new model with the same structure
model = clone_model(members[1])
# set the weights in the new
model.set_weights(avg_model_weights)
model.compile(optimizer='SGD', loss='mean_squared_error')
return model
members = load_all_models()
print('Loaded %d models' % len(members))
n_models = len(members)
weights = [1 for i in range(1, n_models+1)]
autoencoder_global = model_weight_ensemble(members,weights)
print(autoencoder_global.get_weights()[12])
print('------------------------------------------------------------------')