DP-SGD: Differentially Private Training
DP-SGD (Differentially Private Stochastic Gradient Descent) modifies the standard SGD training algorithm to provide formal differential privacy guarantees. It is the most widely used method for training neural networks with privacy.
How DP-SGD Works
DP-SGD modifies standard SGD with two key changes at each training step:
Per-Example Gradient Clipping
Compute gradients for each example individually, then clip each gradient to a maximum norm C. This bounds the sensitivity - no single example can have an outsized influence on the update.
Noise Addition
Add calibrated Gaussian noise to the clipped, aggregated gradient before applying the update. The noise scale depends on the clipping norm C, the batch size, and the target privacy budget.
Privacy Accounting
After each step, track the cumulative privacy loss using a privacy accountant (Rényi DP or PLD-based). Training stops when the privacy budget is exhausted.
def dp_sgd_step(model, batch, clip_norm, noise_multiplier, lr): """One step of DP-SGD training.""" # 1. Compute per-example gradients per_example_grads = [] for example in batch: grad = compute_gradient(model, example) per_example_grads.append(grad) # 2. Clip each gradient to bound sensitivity clipped_grads = [] for grad in per_example_grads: norm = torch.norm(grad) clip_factor = min(1.0, clip_norm / norm) clipped_grads.append(grad * clip_factor) # 3. Aggregate and add noise avg_grad = sum(clipped_grads) / len(batch) noise_std = clip_norm * noise_multiplier / len(batch) noisy_grad = avg_grad + torch.normal(0, noise_std) # 4. Update model parameters model.parameters -= lr * noisy_grad
Training with Opacus (PyTorch)
import torch from opacus import PrivacyEngine # Standard model, optimizer, dataloader model = MyModel() optimizer = torch.optim.SGD(model.parameters(), lr=0.01) dataloader = DataLoader(dataset, batch_size=256) # Attach PrivacyEngine for DP-SGD privacy_engine = PrivacyEngine() model, optimizer, dataloader = privacy_engine.make_private_with_epsilon( module=model, optimizer=optimizer, data_loader=dataloader, epochs=10, target_epsilon=3.0, target_delta=1e-5, max_grad_norm=1.0, # Clipping norm C ) # Train normally - Opacus handles clipping + noise for epoch in range(10): for batch in dataloader: optimizer.zero_grad() loss = criterion(model(batch[0]), batch[1]) loss.backward() optimizer.step() # Check privacy budget spent so far epsilon = privacy_engine.get_epsilon(delta=1e-5) print(f"Epoch {epoch}: ε = {epsilon:.2f}")
Key Hyperparameters
| Parameter | Effect | Guidance |
|---|---|---|
| Clipping norm (C) | Bounds per-example gradient influence | Too small = underfitting; too large = more noise needed. Start with median gradient norm. |
| Noise multiplier (σ) | Controls privacy-utility trade-off | Higher = more private but noisier gradients. Auto-calibrated by Opacus if target ε is set. |
| Batch size | Larger batches improve signal-to-noise ratio | Use the largest batch size you can afford. DP-SGD benefits more from large batches than standard SGD. |
| Epochs | More epochs = more privacy budget spent | Fewer epochs with larger batches is generally better under DP. |
| Learning rate | Standard training parameter | May need to be higher than usual to compensate for noisy gradients. |
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX