TL;DR
Federated learning trains one shared model on data that stays on many separate devices, such as phones. Each device improves a copy of the model using only its own data and sends back the change, never the raw data. A central server combines the changes, usually by a weighted average, and sends the new model out again. The method was introduced to keep private data on the phone while still learning from it.
Builds on: Stochastic Gradient Descent, Empirical Risk Minimization · Leads to: the FedAvg paper, Data Parallelism
Where it's used
Next-word prediction on phone keyboards. The text a person types, including passwords and private messages, is the most sensitive data a phone holds. A keyboard model that learns from that text can improve suggestions without uploading the text. The 2016 FedAvg paper by McMahan and colleagues names this as a motivating case, together with language models for speech recognition.
Ranking photos on a phone. An image model can learn which photos a user is likely to view again or share. The photos themselves are large and private, and the model needs only the training signal from each phone. The same paper uses this as its second motivating example.
Hospitals and other institutions with locked-up records. Several hospitals may want one diagnostic model, but each one is bound by rules that keep patient records local. Federated learning lets each site train on its own records and share only model updates. This is an application of the idea, not a claim about any particular deployment. It is not medical advice.
How it works
Take one tiny instance: three clients (phones) hold 1, 3 and 4 training examples, and the model has a single number as its parameter, which we call . One round of training works like this.
- The server sends the model. The server holds the current model and sends it to the three clients. Check: every client starts from the same value.
- Each client trains on its own data. Client 1 runs a few gradient steps on its one example and ends with . Client 2 trains on its three examples and ends with . Client 3 trains on its four examples and ends with . The raw examples never leave the devices. Check: each client's result depends only on its own data.
- Each client sends back its model, not its data. The server receives 6, 7 and 9. Check: the messages contain model parameters, which are far smaller than a photo collection.
- The server averages, weighted by data. The weights are the share of the total data on each client: , and . The new model is . Check: the client with the most data pulls the average the most.
- Repeat. The server sends back out and the next round begins. Check: the loop continues until the model is good enough.
The idea is simple, but the hard parts are hidden in step 2: the clients see different data, and they do not all show up every round. The rest of this lesson explains both problems.
The math
The objective
In ordinary training on one machine we minimise the average loss over all examples:
where is the loss on example . This is the form that Empirical Risk Minimization gives. In federated learning the examples are split across clients. Client holds the index set with examples, and its own local objective is
Because the examples are split, the global objective can be written as a weighted sum of the local ones:
The weight is the share of the data on client . This is the formula behind step 4 of the walkthrough.
Two words recur below. The data are called IID (independent and identically distributed, see Statistical Independence) when each client's examples look like a random sample from the same population. They are non-IID when they do not, for example because each phone's user types different words or photographs different things.
Step 2 in symbols: each client's local update
Client does not compute in one jump. It runs Stochastic Gradient Descent on its own data for a number of local steps. A single local step with learning rate and a minibatch of its examples is
After local passes over its data, with batch size , the client returns its model . The number of local updates it makes in a round is about , where is the average number of examples per client.
Step 4 in symbols: the server's average
The server combines the returned models with the same weights:
Here is the set of clients that took part in round , and is the total number of examples they hold. This is the update the FedAvg paper calls FederatedAveraging, and it is the most common form of the method.
Why the simplest version is not enough
Suppose instead each client computes one gradient and the server applies one step:
This equals plain full-batch gradient descent on , so it is correct, but it needs one communication round per gradient step. The FedAvg paper calls this FedSGD and uses it as the baseline. The point of FedAvg is that each round can do many local steps, so fewer rounds are needed for the same progress.
Worked examples
- Client A holds 1 example and returns . Client B holds 3 examples and returns .
- The total number of examples is .
- The weights are and .
- The average is .
- Interpretation: the server's model sits closer to client B, because B holds three times as much data.
- Three clients each hold 100 examples, so and .
- Each weight is .
- The returned models are , and .
- The average is .
- Interpretation: with equal data the weights are equal and the result is the ordinary mean of the three models. Compare with the earlier example to see how the weights change the answer.
- The FedAvg paper trains a convolutional network on MNIST digits with 100 clients. Each round, a random 10 percent of clients (that is, ) take part.
- The target is 99 percent test accuracy. With the baseline FedSGD, the IID split needs 626 communication rounds.
- With FedAvg using 20 local epochs (full passes over the local data) and batch size 10, the IID split reaches the target in 18 rounds.
- The speedup is , which the paper rounds to 35 times in the text.
- For the non-IID split, where the paper partitions the digits pathologically across clients, FedSGD needs 483 rounds and FedAvg with the same settings needs 173. The speedup is .
- Interpretation: local computation saves communication in both cases, and much less when the data is non-IID. The gap between the two cases is the cost of heterogeneous clients.
Try it: a FedSGD baseline needs 800 rounds on the IID split and FedAvg needs 40. What is the speedup? Answer: , so FedAvg is 20 times faster in rounds.
Common mistakes and tradeoffs
It is tempting to think that federated learning is private by default. In fact the updates can still leak information about the local data, and the server must be trusted to combine them honestly. Sending model updates instead of data reduces the risk, but it does not remove it. Practical systems add further protections, such as secure aggregation (a protocol in which the server learns only the sum of the client updates) and noise added to the updates. Those are separate techniques, each with its own costs.
It is tempting to think that averaging the models gives the same result as training on all the data at once. In fact this holds only in a simple setting. When the clients' data differ, which is the usual case on phones, each local model drifts toward its own data, and the average can be worse than centralised training on the same number of steps. The paper's experiments show that FedAvg still converges well in practice, and it also notes that very many local steps can plateau or diverge.
It is tempting to think that more devices always helps. In fact each round uses only a fraction of the clients, and more clients per round gives diminishing returns. The paper's experiments find that a client fraction of about 0.1 gives a good balance between computation and convergence.
When to use it. Use federated learning when the data is spread over many devices or institutions, is private or large compared with the model, and the communication cost matters more than raw compute.
When not to use it. Do not use it when the data can be pooled centrally without problems, because centralised training is simpler and faster. It is also a poor fit when the clients are few, always online and have fast links, since then the ordinary Data Parallelism setting is easier.
Alternatives. Model averaging over independent models also combines several models, but each model there is trained on the full data. Splitting a single large model across machines is Model Parallelism, and copying one model across machines that share data is Data Parallelism.
History
Before the mid-2010s, distributed training of neural networks was usually done inside a data center, where the network is fast and the data can be moved freely. Researchers had studied averaging locally trained models before, but mostly for clusters with fast links and balanced data. In the 2016 FedAvg paper, Brendan McMahan and colleagues at Google named the new problem federated learning. They stressed the non-IID and unbalanced nature of phone data, and the limits on upload bandwidth, which the paper puts at 1 MB per second or less in typical settings.
The method caught on because it was simple to implement and cheap to run. Its name comes from the federation of devices coordinated by a server. The later literature studies the same problem with privacy protections, asynchronous updates and personalised models, all of which build on the 2016 paper.
Recap
- The data stays on each client; only model parameters move between client and server.
- Each client runs local SGD for several steps, which is what makes a round worth its communication cost.
- The server averages the returned models, weighted by how much data each client holds: .
- Non-IID and unbalanced data make the average drift, and the amount of local work has to be tuned against it.
- Read next: the FedAvg paper for the experiments, and Data Parallelism for the closely related setting inside one data center.