A keyboard app wants to learn from millions of phones' typing habits to get better at suggesting the next word. Uploading everyone's actual keystrokes to one server would train a great model — and would also be a privacy catastrophe. Can a shared model still learn from data that never leaves the device it lives on?
Each device computes a gradient using only its own local data — Phone A never sees Phone B's numbers, and neither ever sees a raw data point from the other. Move θ and watch every device's bar move independently, purely from its own local view, while a "Global (averaged)" bar tracks what happens when those local gradients — not the underlying data — get combined.
Fitting a shared parameter θ normally means minimizing squared error over every data point at once. In federated averaging (FedSGD), each device instead computes its own local gradient using only its own data:
and only — one number per device, per round — ever leaves the device. The server combines them into one global update, weighted by how many points each device holds:
- — device 's local gradient, computed entirely from its own points.
- — how many data points device holds locally; used only as an averaging weight.
- — the aggregated global gradient, built only from local gradients, never raw data.
- — the learning rate applied to the global update.
- Weighting by n_k isn't incidental — it's what makes this exact
A device with more points contributes proportionally more to the global gradient, exactly the influence its points would have if they'd been pooled into one dataset and fit centrally.
- This weighted average IS the centralized gradient
Expand the weighted sum and it telescopes into precisely the gradient a single model would compute over all the devices' data pooled together — federated averaging isn't an approximation of the centralized answer here, it's mathematically identical to it, without ever pooling the raw points.
Each click runs one federated round: every device computes its own gradient at the current θ, the server averages them (weighted by sample count), and θ takes one step. Watch θ walk toward the pooled mean of all 8 points across all 4 devices — 5.875 — even though no device's raw points were ever pooled anywhere.
Four devices hold toy datasets: Phone A = [2, 3], Phone B = [8, 9, 10], Phone C = [4], Phone D = [6, 5]. Start at θ = 0.
- Each device computes its own local gradient
- Phone A:
- Phone B:
- Phone C:
- Phone D:
- The server averages them, weighted by sample count
Weighted sum , over a total of points: .
- Compare to pooling all 8 raw points directly
Pooling [2,3,8,9,10,4,6,5] and computing one centralized gradient at θ=0 gives — the identical number, with no raw point ever leaving its device.
- One step, with learning rate 0.1
. Repeating this for enough rounds walks θ all the way to 5.875 — the pooled mean of all 8 points.
Click Run one federated round repeatedly until the global (averaged) gradient is within 0.05 of zero — θ has converged, using nothing but each device's own local gradient.
Federated averaging trains one shared model by having every device compute a gradient from its own data and shipping only that gradient — never the data itself. Weighted properly by sample count, the result is mathematically identical to training on all the data pooled in one place, just without ever pooling it.