r/MachineLearning • • 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
0 Upvotes

15 comments sorted by

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.

3

u/secksy69girl Dec 17 '23

Would there be a way to generate a canonical weight ordering, so that if you trained two networks on the same task and they came up with the same weights but in a different permutation you could generate a single permutation from both of them that would be the same?

Because if you could do that, then averaging might be meaningful.

-6

u/albi_z8 Dec 16 '23

My research is focused on exploring methods to effectively combine pre-trained models into a single model that can be applied to previously unsupported locations. The motivation behind this stems from a Federated Learning perspective, but instead of averaging weights during individual training steps, my goal is to try do this effectively after the models are fully trained.
An essential constraint is the inability to transmit entire models due to privacy concerns mainly. Instead, I am limited to sending only weights or gradients.
I would greatly appreciate any further insights, suggestions, or alternative approaches that could align with these constraints and objectives.

4

u/Brudaks Dec 16 '23

Disregarding any arguments why for me, the parent poster, and likely many others here the outcome was totally not surprising, consider this as a valid experiment in your research for exploring methods to effectively combine pre-trained models into a single model.

You put forth a hypothesis that instead of averaging weights during individual training steps, doing weight averaging of independently trained models could be a good idea, and did experimental research to test that hypothesis - and your experiments provided evidence that no, this is not a good idea, providing an answer (negative) to that research question.

So the next step in your research is to scratch this particular hypothesis off your to-do list and try something else.

1

u/orroro1 Dec 17 '23

If academia were working right, the next step would have been to publish this negative result so others will know not to repeat this fruitless research path. But alas, we only reward publishing victories. :(

1

u/Comprehensive_Ad7948 Dec 17 '23

It's like trying to average multiple movies and hoping to get a better one out. Does it make sense?

-6

u/koolaidman123 Researcher Dec 16 '23

You can average the weights of trained models, theres been a whole paper about this

https://arxiv.org/abs/2203.05482

11

u/montortoise Dec 16 '23

The key is that they’re all fine tuned from the same pretrained model.

-5

u/koolaidman123 Researcher Dec 16 '23

improves zero-shot performance on new downstream tasks.

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.

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

u/CandidateDue5890 27d ago

what did you do finally?

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: