Search

Search papers and lessons

Federated Learning

Level 4 · 11 min read

Federated learning trains one shared model across many devices while the training data stays on each device; devices send model updates to a server, which averages them.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Linear algebra
  • Learning basics
  • Optimizers
  • ReadReady to readNot yet

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 ww. One round of training works like this.

  1. The server sends the model. The server holds the current model w=4w = 4 and sends it to the three clients. Check: every client starts from the same value.
  2. Each client trains on its own data. Client 1 runs a few gradient steps on its one example and ends with w1=6w^1 = 6. Client 2 trains on its three examples and ends with w2=7w^2 = 7. Client 3 trains on its four examples and ends with w3=9w^3 = 9. The raw examples never leave the devices. Check: each client's result depends only on its own data.
  3. 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.
  4. The server averages, weighted by data. The weights are the share of the total data on each client: 1/81/8, 3/83/8 and 4/84/8. The new model is w=(1/8)(6)+(3/8)(7)+(4/8)(9)=0.75+2.625+4.5=7.875w = (1/8)(6) + (3/8)(7) + (4/8)(9) = 0.75 + 2.625 + 4.5 = 7.875. Check: the client with the most data pulls the average the most.
  5. Repeat. The server sends w=7.875w = 7.875 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 nn examples:

min⁡w f(w),f(w)=1n∑i=1nfi(w),\min_{w}\ f(w), \qquad f(w) = \frac{1}{n}\sum_{i=1}^{n} f_i(w),

where fi(w)f_i(w) is the loss on example ii. This is the form that Empirical Risk Minimization gives. In federated learning the examples are split across KK clients. Client kk holds the index set Pk\mathcal{P}_k with nkn_k examples, and its own local objective is

Fk(w)=1nk∑i∈Pkfi(w).F_k(w) = \frac{1}{n_k}\sum_{i \in \mathcal{P}_k} f_i(w).

Because the examples are split, the global objective can be written as a weighted sum of the local ones:

f(w)=∑k=1Knkn Fk(w).f(w) = \sum_{k=1}^{K} \frac{n_k}{n}\, F_k(w).

The weight nk/nn_k/n is the share of the data on client kk. 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 kk does not compute FkF_k 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 η\eta and a minibatch bb of its examples is

w←w−η ∇ℓ(w;b).w \leftarrow w - \eta\, \nabla \ell(w; b).

After EE local passes over its data, with batch size BB, the client returns its model wkw^k. The number of local updates it makes in a round is about u=nE/(KB)u = nE/(KB), where n/Kn/K 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:

wt+1=∑k∈Stnkmt wt+1k,mt=∑k∈Stnk.w_{t+1} = \sum_{k \in S_t} \frac{n_k}{m_t}\, w^k_{t+1}, \qquad m_t = \sum_{k \in S_t} n_k.

Here StS_t is the set of clients that took part in round tt, and mtm_t 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 gk=∇Fk(wt)g_k = \nabla F_k(w_t) and the server applies one step:

wt+1=wt−η∑k=1Knkngk.w_{t+1} = w_t - \eta \sum_{k=1}^{K} \frac{n_k}{n} g_k.

This equals plain full-batch gradient descent on ff, 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

Example
1. Two clients, one parameter
  1. Client A holds 1 example and returns wA=4w^A = 4. Client B holds 3 examples and returns wB=8w^B = 8.
  2. The total number of examples is m=1+3=4m = 1 + 3 = 4.
  3. The weights are nA/m=1/4n_A/m = 1/4 and nB/m=3/4n_B/m = 3/4.
  4. The average is w=(1/4)(4)+(3/4)(8)=1+6=7w = (1/4)(4) + (3/4)(8) = 1 + 6 = 7.
  5. Interpretation: the server's model sits closer to client B, because B holds three times as much data.
Example
2. Three clients with equal data
  1. Three clients each hold 100 examples, so n1=n2=n3=100n_1 = n_2 = n_3 = 100 and m=300m = 300.
  2. Each weight is 100/300=1/3100/300 = 1/3.
  3. The returned models are w1=2w^1 = 2, w2=5w^2 = 5 and w3=11w^3 = 11.
  4. The average is w=(1/3)(2+5+11)=(1/3)(18)=6w = (1/3)(2 + 5 + 11) = (1/3)(18) = 6.
  5. 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.
Example
3. The communication saving reported in the FedAvg paper
  1. The FedAvg paper trains a convolutional network on MNIST digits with 100 clients. Each round, a random 10 percent of clients (that is, C=0.1C = 0.1) take part.
  2. The target is 99 percent test accuracy. With the baseline FedSGD, the IID split needs 626 communication rounds.
  3. 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.
  4. The speedup is 626/18≈34.8626 / 18 \approx 34.8, which the paper rounds to 35 times in the text.
  5. 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 483/173≈2.8483/173 \approx 2.8.
  6. 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: 800/40=20800/40 = 20, 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: wt+1=∑k(nk/mt) wt+1kw_{t+1} = \sum_k (n_k/m_t)\, w^k_{t+1}.
  • 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.

Check yourself

QuestionThree clients hold 2, 2 and 6 examples and return the parameters 1, 4 and 10. What is the weighted average?Show answer
The total is m=10m = 10, so the weights are 0.20.2, 0.20.2 and 0.60.6. The average is 0.2(1)+0.2(4)+0.6(10)=0.2+0.8+6=70.2(1) + 0.2(4) + 0.6(10) = 0.2 + 0.8 + 6 = 7.
QuestionWhy does FedAvg need fewer communication rounds than FedSGD on the same task?Show answer
Each FedAvg round lets every client take many local SGD steps on its own data before sending anything back, so one round does much more work. FedSGD sends one gradient per round, so it needs many more rounds to make the same progress.
QuestionA client holds 50 examples and another holds 150. If you treat them equally by giving each the same weight, what goes wrong?Show answer
The server gives the two models the same influence, although the second client holds three times as much data. The global objective f(w)f(w) weights each client by nk/nn_k/n, so equal weights no longer match the training objective, and the average shifts toward the small client.
QuestionA hospital network wants a shared model but cannot move patient records. Describe what each site sends to the server and why that protects the records, and name one remaining risk.Show answer
Each site trains a copy of the model on its own records and sends back only the updated model parameters, not the records. This keeps the raw data local. A remaining risk is that the updates can still leak information about the records, so the server must be trusted and extra protections such as secure aggregation may be needed.
QuestionIn a scenario with 1000 clients, only 10 percent take part in each round. Roughly how many clients take part in a round, and what does this do to the average?Show answer
About 100 clients take part. The average is then taken over those 100 models only, so each round is a noisier estimate of the full average than it would be with all 1000 clients, but each round is cheaper in communication.

Sources

Federated Learning — explained simply · ungoon.me