Written and maintained by CASRAI Editorial Board
Published
On this page
FedAvg, short for Federated Averaging, is the training algorithm in which many clients each train a copy of a shared model on their own data for several local steps, and a server then replaces the global model with the average of the returned models, weighted by how much data each client holds. McMahan et al. introduced it in 2016 as a way to learn deep networks from decentralized data while cutting the number of communication rounds.
What FedAvg is used for in research
FedAvg is the baseline that most federated learning work starts from. Researchers use it when the training data cannot be pooled in one place, for example because each site holds records it may not move, and the aim is still a single shared model. The original paper describes the setting as training on data that stays on the client, with a server that only coordinates rounds, and it reports that the method was robust to unbalanced and non-IID data distributions and needed 10 to 100 times fewer communication rounds than a synchronized stochastic gradient descent baseline across five model architectures and four datasets.
For a health-data application of the same idea, see the guide on federated learning in healthcare. For a decentralised variant that removes the central server, see swarm learning.
The algorithm in one round
Algorithm 1 in the paper (revision v4, 26 January 2023) uses four symbols: K is the number of clients, C is the fraction of clients selected each round, B is the local minibatch size, E is the number of local epochs and η is the learning rate. One round has these steps.
- The server computes m = max(C·K, 1) and picks a random set St of m clients.
- The server sends the current global model wt to every selected client.
- Each selected client splits its local data into batches of size B and, for E local epochs, takes a gradient step w ← w − η∇ℓ(w; b) on every batch, then returns its model.
- The server sets the new global model to the average of the returned models, wt+1 = Σ (nk / mt) wkt+1, where nk is the number of examples on client k and mt is the sum of nk over the selected clients.
The paper carries an erratum on step 4: earlier versions of the pseudocode summed over all K clients, and the corrected version sums over the clients selected in that round only. An implementation written from an older copy of the pseudocode can therefore normalise by the wrong total.
FedSGD is the special case
The paper’s baseline, FederatedSGD (FedSGD), is FedAvg with B = ∞ and E = 1: each client computes one gradient over all of its local data and the server applies the weighted result. With C = 1 the selected set is every client and the global step is a full-batch, non-stochastic gradient descent step. The paper’s reason for selecting only a fraction of clients is efficiency, because its experiments showed diminishing returns for adding more clients beyond a certain point.
How to install or access FedAvg
FedAvg is not a standalone program. It is implemented as a strategy or learning process in federated learning frameworks. Two with official documentation are Flower and TensorFlow Federated (TFF).
Flower
The Flower installation page for the v1.43.x documentation states that Flower requires at least Python 3.11. The stable release installs with pip:
python -m pip install flwr
For the Simulation Runtime, which runs many virtual clients on one machine, install the simulation extra:
python -m pip install "flwr[simulation]"
The page also gives a conda route (add the conda-forge channel with strict priority, then conda install flwr) and a check that the install worked:
flwr --version
TensorFlow Federated
The TFF install page offers three routes: run the tutorials in a browser through Google Colaboratory with no installation, install the package with pip, or build from source. The pip route creates a virtual environment and installs the released package:
python3 -m venv "venv" source "venv/bin/activate" pip install --upgrade "pip" pip install --upgrade tensorflow-federated
The page then gives a one-line test that imports tensorflow_federated and runs a small computation returning “Hello World”.
How to run FedAvg step by step
Run it with Flower
- Install Flower as above and confirm it with
flwr --version. - Import the strategy from
flwr.serverapp.strategy. - Create the strategy. Every argument has a default, so
FedAvg()is valid; the settings that matter most for a first run arefraction_train,min_train_nodes,min_available_nodesandweighted_by_key. - Call the strategy’s
startmethod with thegridthat carries messages between the server and clients, anArrayRecordholding the initial model parameters (initial_arrays) and the number of rounds (num_rounds, default 3).
from flwr.serverapp.strategy import FedAvg strategy = FedAvg(fraction_train=0.5, min_train_nodes=2, weighted_by_key="num-examples") result = strategy.start(grid=grid, initial_arrays=initial_arrays, num_rounds=5)
The local epochs E, the batch size B and the learning rate η do not appear in the strategy’s signature. In Flower they belong to the client-side training code, which receives the global model, trains and returns the result with the key named in weighted_by_key so the server can weight it.
Run it with TensorFlow Federated
- Install the package as above.
- Define
model_fn, a function with no arguments that returns a newtff.learning.models.VariableModel(or aFunctionalModel) every time it is called. TFF’s documentation says the function must not capture TensorFlow tensors or variables, and that returning one pre-built model on each call produces an error. - Choose
client_optimizer_fn, atff.learning.optimizers.Optimizerthat carries the client learning rate. - Build the process with
tff.learning.algorithms.build_weighted_fed_avg(model_fn, client_optimizer_fn). It returns aLearningProcess. - Call the process’s
initializemethod to get the initial server state, then callnextwith that state and the list of client datasets once per round. Each call broadcasts the server model, trains on each client withclient_optimizer_fn, computes each client’s difference from the model it started with, aggregates those differences with a weighted aggregation and applies the result with the server optimizer. - Use
get_model_weightson a state to extract the model weights, for evaluation or saving.
Parameter cross-walk: paper, Flower and TFF
The three vocabularies describe the same knobs differently. This table maps the paper’s symbols to what the two documentation pages expose.
| Paper symbol | Meaning in the paper | Flower FedAvg strategy | TFF build_weighted_fed_avg |
|---|---|---|---|
| C | Fraction of clients selected each round; m = max(C·K, 1) | fraction_train (default 1.0); min_train_nodes (default 2) still applies if it exceeds the fraction |
Not an argument of this function; client selection is not part of its signature |
| nk weighting | Each client’s model is weighted by its number of examples | weighted_by_key, default “num-examples” |
client_weighting, default weighting by number of examples |
| η | Client learning rate | Set in client-side code | client_optimizer_fn |
| E, B | Local epochs and minibatch size | Not in the strategy signature; set in client-side code | Not in the function signature |
| Server step | New model = weighted average of returned models | Weighted average of the replies’ ArrayRecords |
server_optimizer_fn; the default is build_sgdm with learning rate 1.0, which adds the averaged difference to the server model |
The last row is the part that is easy to miss. TFF’s documentation states that its default server optimizer corresponds to the original FedAvg of McMahan et al., and that a generalized FedAvg with different server learning rates or optimizers is described by Reddi et al. (2021). Changing server_optimizer_fn therefore moves you away from the algorithm in the original paper even though the function is still called build_weighted_fed_avg.
How to read the output
Federated experiments are usually compared by how many communication rounds a method needs to reach a target accuracy, not by wall-clock time on one machine. That is how the paper reports its results, and the data table below reproduces the Shakespeare LSTM block of the paper’s Table 2 with one derived column. The paper fixed C = 0.1 for this table and describes u = E·n/(K·B) as the expected number of local updates per client per round. “IID” means the data was shuffled across clients; in the paper’s Shakespeare experiments the non-IID partition is by play and role.
Three readings follow from the table. First, more local computation per round cuts rounds: for the paper’s 54% accuracy target, FedSGD needed 2,488 rounds on the IID partition and 3,906 on the non-IID partition, while FedAvg with E = 5 and B = 10 needed 192 and 41. Second, the worked arithmetic of the headline figure is 3,906 ÷ 41 = 95.3, the 95.3× the paper lists. Third, the non-IID column is not simply worse: for five of the six configurations here the non-IID run needed fewer rounds than the IID run, which the derived ratio column shows. That is a result for this one task and target, so do not generalise it to other datasets or models.
In your own runs, read the per-round metrics your framework reports. In Flower, training and evaluation metrics from the replies are aggregated with a weighted average by default, using train_metrics_aggr_fn and evaluate_metrics_aggr_fn if you replace them, and an optional evaluate_fn gives a centralized evaluation before the first round and after each round. In TFF, the output of each next call contains the updated server state together with aggregated metrics, including client training metrics.
Troubleshooting FedAvg
- The run never starts a round in Flower. The defaults of
min_train_nodes,min_evaluate_nodesandmin_available_nodesare all 2. Check them against the number of nodes you actually connect before lowering anything else. - Clients are slow or drop out.
startwaits up totimeoutseconds for node responses (default 3600), and replies from unavailable nodes are returned as error replies. Ashould_stopcallback can end training early. - TFF raises an error when building the process. The documentation says a
model_fnthat returns the same pre-built model on every call errors, and that aTypeErroris raised when arguments are not of the documented types. Build the model from scratch insidemodel_fn. - Your average looks wrong against the paper. Compare the normalisation with the corrected Algorithm 1: weights are nk over the sum for the selected clients, not over all K.
- Results do not match a baseline from the paper. The paper notes that SGD is sensitive to the learning rate and that its reported numbers come from a grid of 11 to 13 learning-rate values with the best value in the middle of the grid. Tune η before comparing round counts.
- Flower will not install. Check the Python version first; the documentation requires at least 3.11.
What FedAvg does not do
Algorithm 1 contains no noise, encryption or secure aggregation step: each selected client returns its model parameters and the server averages them. Keeping raw data on the client is therefore not the same as a privacy guarantee about the updates, and the weighting by nk means a client’s reported example count directly sets its influence on the average. If you need stronger protection, the related guides cover secure multiparty computation and confidential computing, and the risk of a client submitting harmful updates is the subject of the guide on data poisoning.
Free alternatives to FedAvg
Within these two frameworks the practical alternatives are other settings or algorithms in the same code base. The table lists what the sources above document, not a ranking.
| Option | What it is | Where it is documented |
|---|---|---|
| FedSGD | FedAvg with B = ∞ and E = 1; one full-data gradient per client per round | McMahan et al. 2017, arXiv:1602.05629 |
| Flower FedAvg strategy | Strategy class based on the paper; weighted averaging by weighted_by_key |
Flower documentation, flwr.serverapp.strategy.FedAvg |
| TFF build_weighted_fed_avg with the default server optimizer | Learning process that recovers the original FedAvg | TensorFlow Federated API documentation |
| TFF build_weighted_fed_avg with a different server optimizer | Generalized FedAvg with server-side learning rates or optimizers | TFF documentation; Reddi et al. 2021, arXiv:2003.00295 |
The TFF source file shown on its documentation page carries an Apache License, Version 2.0 header. Check each framework’s own licence page before reuse.
Sources
- arXiv: McMahan et al., Communication-Efficient Learning of Deep Networks from Decentralized Data (v4), checked 2026-11-17
- Flower documentation: FedAvg strategy (flwr.serverapp.strategy), checked 2026-11-17
- Flower documentation: Install Flower, checked 2026-11-17
- TensorFlow Federated: tff.learning.algorithms.build_weighted_fed_avg, checked 2026-11-17
- TensorFlow Federated: Install TensorFlow Federated, checked 2026-11-17
- arXiv: Reddi et al., Adaptive Federated Optimization, checked 2026-11-17
Data: Rounds to 54% accuracy on the Shakespeare LSTM: FedSGD vs FedAvg configurations
Communication rounds needed to reach 54% test accuracy with the Shakespeare LSTM, for FedSGD (E=1, B=infinity) and five FedAvg settings, on IID and by-play-and-role non-IID partitions with C=0.1. Rounds and speedups are transcribed from Table 2 of McMahan et al. (2017); the final column, non-IID rounds divided by IID rounds, is computed here.
Period covered: Paper v4, 26 January 2023.
| Configuration | Local epochs E | Minibatch size B | Updates per round u | IID rounds (rounds) | IID speedup vs FedSGD (x) | Non-IID rounds (rounds) | Non-IID speedup vs FedSGD (x) | Non-IID to IID rounds ratio |
|---|---|---|---|---|---|---|---|---|
| FedSGD (E=1, B=∞) | 1 | ∞ | 1 | 2488 | baseline | 3906 | baseline | 1.57 |
| FedAvg (E=1, B=50) | 1 | 50 | 1.5 | 1635 | 1.5 | 549 | 7.1 | 0.34 |
| FedAvg (E=5, B=∞) | 5 | ∞ | 5 | 613 | 4.1 | 597 | 6.5 | 0.97 |
| FedAvg (E=1, B=10) | 1 | 10 | 7.4 | 460 | 5.4 | 164 | 23.8 | 0.36 |
| FedAvg (E=5, B=50) | 5 | 50 | 7.4 | 401 | 6.2 | 152 | 25.7 | 0.38 |
| FedAvg (E=5, B=10) | 5 | 10 | 37.1 | 192 | 13 | 41 | 95.3 | 0.21 |
Sources
- McMahan et al., Communication-Efficient Learning of Deep Networks from Decentralized Data, Table 2 (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







