Differential Privacy › DP-SGD: Training Neural Networks with Privacy

Michael BrenndoerferFebruary 24, 20268 min read
Language AI HandbookAll chapters
Part 60 of 67Safety and Security
Chapter 464Differential Privacy › DP-SGD: Training Neural Networks with Privacy
Reading section 5 of 12 · DP-SGD: Training Neural Networks with Privacy · 8 minSection 5 of 12 · 8 min

Book contents

Section 5 of 12 · 8 min read

Differential Privacy › DP-SGD: Training Neural Networks with PrivacyOne section · Sections in this chapter5 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Applying differential privacy to training a neural network is non-trivial because the standard training algorithm, stochastic gradient descent, has unbounded sensitivity. A single outlier training example with a very large loss can produce a very large gradient, pushing model parameters arbitrarily far from where they would have been without that example. If we tried to apply the Gaussian mechanism directly to the gradient of a minibatch without any modification, we would need to add infinite noise to achieve meaningful privacy. The key challenge is bounding the sensitivity.

DP-SGD, introduced by Abadi et al. in 2016, solves this through two modifications to the standard SGD update. Together, these two changes transform the training algorithm from one with potentially unbounded sensitivity into one with precisely bounded sensitivity.

The Two Key Operations

Step 1: Per-example gradient clipping. Before aggregating gradients from a minibatch, DP-SGD clips each individual example's gradient vector to have ℓ2\ell_2 norm at most CC (the clipping threshold):

g~i=gi⋅min⁡(1,C∥gi∥2)\tilde{g}_i = g_i \cdot \min\left(1, \frac{C}{\|g_i\|_2}\right)

where:

  • gi=∇θL(θ;xi)g_i = \nabla_\theta \mathcal{L}(\theta; x_i) is the gradient of the loss for training example xix_i.
  • CC is the clipping threshold, a hyperparameter chosen before training.
  • g~i\tilde{g}_i is the clipped gradient, which satisfies ∥g~i∥2≤C\|\tilde{g}_i\|_2 \leq C by construction.

Clipping bounds the sensitivity of the gradient sum: no single example can shift the aggregate gradient by more than CC in ℓ2\ell_2 norm. Without clipping, an example with a large loss would produce a large gradient, making the sensitivity potentially infinite and requiring infinite noise for privacy. After clipping, the sensitivity is exactly CC, regardless of the data distribution. This is the mechanism that makes DP-SGD work.

