Differential Privacy in Machine Learning
The problem: models can leak their training data
Trained models don't just learn general patterns — they can memorize specific details of individual training examples, and those details can sometimes be extracted or inferred later (a concern made concrete by membership inference and model extraction attacks). Differential privacy (DP) is a formal mathematical framework for bounding exactly how much any single individual's data can influence a model's output, giving a provable guarantee — not just a heuristic — about how much information about them could leak, regardless of what an attacker knows or how they try to extract it.
The formal definition
A randomized algorithm $M$ is $\varepsilon$-differentially private if for any two datasets $D_1$ and $D_2$ differing in exactly one individual's record, and for any possible output $S$: $P[M(D_1) \in S] \le e^{\varepsilon} \cdot P[M(D_2) \in S]$. In words: whether or not any single person's data was included barely changes the probability distribution of the algorithm's outputs — bounded by a factor of $e^{\varepsilon}$. Smaller $\varepsilon$ means a tighter bound and stronger privacy (closer to 'this person's data made no detectable difference'), at the cost of more noise and typically worse model utility. $(\varepsilon, \delta)$-DP, a common relaxation, allows the guarantee to fail with small probability $\delta$, which is usually necessary for practical algorithms like DP-SGD to work at all.
DP-SGD: making deep learning differentially private
The standard way to train a differentially private neural network is DP-SGD (Differentially Private Stochastic Gradient Descent), introduced by Abadi et al. (2016). It modifies ordinary SGD in two ways: (1) gradient clipping — each individual example's per-sample gradient is clipped to a maximum L2 norm $C$, bounding how much any single example can influence the update; (2) noise injection — calibrated Gaussian noise is added to the sum of clipped gradients before the parameter update. The clipping bounds sensitivity (how much one record can change the result); the noise is what actually provides the formal privacy guarantee, calibrated via the privacy budget $\varepsilon$ and the number of training steps.
import torch
def dp_sgd_step(model, per_sample_grads: list[dict], clip_norm: float, noise_multiplier: float, lr: float):
"""
per_sample_grads: one gradient dict per example in the batch (requires per-example gradients,
not the usual batch-averaged gradient).
"""
clipped_sum = {name: torch.zeros_like(p) for name, p in model.named_parameters()}
for grad_dict in per_sample_grads:
total_norm = torch.sqrt(sum((g ** 2).sum() for g in grad_dict.values()))
clip_factor = min(1.0, clip_norm / (total_norm + 1e-6))
for name, g in grad_dict.items():
clipped_sum[name] += g * clip_factor # bound this example's influence
for name, p in model.named_parameters():
noise = torch.normal(0, noise_multiplier * clip_norm, size=p.shape)
noisy_grad = (clipped_sum[name] + noise) / len(per_sample_grads)
p.data -= lr * noisy_grad # standard SGD update, now with bounded, noised gradientsThe privacy-utility-compute trade-off is real and steep
Differential privacy vs other privacy-preserving approaches
| Approach | Guarantee type | Typical cost |
|---|---|---|
| Differential privacy (DP-SGD) | Formal, mathematically provable bound | Accuracy loss, compute overhead per step |
| Anonymization (e.g. removing names) | Heuristic, no formal guarantee | Low, but vulnerable to re-identification attacks |
| Federated learning alone | Data stays local, no direct guarantee against leakage via gradients | Communication overhead, still needs DP for strong guarantees |
| Homomorphic encryption | Cryptographic — computation on encrypted data | Very high compute overhead |
The privacy budget as a finite, composing resource
A crucial and often underappreciated property of differential privacy is composition: each query or training step against the same dataset consumes some of a finite privacy budget, and running multiple DP operations on the same data degrades the overall guarantee (roughly additively, more tightly via advanced composition theorems). This means DP isn't a one-time setting to configure and forget — it's an ongoing budget that must be tracked across an entire pipeline of training runs, hyperparameter searches, and evaluations touching the same sensitive dataset, since each one spends from the same finite privacy allowance.
What's Next?
Differential privacy bounds information leakage through noise and mathematical guarantees at the data/gradient level; an entirely different approach to the same underlying problem — protecting data while still computing on it — works by never decrypting the data at all, which is the domain of homomorphic encryption.
I build these systems professionally.
Whether it's a RAG pipeline, analytics migration, or AI workflow — let's talk.