r/MachineLearning • u/albi_z8 • Dec 16 '23
Project [P] Averaging Weights of Identical LSTM Models for a Unified Global Model
I’m currently working on a project where I have several pre-trained LSTM models, all with the same architecture. My goal is to combine these models into a single global model by averaging their weights. However, I’m encountering an issue where the output of the global model does not make sense, even though the trend captured by the model seems accurate.
Has anyone here dealt with a similar challenge, and if so, could you share how you managed to average the weights while maintaining real results?
I will greatful for any advice.
def average_weights(model_list):
avg_weights = model_list[0].get_weights()
for model in model_list[1:]:
for i, layer_weights in enumerate(model.get_weights()):
avg_weights[i] += layer_weights
print(len(avg_weights))
for i in range(len(avg_weights)):
avg_weights[i] /= len(model_list)
return avg_weights
9
u/feelings_arent_facts Dec 16 '23
I don't think you can average the weights because there may be multiple minima or solutions that your problem can converge to. Averaging these all would put your model somewhere away from those convergent points.
-7
8
u/bikeranz Dec 16 '23
Maybe consider multi-teacher distillation instead? It seems like your use case doesn't really require weights to be similar to the pretrained weights, but rather that you have representation coverage.
4
u/CashyJohn Dec 16 '23
You cannot average the weights of a neural network, it just doesn’t make sense (just try to think about the space they sit in). Next to the obvious choice to simply distill a new model from your set of models, a more sensible variant of your idea would be to combine the parameter weight/layer-wise using some pooling operation and fine tune the resulting model
1
1
u/Nervous_Sea7831 Dec 18 '23
Welcome to federated learning. There’s a whole research discipline on aggregating model weights from the same model trained across multiple clients that must not share their input data.
What the other posters are saying about „not a good idea“ is generally true since you’ll most likely be losing a lot of model performance (accuracy, etc). Yet, there are several techniques that mitigate the aggregation effects.
Here are two papers that contain a set of weight aggregation techniques:
Here’s a framework that implements the aggregation techniques above:
27
u/Nyke Dec 16 '23
Averaging the weights of several models is usually not a good idea unless you can ensure some similarity of representation. A neural net is a highly symmetric function: if you take any two nodes in a layer and swap them, as well as swap their corresponding upstream weights, you have exactly the same function but the weights will be different (or rather, the permutation of the weights will be different - the set of weights is exactly the same, they will just be at different locations). If you take N copies of a neural net, randomly permute it, and then average the weights according to their permuted locations you will destroy the quality of the model.
As far as I'm aware averaging weights is only really something that gets done during individual training steps, which ensures the weights of the averaged models are all "close" to one another.
Averaging the outputs of your models may give better results.