To see why clipping achieves sensitivity CC, consider two neighboring datasets DD and D′=D∪{xnew}D' = D \cup \{x_\text{new}\}. The gradient sum on D′D' differs from the gradient sum on DD by exactly the clipped gradient of xnewx_\text{new}, which has ℓ2\ell_2 norm at most CC. Thus ∥f(D′)−f(D)∥2≤C\| f(D') - f(D) \|_2 \leq C, giving Δ2f=C\Delta_2 f = C.

Notice that gradient clipping is not the same as gradient norm regularization or weight clipping. It operates per-example, before aggregation, and it scales the entire gradient vector uniformly. A gradient with norm 10 and clipping threshold 1 is scaled by a factor of 0.1; its direction is preserved but its magnitude is reduced to exactly 1.

Step 2: Gaussian noise addition. After clipping and summing gradients, DP-SGD adds Gaussian noise calibrated to the clipping threshold:

g^=1B(∑i=1Bg~i+N(0,σ2C2I))\hat{g} = \frac{1}{B}\left(\sum_{i=1}^{B} \tilde{g}_i + \mathcal{N}(0, \sigma^2 C^2 \mathbf{I})\right)

where:

  • BB is the minibatch size.
  • σ\sigma is the noise multiplier, the ratio of noise standard deviation to clipping threshold.
  • The noise variance σ2C2\sigma^2 C^2 is proportional to the squared clipping threshold, since that is the sensitivity.

The noise multiplier σ\sigma is the key privacy-utility knob. Setting σ\sigma larger adds more noise, giving stronger privacy guarantees. Setting it smaller adds less noise, giving better gradient quality but weaker privacy. Given a target privacy budget (ε,δ)(\varepsilon, \delta) and a training procedure (number of steps, batch size, dataset size), the minimum σ\sigma that achieves the budget can be computed using a privacy accountant.

The full DP-SGD update then proceeds as in standard SGD: parameters are updated using this noisy, clipped gradient. The privacy guarantee applies to each step independently, and the total privacy cost after TT steps is computed via composition.

The effect of gradient clipping is intuitive to visualize: gradients with large norms are scaled down to lie exactly on the boundary of a ball of radius CC, while gradients with small norms are left unchanged. The plot below illustrates this with a distribution of gradient norms before and after clipping.

Out[3]:
Visualization
Histogram of gradient norms before clipping with a log-normal-like distribution.
Distribution of per-example gradient norms before clipping in a typical DP-SGD batch. Most gradients have modest norms, but the long tail of high-norm gradients (from difficult training examples) would dominate the unclipped aggregate and make sensitivity infinite.
Scatter plot of original vs clipped gradient norms showing identity line up to C then flat.
Effect of clipping threshold C=1.0 on gradient norms. Gradients exceeding the threshold are scaled down to exactly C (shown in red), while those below are unchanged (blue). This bounds sensitivity to C, enabling noise calibration.

The left panel shows the distribution of per-example gradient norms before clipping. The distribution is right-skewed: most gradients have moderate norms, but a long tail of high-norm gradients exists. These high-norm gradients correspond to training examples with large losses, which are often the most informative but also the most sensitive. Without clipping, these outliers would dominate the aggregate gradient and make the sensitivity unbounded.

The right panel shows what clipping does to each gradient. Every point on or below the diagonal (blue) has a norm below the threshold and is left unchanged. Every point above the threshold (red) is pulled back to exactly C=1.0C = 1.0. After clipping, no single example can contribute a gradient with norm larger than CC, bounding the sensitivity.

Privacy Accounting

A single DP-SGD step with Poisson subsampling (where each example is independently included in the minibatch with probability q=B/nq = B/n) achieves (ε′,δ′)(\varepsilon', \delta')-DP for that step. The privacy cost over TT steps accumulates, but thanks to the moments accountant or equivalently Renyi DP accountant, the total privacy cost is tighter than naive composition.

The key insight is that subsampling amplifies privacy: if only a fraction qq of the training data participates in each step, an individual example is included in any given step with probability qq. This "privacy amplification by subsampling" means the effective privacy loss per step is roughly O(qε′)O(q \varepsilon') rather than ε′\varepsilon'. Subsampling acts like an extra layer of randomness on top of the noise, making it even harder for an adversary to determine whether a specific record was used.

To see why this amplification occurs, consider the mechanism from the perspective of a single individual's record. If the record is included in the training set, it enters any given minibatch with probability qq. From the adversary's perspective, most steps look identical whether the record is in the dataset or not (because it simply was not sampled). Only the fraction qq of steps where it was sampled carry any information. This dramatically reduces the per-step privacy cost.

The total privacy cost after TT steps, at failure probability δ\delta, can be approximated as:

εtotal≈2T⋅q⋅σ−1⋅ln⁡(1/δ)\varepsilon_{\text{total}} \approx \sqrt{2T} \cdot q \cdot \sigma^{-1} \cdot \sqrt{\ln(1/\delta)}

where:

  • εtotal\varepsilon_{\text{total}} is the cumulative privacy cost after all TT training steps.
  • TT is the total number of gradient steps, equal to epochs times batches per epoch.
  • q=B/nq = B/n is the subsampling rate, the fraction of training data in each mini-batch.
  • σ\sigma is the noise multiplier; larger σ\sigma means smaller εtotal\varepsilon_{\text{total}}.
  • δ\delta is the failure probability in the (ε,δ)(\varepsilon, \delta)-DP guarantee.

This is a simplified approximation. In practice, tight accounting uses the moments accountant or the PRV accountant (Privacy Random Variable accountant), which numerically compose the per-step privacy guarantees without closed-form approximations.

The formula's implications are direct and practical:

  • More training steps TT increase privacy cost: training longer spends more budget.
  • Larger subsampling rate qq (bigger batches relative to dataset size) increases privacy cost per step.
  • Less noise σ\sigma increases privacy cost proportionally.
  • The 2T\sqrt{2T} factor reflects the square-root growth from advanced composition rather than the linear growth O(T)O(T) that naive composition would give.

This composability is what makes DP practical for machine learning: you can track exactly how much privacy budget each training step consumes, and stop training once the budget is exhausted. A privacy accountant runs alongside training, incrementing the total ε\varepsilon after each step and triggering early stopping if the budget is reached.