Hi i'm a student and i'm working on a Federated Learning problem, but before doing that with the proper tools like OpenFL or Flower, I started a little experiment to try in local to train using this technique.

I managed to train multiple models using IID data, now I'm struggling with the local_update() function that should collect the models and then i need to take all the weights of these models and compute their mean. I read some documentation of Keras and Tensorflow that I'm using for my work, and i found some functions but i can't get it to work properly.

Currently this is my local_update() that's not working

    def local_update(self, models):

        weights = []
        #Take the weights of the models and compute the mean then return the weights to an updated model

        for model in models:
            for layer in model.layers:
                weights = layer.get_weights()
        #Compute the mean of weights  
        weights = np.mean(weights, axis=0)

        for layer in self.model.layers:

        return self.model

Thank you in advance for the help.

  • $\begingroup$ What do you mean by it is not working? post the error $\endgroup$
    – Oscar
    Apr 20, 2022 at 12:33

1 Answer 1


In your function, you get weights by each model layer but you always assign them to the same variable.

Hence at the end, you will have weights assigned to the average of the last layer weights.

here, you can find an example working and calculating the mean of weights.

  • $\begingroup$ Thank you a lot, i had a workflow a lot different from that of your, but it kinda worked. Now i tryied 5 iteration of federated learning with my data and the final global model had 0 accuracy. I suspect that my validation set is wrong or something in my code is bugged. $\endgroup$ Apr 22, 2022 at 15:21
  • $\begingroup$ unfortunately, currently the given link is broken $\endgroup$
    – nooshinha
    Dec 19, 2022 at 9:11
  • $\begingroup$ Thank you for pointing this out. I have just updated the link $\endgroup$
    – Oscar
    Dec 19, 2022 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.