6.5.8 · D4Research Frontiers & Practice

Exercises — Federated learning

3,579 words16 min readBack to topic

Before we start, one symbol reminder so nothing is used un-defined:


Level 1 — Recognition

L1.1 — Reading the global objective

You are told . There are clients with , , . What weight does each client's local loss get in the global loss?

Recall Solution

The weight for client is the fraction . First the total: Then each weight: They sum to , which they must — a weighted average always has weights adding to . Client 3 pulls the model hardest because it holds half the data.

L1.2 — Which quantity leaves the device?

In one sentence: in Federated learning, what does a client send back to the server, and what does it keep?

Recall Solution

The client sends back its updated model weights (and its count ) and keeps its raw data on-device. That is the whole point — "bring the model to the data," not data to the model. This is the reason federated learning is a Privacy-Preserving ML method by default.


Level 2 — Application

L2.1 — One FedAvg aggregation round

Round selects two clients — so the selected set is . They report:

  • Client A: , updated weight (1-D toy model)
  • Client B: , updated weight

Compute the aggregated global weight using the FedAvg rule where the sum runs only over the clients in (here, just A and B).

Recall Solution

The denominator is the total data of the selected clients only — i.e. summed over : Now the weighted average: Read the figure below. It draws A at and B at on a number line (cyan dots) with the aggregate as an amber square. Because B's weight is and A's is only , the amber square does not land at the midpoint ; it lands at , which is exactly of the way along the arrow from A to B. The picture makes concrete what the fraction does: the bigger client literally pulls the answer toward itself.

Figure — Federated learning

L2.2 — FedAvg with equals plain gradient descent

Two clients, equal data (, so each weight ). Global model , learning rate . One local SGD step each, with gradients and . Show the aggregated result equals one gradient-descent step on the global loss .

Recall Solution

Local steps first (each client does ): Aggregate (equal weights): Now the direct GD step on . The global gradient is the weighted average of local gradients: Same answer, . This is the algebraic identity from the parent note: with , FedAvg is one step of gradient descent on . The averaging of models and the averaging of gradients commute because a step is linear in the gradient.


Level 3 — Analysis

L3.1 — Communication cost of FedAvg vs centralized

Model: parameters, each a -byte float. Each round, clients are selected; every selected client both downloads and uploads the full model. Run rounds. Compare against uploading a raw dataset of images at bytes each.

Compute (a) bytes per client per round (down+up), (b) total federated bytes over 50 rounds, (c) raw-data bytes, (d) the ratio raw / federated.

Recall Solution

(a) One full model bytes. Converting: . Down + up bytes per client per round. (b) Per round for clients: bytes. Converting by dividing by : . Over rounds: bytes . (c) Raw data: bytes. Watch the unit conversion carefully: to read this in gigabytes we divide by (the number of bytes in one GB), not by (which would be terabytes): (In terabytes it would be a mere .) So the raw dataset is , not — a factor-of-1000 unit trap to avoid. (d) Ratio, both in the same unit: So the raw upload is actually smaller than 50 rounds of full-model federated traffic here — about the size. This is a genuine and important lesson: federated learning is not automatically cheaper on bandwidth. It pays per round in model-sized chunks, so with a large model, many clients, and many rounds the totals can exceed one-time raw upload. Federated learning's real wins are privacy and regulation; to also win on bandwidth you must cut rounds (larger ), sample fewer clients, or shrink each upload with Model Compression.

L3.2 — The local-drift trade-off

The simplified convergence bound is where is number of rounds and is local epochs. Here is the variance of the stochastic gradients — a measure of how noisy each client's SGD step is (how much a mini-batch gradient jitters around the true local gradient). Large means each local step is a noisier estimate, which is exactly what makes many local epochs drift off course. Explain, using the two terms, why raising can lower total communication yet raise the error floor. Then: with , , , , compare the bound at versus .

Recall Solution

The two terms tell opposite stories. The first term shrinks as you run more rounds — pure "keep talking, keep improving." The second term is the local-drift error floor: when clients do many local epochs , each one wanders toward its own optimum before the server can re-sync them, and the gradient noise amplifies that wandering. It grows like , so it punishes large hard. Bigger means you need fewer rounds (less communication) but you sit on a higher floor.

Numbers. Drift floor .

  • : .
  • : .

Read the figure below. It plots the bound as a function of rounds for the two choices of . The cyan curve () hugs a nearly-zero dashed floor and, by (cyan dot), has dropped to . The amber curve () sits on a visibly higher dashed floor (the term is 100× bigger), and even at (amber square) the term hasn't shrunk much, landing at . The eye sees the trade instantly: the second setup uses 4× fewer rounds but its bound is larger — the term ballooned (fewer rounds) and the drift floor jumped 100× (because went ). So fewer rounds isn't free.

Figure — Federated learning

Level 4 — Synthesis

Before L4.2 we need one new pair of symbols, so nothing is used un-defined:

L4.1 — Weighting fixes a noisy outlier

clients. "good" clients each hold clean samples and each learned the same 1-D weight . One outlier holds noisy samples and learned . Compute the aggregated weight under (a) un-weighted averaging (divide by client count) and (b) data-weighted FedAvg. Comment on which is safe.

Recall Solution

