How to save local models?

Hello. I have studied how to save global model, but I also need to save/modify local models. After a client finishing its local epochs, how do I read/modify this model before it is aggregated? and I need to know ‘server round’ and ‘client id number’

1 Like

Hi @john678,

Thank you for your question. You could adjust a model before it is aggregated:

On the client: In a Client/NumPyClient, the fit method does three things: it deserialises the server‑sent parameters, updates the local model, trains for some epochs and then serialises the updated weights to send back to the server. You can insert your own logic here. For example, in a PyTorch client you could call torch.save(model.state_dict(), …) after train(…) to persist the local model or modify ndarrays_updated before converting them back to a Parameters object.

class MyClient(fl.client.NumPyClient):
    def fit(self, parameters, config):
        # update local model
        ndarrays_original = fl.common.parameters_to_ndarrays(parameters)
        set_parameters(self.net, ndarrays_original)
        train(self.net, self.trainloader, epochs=config["local_epochs"])
        # get updated weights
        ndarrays_updated = get_parameters(self.net)

        # e.g. save a PyTorch checkpoint
        torch.save(self.net.state_dict(), f"client_{self.cid}_round_{config['server_round']}.pth")
        # or modify the update before sending it back
        # ndarrays_updated = clip_weights(ndarrays_updated)

        # send the (possibly modified) update back
        return ndarrays_updated, len(self.trainloader), {}

Hope this helps, I think it can be done on the server as well, just gave some initial thoughts.

Thanks this is helpful.
For others who has the same question, please check Sending/receiving arbitrary values to/from clients from Use a federated learning strategy - Flower Framework

and server can receive client’s values by handle_fit_metrics(), please study Sending ClientApp Metrics - 2025 Tutorial: Federated AI Simulations with Flower - YouTube

1 Like

Ok I have studied that server should read client’s returned value in fit_metrics_aggregation_fn, but it is not told explicitly in tutorial Use a federated learning strategy - Flower Framework
I think it’s better to tell people in the tutorial. Hope this is useful for others

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.