6.5.8Research Frontiers & Practice

Federated learning

3,057 words14 min readdifficulty · medium3 backlinks

What Problem Does This Solve?

Classic centralized ML workflow:

  1. Collect user data from millions of devices → central server
  2. Train model on aggregated dataset
  3. Deploy model back to devices

Problems:

  • Privacy: Sensitive data (health records, messages, photos) leaves user control
  • Bandwidth: Terabytes of raw data transmission
  • Regulatory: GDPR, HIPAA prohibit moving certain data
  • Latency: Round-trip to cloud for every training batch

Federated learning keeps data decentralized while still learning a global model.


How Federated Learning Works (Step-by-Step Derivation)

The Setup

We have:

  • NN clients (devices, hospitals, organizations), each with local dataset Dk\mathcal{D}_k of size nkn_k
  • Total data points: n=k=1Nnkn = \sum_{k=1}^N n_k
  • Goal: Learn a shared model θ\theta that minimizes global loss:
F(θ)=k=1NnknFk(θ)F(\theta) = \sum_{k=1}^N \frac{n_k}{n} F_k(\theta)

where Fk(θ)=1nkiDk(xi,yi;θ)F_k(\theta) = \frac{1}{n_k}\sum_{i \in \mathcal{D}_k} \ell(x_i, y_i; \theta) is client kk's local loss.

Why this weighting? Each client contributes proportionally to its data size. A client with 1000 samples should influence the model 10× more than one with 100 samples.

Algorithm: Federated Averaging (FedAvg)

The breakthrough McMahan et al. (2017) algorithm:

Server (orchestrator) steps:

  1. Initialize global model θ(0)\theta^{(0)}
  2. For each round t=0,1,2,t = 0, 1, 2, \ldots:
    • Sample subset St\mathcal{S}_t of CNC \cdot N clients (typically C=0.1C=0.1, i.e., 10%)
    • Broadcast current θ(t)\theta^{(t)} to selected clients
    • Wait for client updates
    • Aggregate: θ(t+1)=kStnknStθk(t+1)\theta^{(t+1)} = \sum_{k \in \mathcal{S}_t} \frac{n_k}{n_{\mathcal{S}_t}} \theta_k^{(t+1)} where nSt=kStnkn_{\mathcal{S}_t} = \sum_{k \in \mathcal{S}_t} n_k

Client kk steps (when selected):

  1. Receive θ(t)\theta^{(t)} from server
  2. Local training: Run EE epochs of SGD on local data Dk\mathcal{D}_k: θk(t+1)=θ(t)ηe=1EFk(θ)\theta_k^{(t+1)} = \theta^{(t)} - \eta \sum_{e=1}^E \nabla F_k(\theta)
  3. Send back θk(t+1)\theta_k^{(t+1)} (only the updated weights, not data!)

Why this works—the math:

If each client does one SGD step (E=1E=1, batch size nkn_k): θk(t+1)=θ(t)ηFk(θ(t))\theta_k^{(t+1)} = \theta^{(t)} - \eta \nabla F_k(\theta^{(t)})

Server aggregation: θ(t+1)=knkn(θ(t)ηFk(θ(t)))=θ(t)ηknknFk(θ(t))\theta^{(t+1)} = \sum_k \frac{n_k}{n} \left(\theta^{(t)} - \eta \nabla F_k(\theta^{(t)})\right) = \theta^{(t)} - \eta \sum_k \frac{n_k}{n} \nabla F_k(\theta^{(t)})

This is exactly one step of gradient descent on F(θ)F(\theta)! Multiple local epochs (E>1E>1) approximate this more efficiently.


Key Concepts Deep Dive

1. Communication Efficiency

The bottleneck: Sending model updates over networks is expensive (mobile data, slow hospital networks).

How many communication rounds vs. accuracy?

Derived from convergence analysis (simplified):