(a) Un-weighted (each of 100 clients gets weight ): The outlier — with almost no data — dragged the answer nearly a whole unit off the true . (b) Data-weighted FedAvg. Total data . The weighted answer is essentially — the outlier's influence is , i.e. , so it barely moves the result. Takeaway: data-weighting is the built-in defense against low-data noisy clients. (For malicious high-data clients you need a different tool — Byzantine Robust Learning with median/trimmed aggregation, since weighting alone won't stop an attacker who fakes a large .)

L4.2 — Adding differential privacy noise

Using the machinery just defined: a client's honest update is . To make the release -differentially private we first clip the update to norm , then add Gaussian noise with . (a) What is the standard deviation of the noise added? (b) If we average the noisy updates of independent clients, by what factor does the averaged noise standard deviation shrink versus a single client? Give the numeric averaged std.

Recall Solution

(a) Noise std . (b) Averaging independent noises with equal weight divides variance by , so the std divides by : So it shrinks by a factor of . This is why federated DP scales: each client can add just enough noise for its own privacy (its own ), yet after aggregating hundreds of clients the global model is barely perturbed. Clipping to is what makes the required noise finite — an un-clipped update could be arbitrarily large and would need infinite noise to hide, i.e. no achievable .


Level 5 — Mastery

L5.1 — Design and defend a full system

You are the ML lead for a next-word keyboard running on phones (Edge Computing constraints: limited battery, metered data). Design the training system. Address, with justification and numbers where earlier exercises supply them: (1) objective & weighting, (2) client sampling including the degenerate case of an empty selected set, (3) local epochs , (4) communication budget, (5) privacy with an explicit target, (6) non-IID handling.

Recall Solution

1. Objective & weighting. Minimize , weighting each phone by its keystroke count (L1.1, L4.1 show why: it neutralizes low-data noisy clients — the outlier's pull dropped to ).

2. Client sampling (and the empty-set edge case). Never touch all phones per round — sample a set of a few hundred to a few thousand devices, and only those that are idle, charging, on Wi-Fi (edge constraints). Random diverse sampling also averages over label skew (different languages/users). Degenerate case — what if ? If no eligible device responds (all offline / a dead network window), the FedAvg denominator , and the update is undefined — a division by zero. The safe rule: detect (or too few responders below a quorum) and skip the round entirely, keeping unchanged, then retry later. Never let the aggregator run with an empty or below-quorum set.

3. Local epochs . Choose a moderate . From L3.2, the drift floor grows like ; on highly non-IID typing data (each user a different vocabulary) large makes clients over-fit their own text before sync, so keep small-to-moderate rather than maxing it.

4. Communication budget. Cost per round (L3.1). Because L3.1 showed federated traffic is not automatically cheaper than raw upload, this must be budgeted deliberately: shrink each uploaded delta with Model Compression (quantize to fewer bits), sample fewer devices, and cut rounds using a moderate — but not so large that L3.2's error floor dominates. This is the round-count vs drift trade-off, spent on purpose.

5. Privacy. Raw text never leaves the phone, but that alone is not private (recall the L1 trap: gradient-inversion attacks reconstruct typed text from a single update). So layer on Differential Privacy with an explicit budget, e.g. , (small = strong privacy, tiny = negligible failure probability, per the L4 definition). On each device, clip the update to norm then add Gaussian noise scaled to . This works at scale precisely because of L4.2: each phone adds enough noise for its own guarantee, but when the server averages contributions the injected noise shrinks like — with that is , negligible against real gradient magnitudes. Net result: provable per-user privacy at almost no accuracy cost. This is the Privacy-Preserving ML backbone of the whole design.

6. Non-IID handling. Typing data is extremely heterogeneous — different languages, slang, names — so the per-client divergence is large and clients pull in conflicting directions (slower convergence, higher L3.2 drift). Three concrete mitigations: (i) keep modest so clients don't over-commit to their own optimum before syncing; (ii) sample a diverse each round so the aggregate averages over language/skew; (iii) split the model into a shared federated backbone plus a small per-user personalized head kept on-device — a Multi-Task Learning view where the common trunk trains federated while each user's head adapts locally. This preserves one global model of shared language structure while respecting each user's idiosyncratic vocabulary.

Sanity number tying it together: with the L4.2 setup, averaging noisy clients cuts the injected std from to — confirming the privacy layer (5) is affordable, so the design meets all six requirements at once. (Note: DP handles privacy but not malicious clients faking a huge ; that orthogonal threat needs Byzantine Robust Learning — see the L5 trap.)


Recall Quick self-test recap

FedAvg denominator ::: sum of the selected clients' , i.e. over the set — never the client count . Empty selected set ::: denominator is , update undefined — skip the round and keep . Why FedAvg equals GD ::: averaging models and averaging gradients commute because one SGD step is linear in the gradient. The two convergence terms ::: shrinks with more rounds; is the local-drift floor ( = stochastic-gradient variance) that grows with . Federated bandwidth ::: NOT automatically cheaper — L3.1 gave ratio (raw upload smaller); wins are privacy/regulation, bandwidth needs compression/fewer rounds. DP budget ::: small = strong privacy (output barely reacts to one person); = tiny failure slack. DP noise after averaging clients ::: std shrinks by (from to ). Weighting vs robustness ::: data-weighting stops low-data noisy clients; malicious clients need Byzantine-robust aggregation.