Foundations to Frontier
Tests
P. Responsible AI › P Responsible AI

Privacy: Differential Privacy, Federated Learning

Models can leak the data they were trained on: an attacker can tell whether a person was in the training set, or reconstruct examples from shared gradients. Differential privacy gives a mathematical guarantee against this: an algorithm's output barely changes, by a factor e^ε plus a small δ, whether or not any one person's data is included. It is achieved by adding calibrated noise, to answers (the Laplace and Gaussian mechanisms, randomised response) or to training (DP-SGD, which clips each person's gradient and adds Gaussian noise). Federated learning keeps data on devices and shares only model updates, which helps but is not private by itself. The exercises implement the mechanisms, then train an overfitting logistic regression on 200 people: without noise a loss-threshold attack identifies members with an advantage of about 0.45 to 0.49, and DP-SGD at ε = 1.08 cuts it to the chance level of about 0.01 to 0.1, at a large cost in accuracy for such a small dataset. Practice problems cover randomised response surveys, composition over many queries, gradient inversion in federated learning, differencing attacks and how the privacy budget grows with training.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learning from data without exposing people

A trained model is a summary of its data, and summaries can leak. Attackers have recovered training examples from language models, inferred who was in a medical dataset, and reconstructed images from shared gradients. Differential privacy limits how much any one person’s data can change what an algorithm outputs, so nothing learned from the output can depend much on that person.

🎮 Responsible AI lab

Logistic regression on 200 people with 150 features, trained for 200 full-batch steps with each person's gradient clipped to norm 1 and Gaussian noise added. An attacker guesses who was in the training set from the model's loss on them.

∞
epsilon (delta 1e-5)
100.0%
train accuracy
66.5%
test accuracy
0.480
attack advantage

Without noise the model memorises its training set and the attack wins; enough noise stops the attack but, with only 200 people, also most of the learning.

Why engineers care

In Exercise 2, a membership attack on a model trained without noise reached an advantage of 0.480; DP-SGD at ε = 1.08 brought it down to 0.015.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A membership inference attack tries to tell…
  2. 2. Differential privacy is achieved by…
  3. 3. Federated learning keeps raw data…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Differential privacy (C. Dwork, F. McSherry, K. Nissim and A. Smith, 2006): M is (ε, δ)-DP if for datasets D and D′ differing in one person and every set of outputs S, P(M(D) ∈ S) ≤ e^ε P(M(D′) ∈ S) + δ.
  • Sensitivity: the most one person can change a query’s answer; noise is scaled to it.
  • Mechanisms: Laplace noise of scale Δ/ε (pure ε-DP); Gaussian noise (ε, δ); randomised response for individual answers.
  • Composition: privacy losses add up over queries or training steps; zero-concentrated DP (ρ-zCDP) composes by adding ρ and converts to ε = ρ + 2√(ρ ln(1/δ)).
  • DP-SGD (M. Abadi and colleagues, 2016): clip each example’s gradient to norm C, add Gaussian noise of sd σC to the sum, and account for the privacy loss over steps.
  • Federated learning (B. McMahan and colleagues, 2017): clients train locally and send updates for averaging (FedAvg); updates can still leak data, so it is combined with secure aggregation and DP.
  • Post-processing: anything computed from a DP output is equally private.
📝 Quick check (Level 1)
0/3 answered
  1. 1. The sensitivity of a counting query is…
  2. 2. Laplace noise for ε-DP has scale…
  3. 3. DP-SGD bounds each person's influence by…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Mechanisms and private training

Exercise 1.

  • Add Laplace noise to a count and measure the error against 1/ε.
  • Run randomised response and invert it to estimate a share.
  • Compute the Gaussian mechanism’s noise and convert zCDP to (ε, δ).

Practice.

  • Decide the unit of privacy (a record, a person, a device) and bound each one’s contribution.
  • Track the total budget across every query and training step, and publish ε and δ.
  • Prefer aggregate releases with noise over raw data or per-person outputs.
  • Do not assume federated learning is private without secure aggregation and DP noise.
  • Test with attacks: membership inference gives an empirical check on leakage.
G = (sigmoid(Xb @ w) - y)[:, None] * Xb               # per-example gradients
G = G / np.maximum(1, np.linalg.norm(G, axis=1, keepdims=True) / clip)
g = G.sum(0) + sigma * clip * rng.standard_normal(len(w))
w -= lr * g / len(y)
eps = zcdp_to_eps(steps / (2 * sigma ** 2), 1e-5)
🐍 Exercise 1: privacy mechanisms
Implement the Laplace and Gaussian mechanisms, randomised response with its unbiased estimator, and the conversion from zCDP to (ε, δ).
📝 Quick check (Level 2)
0/3 answered
  1. 1. With 1,000 queries under a total ε of 1, the Gaussian mechanism's error per query was about…
  2. 2. Reading a single example's input from its gradient gave a cosine similarity of…
  3. 3. Training for 400 steps instead of 25 at noise multiplier 16 raised ε from 1.55 to…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Laplace mechanism. Adding Laplace noise of scale b = Δ/ε to a query of sensitivity Δ: the densities of the output for D and D′ differ by a factor e^(|f(D) − f(D′)|/b) ≤ e^ε. The mean absolute error is b, so a count at ε = 1 is off by about 1.

Randomised response. Answering truthfully with probability q = e^ε/(1 + e^ε) makes any answer at most q/(1 − q) = e^ε times likelier under one truth than the other. The share estimate (mean − (1 − q))/(2q − 1) is unbiased, with standard error the square root of π(1 − π)/n divided by 2q − 1, where π is the share of yes reports: 0.034 for n = 1,000, ε = 1 and a true share of 0.3.

Composition. k Laplace queries with a total ε get ε/k each, so errors grow like k. Gaussian noise of sd σ on a sensitivity-1 query is ρ-zCDP with ρ = 1/(2σ²), and ρ adds up, so with a fixed total the sd grows only like the square root of k: for ε = 1 and δ = 10⁻⁵, ρ = 0.0208 and the error per query at k = 1,000 is about 124 instead of 1,000.

What ε means for attacks. For any membership test, TPR ≤ e^ε FPR + δ, so the advantage TPR − FPR is at most about (e^ε − 1)/(e^ε + 1): 0.49 at ε = 1.08 and 0.98 at ε = 4.63. Real attacks are usually far weaker than the bound.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The mean absolute error of Laplace noise of scale b is…
  2. 2. Under a fixed total budget, Gaussian noise per query grows with the number of queries k like…
  3. 3. ε = 1.08 limits any membership attack's advantage to at most about…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How much does differentially private training reduce membership leakage, and what does it cost in accuracy?

History.

  • S. Warner (1965) proposed randomised response for sensitive survey questions; I. Dinur and K. Nissim (2003) showed that answering too many queries too accurately lets an attacker reconstruct a database.
  • C. Dwork, F. McSherry, K. Nissim and A. Smith (2006) defined differential privacy and the Laplace mechanism; the US Census Bureau used it for the 2020 census.
  • M. Abadi and colleagues (2016) introduced DP-SGD with the moments accountant; B. McMahan and colleagues (2017) introduced federated averaging.
  • R. Shokri and colleagues (2017) introduced membership inference attacks on machine learning models; L. Zhu, Z. Liu and S. Han (2019) reconstructed training images from shared gradients; N. Carlini and colleagues (2021) extracted verbatim training data from a large language model.

Exercise 2.

  • Labels from a logistic model with 5 informative features out of 150; 200 training people (members), 200 non-members and 200 test people.
  • Full-batch gradient descent for 200 steps with learning rate 2; each person’s gradient is clipped to norm 1 and Gaussian noise of sd σ is added to the sum. Each step is ρ = 1/(2σ²)-zCDP; ε is reported at δ = 10⁻⁵.
  • Membership attack: predict “member” when the model’s loss on a person is below a threshold; the advantage is the largest TPR − FPR over thresholds. The chance level is the advantage between two sets of non-members.

Seed 0:

noise multiplier ε train accuracy test accuracy attack advantage
0 ∞ 100.0% 66.5% 0.480
4 23.22 99.0% 65.0% 0.330
16 4.63 77.5% 58.5% 0.215
64 1.08 54.0% 50.5% 0.015

Chance level: 0.015. Seeds 1 and 2: advantages 0.445 and 0.485 without noise, 0.345 and 0.395 at σ = 4, 0.130 and 0.255 at σ = 16, 0.090 and 0.100 at σ = 64 (chance 0.080 and 0.070); test accuracy 75.5% and 75.0% without noise, 58.0% and 60.5% at σ = 64.

Findings.

  • Without noise the model memorised its training set (98.5% to 100% train accuracy) and the attack’s advantage was 0.45 to 0.49.
  • Noise reduced leakage steadily: at ε = 1.08 the advantage fell to within 0.03 of chance in every seed.
  • The cost in accuracy was large for 200 people: test accuracy fell to 50.5% to 60.5%; private learning needs much more data per parameter.
  • ε = 23 still allowed most of the leakage, so a guarantee is only meaningful when ε is small.
🐍 Exercise 2 (research): how much does differentially private training reduce membership leakage, and what does it cost in accuracy?
Train logistic regression with DP-SGD at four noise levels, compute ε, and measure accuracy and the advantage of a membership inference attack.

Privacy in practice

  • Minimise and protect data first: collect less, separate identifiers, control access (topic O4-3).
  • Choose ε deliberately, typically between about 1 and 10, account for the whole pipeline, and report it with δ and the unit of privacy.
  • Use maintained libraries (Opacus, TensorFlow Privacy, OpenDP) with proper accounting for subsampling, rather than hand-rolled noise.
  • Pretrain on public data and fine-tune privately to recover accuracy.
  • Combine federated learning with secure aggregation and DP; test releases with membership and extraction attacks (topic P-1).

References

  1. C. Dwork, F. McSherry, K. Nissim and A. Smith (2006). “Calibrating noise to sensitivity in private data analysis.” Theory of Cryptography Conference.
  2. C. Dwork and A. Roth (2014). The Algorithmic Foundations of Differential Privacy. Foundations and Trends in Theoretical Computer Science.
  3. M. Abadi, A. Chu, I. Goodfellow, H. B. McMahan, I. Mironov, K. Talwar and L. Zhang (2016). “Deep learning with differential privacy.” ACM CCS.
  4. H. B. McMahan, E. Moore, D. Ramage, S. Hampson and B. Agüera y Arcas (2017). “Communication-efficient learning of deep networks from decentralized data.” AISTATS.
  5. R. Shokri, M. Stronati, C. Song and V. Shmatikov (2017). “Membership inference attacks against machine learning models.” IEEE Symposium on Security and Privacy.