For strongly convex FF, after TT rounds: F(θ(T))F(θ)O(1T)+O(E2η2σ2)F(\theta^{(T)}) - F(\theta^*) \leq \mathcal{O}\left(\frac{1}{T}\right) + \mathcal{O}(E^2\eta^2\sigma^2)

  • First term: Faster convergence with more rounds TT
  • Second term: Error from local drift (clients diverge when doing EE local epochs)

Trade-off:

  • Large EE → fewer rounds (less communication) but higher drift error
  • Small EE → more rounds (more communication) but lower drift

Practical choice: E=5E=5 to E=20E=20 epochs per round works well.

2. Non-ID Data Distribution

Real-world data is heterogeneous across clients:

  • Label skew: User A only types in English, User B in Spanish
  • Feature skew: Hospital A has older patients, Hospital B has younger
  • Quantity skew: Some users generate 10× more data

Mathematical impact:

Define divergence between local and global optima: δk=θkθ2\delta_k = \|\theta_k^* - \theta^*\|^2

where θk=argminFk(θ)\theta_k^* = \arg\min F_k(\theta) (client kk's optimal) and θ=argminF(θ)\theta^* = \arg\min F(\theta) (global optimal).

High δk\delta_k → clients pull in conflicting directions → slower convergence.

Mitigation strategies:

  1. More local epochs helps each client find better local minimum before aggregating
  2. Client sampling: Sample diverse clients each round
  3. Personalization: Allow clients to keep small personalized layers

3. Privacy Guarantees

Claim: "Federated learning is private because raw data never leaves devices."

Reality: More nuanced.

Attack vectors:

  1. Gradient inversion: Reconstruct training samples from gradients

    • Given θ(x,y;θ)\nabla_\theta \ell(x, y; \theta), solve for xx
    • Possible for small batches, high-res images
  2. Model memorization: Neural nets memorize training examples

    • Query model with prompts → it regurgitates training data

Defense: Differential Privacy

Add calibrated noise to updates before sending:

θ~k(t+1)=θk(t+1)+N(0,σ2S2I)\tilde{\theta}_k^{(t+1)} = \theta_k^{(t+1)} + \mathcal{N}(0, \sigma^2 S^2 I)

where SS is clipping threshold (bound gradient norm).

Trade-off: More noise (σ\sigma \uparrow) → stronger privacy → lower accuracy.


Advanced Variants

Secure Aggregation

Problem: Even model updates can leak info if server is malicious.

Solution: Cryptographic protocol where server learns kθk\sum_k \theta_k but not individual θk\theta_k.

How (simplified):

  1. Each client kk generates random mask rkr_k shared with other clients (via pairwise keys)
  2. Client kk sends θk+rk\theta_k + r_k to server
  3. Server sums: k(θk+rk)=kθk+krk=0 if designed right\sum_k (\theta_k + r_k) = \sum_k \theta_k + \underbrace{\sum_k r_k}_{=0 \text{ if designed right}}
  4. Masks cancel out, server gets true aggregate without seeing individuals

Cost: 2-3× more communication, cryptographic overhead.

Cross-Silo vs. Cross-Device

Cross-Device Cross-Silo
Example Smartphones, IoT Hospitals, banks
# Clients Millions 10-100
Availability Intermittent (only when charging) Stable
Communication Slow (mobile networks) Fast (datacenter links)
Data per client Small (KBs-MBs) Large (GBs-TBs)
Strategy Many rounds, small EE Few rounds, large EE

Why this distinction matters: Algorithm hyperparameters (CC, EE, learning rate) must adapt to the setting.


Worked Example: Binary Classification

Setup: 3 hospitals want to predict disease from patient vitals without sharing records.

Data:

  • Hospital 1: 1000 patients, 60% positive
  • Hospital 2: 500 patients, 40% positive
  • Hospital 3: 2000 patients, 70% positive
  • Feature: Blood pressure (1D for simplicity)
  • Model: Logistic regression p(y=1x)=σ(θTx)p(y=1|x) = \sigma(\theta^T x)

Loss: Fk(θ)=1nki=1nk[yilogσ(θTxi)+(1yi)log(1σ(θTxi))]F_k(\theta) = \frac{1}{n_k}\sum_{i=1}^{n_k} -[y_i \log \sigma(\theta^T x_i) + (1-y_i)\log(1-\sigma(\theta^T x_i))]

Algorithm execution:

Round 0:

  • Server: Initialize θ(0)=0\theta^{(0)} = 0, sample all3 hospitals (C=1C=1)

  • Hospital 1: Receives θ(0)=0\theta^{(0)}=0

    • Runs 5 epochs SGD on 1000 samples, η=0.01\eta = 0.01
    • Computes gradients: F1(θ)=11000i(piyi)xi\nabla F_1(\theta) = \frac{1}{1000}\sum_{i} (p_i - y_i)x_i where pi=σ(θTxi)p_i = \sigma(\theta^T x_i)
    • Updates: θ1(1)=θ(0)ηF1=0.42\theta_1^{(1)} = \theta^{(0)} - \eta \nabla F_1 = 0.42 (suppose)
    • Why this step? Standard gradient descent—move weights to reduce loss on local data.
    • Sends back θ1(1)=0.42\theta_1^{(1)} = 0.42
  • Hospital 2: Similarly gets θ2(1)=0.38\theta_2^{(1)} = 0.38

  • Hospital 3: Similarly gets θ3(1)=0.51\theta_3^{(1)} = 0.51

  • Server aggregates: θ(1)=100035000.42+50035000.38+200035000.51\theta^{(1)} = \frac{1000}{3500} \cdot 0.42 + \frac{500}{3500} \cdot 0.38 + \frac{2000}{3500} \cdot 0.51 =0.2860.42+0.1430.38+0.5710.51=0.465= 0.286 \cdot 0.42 + 0.143 \cdot 0.38 + 0.571 \cdot 0.51= 0.465

    Why these weights? Total n=1000+500+2000=3500n = 1000+500+2000=3500. Hospital 3 has most data (57%), so it pulls θ(1)\theta^{(1)} closest to its θ3(1)=0.51\theta_3^{(1)}=0.51.

Round 1: Repeat from θ(1)=0.465\theta^{(1)} = 0.465 ...

After 100 rounds: θ(100)0.52\theta^{(100)} \approx 0.52 (converged global model)

Privacy preserved: Hospitals never shared patient records, only learned model weights.


Common Pitfalls & How to Fix Them


Connections to Other Concepts

  • Distributed Training: Federated learning is distributed training with privacy constraints and non-IID data
  • Differential Privacy: Formal privacy guarantee for federated updates
  • Optimization Theory: FedAvg is SGD variant; convergence depends on data heterogeneity
  • Edge Computing: Federated learning pushes computation to edge devices
  • Multi-Task Learning: Personalized federated learning treats each client as a task
  • Byzantine Robust Learning: Defense against malicious clients sending bad updates
  • Model Compression: Quantization + sparsity reduce communication cost per round
  • Privacy-Preserving ML: Federated learning is one pilar; others include homorphic encryption, secure multi-party computation

Recall Explain Federated Learning to a 12-Year-Old

Imagine you and99 friends all want to build the world's best autocorrect for texting. But here's the problem: everyone's texts are super private—you can't share them!

Old way (doesn't work): Everyone sends all their texts to one person. That person reads EVERYONE's messages (crepy!) and figures out common patterns. Then they tell everyone the autocorrect rules.

Federated learning way:

  1. One person (the "server") writes down some basic autocorrect rules on paper.
  2. They make100 copies and mail one to each friend.
  3. Each friend practices on their OWN texts (privately!) and improves their copy of the rules.
  4. Friends mail back ONLY their improved rules (not their texts!).
  5. Server combines everyone's improvements: "80 people found that'u' usually means 'you', so let's add that."
  6. Server sends out the new better rules. Repeat!

After 50 rounds, everyone has an amazing autocorrect that learned from 100 people's texting styles—but no one ever saw anyone else's private messages. That's federated learning: learn together, stay private.


Active Recall Questions

#flashcards/ai-ml

What is the core difference between federated learning and centralized ML? :: Federated learning brings the MODEL to the DATA (training happens on local devices, only weight updates are sent). Centralized ML brings DATA to the MODEL (raw data uploaded to central server for training).

Write the federated averaging update rule :: θ(t+1)=kStnkjStnjθk(t+1)\theta^{(t+1)} = \sum_{k \in \mathcal{S}_t} \frac{n_k}{\sum_{j \in \mathcal{S}_t} n_j} \theta_k^{(t+1)} where θk(t+1)\theta_k^{(t+1)} is client kk's local model after EE epochs of SGD, weighted by dataset size nkn_k.

Why do we weight clients by their dataset size nkn_k in aggregation?
To make each data point contribute equally to the global model. A client with 10× more data should influence the model 10× more. This matches the mathematical objective F(θ)=nknFk(θ)F(\theta) = \sum \frac{n_k}{n} F_k(\theta).
What is the communication cost trade-off controlled by local epochs EE?
Large EE → fewer rounds (less communication) but more client drift (local models diverge). Small EE → more rounds (more communication) but less drift. Optimal E[5,20]E \in [5, 20] for most tasks.
What is (ϵ,δ)(\epsilon, \delta)-differential privacy in one sentence?
Adding or removing any one person's data changes the algorithm's output probability by at most eϵe^\epsilon (multiplicatively) plus δ\delta, provably bounding information leakage.
What attack does differential privacy defend against in federated learning?
Gradient inversion attacks, where an adversary reconstructs training samples from the gradients/weight updates. DP adds calibrated noise to updates, making reconstruction impossible.
Why is non-IID data a problem for federated learning?
Clients optimize different objectives (θkθj\theta_k^* \neq \theta_j^* when data distributions differ). Averaging contradictory local models causes oscillation and slower convergence to the global optimum θ\theta^*.
What is secure aggregation?
Cryptographic protocol where the server learns the SUM of client updates kθk\sum_k \theta_k but cannot see individual θk\theta_k. Protects against malicious server trying to infer info from individual updates.
Cross-device vs. cross-silo: which has more clients and less data per client?
Cross-device (millions of smartphones, KBs-MBs each). Cross-silo has 10-100 organizations with GBs-TBs each.
What is client drift and when does it happen?
When clients overfit to local data during many local epochs, their models diverge from the global optimum. Happens with large EE and highly non-IID data. Mitigated by smaller EE or adaptive optimizers like FedAdam.

Concept Map

causes

motivates

principle

enables

produces

preserves privacy

minimizes

uses

implemented by

server does

client does

sends back

weighted by

Centralized ML

Privacy, Bandwidth, Regulatory, Latency problems

Federated Learning

Send model to data

Local training on device

Only weight updates shared

Global loss F theta

Data-size weighting nk over n

FedAvg McMahan 2017

Server aggregation

Client E epochs of SGD

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Federated learning ka matlab simple hai: socho tumhare pas ek billion smartphones hain aur sabko ek AI model train karna hai jo next word predict kare (jaise WhatsApp mein type karte waqt suggestions ate hain). Traditional methodein kya hota? Sab logon ke messages ek central server pe upload ho jayenge, wahan model train ho, phir wapas phone pe aye. Par yahan do badi problems hain: pehla, privacy ka issue—tumhare private chats kaun padhe server pe? Dosra, bandwidth—1 billion phones se data lana toh nightmare hai.

Federated learning ka jugaad: model ko data ke pas le jao, data ko model ke paas nahi. Matlab serverek initial model bhejta hai har phone pe, har phone apne local data pe privately train karta hai (tumhare messages kabhi bahar nahi jate), phir sirf learned weights (jo model ne seekha) wapas bhejta hai server ko. Server sabke weights ko combine karke ek global model banata hai. Is tareke se, data kabhi move nahi hota, sirf knowledge move hoti hai—privacy bani rahi, bandwidth bhi bach gayi.

Core formula:

Go deeper — visual, from zero

Test yourself — Research Frontiers & Practice

Connections