← All Posts
Deep Learning · Regularization in Deep LearningChapter 08 / 9

Noise, Consistency, and Gradient Penalties: Control Sensitivity

Derive how input noise induces L2 and Jacobian-plus-Hessian penalties, connect consistency losses to the Jacobian Frobenius norm, and compare R1 with WGAN-GP.

A model that memorizes training points often changes steeply in the space around them. Injecting random input noise $\xi$, penalizing prediction changes across perturbed views, or penalizing input derivatives $\nabla_x f(x)$ directly all control local sensitivity.

1. Input noise on a linear model equals exact L2 regularization

Take a linear predictor $f(x)=w^\top x$, add zero-mean noise $\xi$ with $\mathbb E[\xi]=0$ and covariance $\mathbb E[\xi\xi^\top]=\Sigma$, and let $r=w^\top x-y$ be the clean residual. Expanding the expected squared error gives:

$$\begin{aligned}\mathbb E_\xi\bigl[(w^\top(x+\xi)-y)^2\bigr]&=\mathbb E_\xi\bigl[(r+w^\top\xi)^2\bigr]\\&=r^2+2r\,w^\top\underbrace{\mathbb E_\xi[\xi]}_{0}+w^\top\underbrace{\mathbb E_\xi[\xi\xi^\top]}_{\Sigma}w=(w^\top x-y)^2+w^\top\Sigma w.\end{aligned}$$

When the noise is isotropic ($\Sigma=\sigma^2 I$), the penalty is $\sigma^2\|w\|_2^2$—so adding Gaussian input noise of variance $\sigma^2$ to linear least squares is exact L2 ridge regression (Bishop, 1995).

2. Nonlinear models: second-order Taylor expansion

For a twice-differentiable predictor $f(x)$ with clean residual $r=f(x)-y$ and isotropic noise $\xi\sim\mathcal N(0,\sigma^2 I)$, expand $f(x+\xi)$ to second order:

$$f(x+\xi)\approx f(x)+\nabla_x f(x)^\top\xi+\frac12\xi^\top H_f(x)\xi.$$

Squaring $r+\nabla_x f(x)^\top\xi+\frac12\xi^\top H_f(x)\xi$ and keeping terms up to $O(\sigma^2)$ (using $\mathbb E[\xi]=0$ and $\mathbb E[\xi^\top A\xi]=\sigma^2\operatorname{tr}A$) yields two distinct penalty terms:

$$\mathbb E_\xi\bigl[(f(x+\xi)-y)^2\bigr]=r^2+\sigma^2\Bigl(\|\nabla_x f(x)\|_2^2+r\,\operatorname{tr}H_f(x)\Bigr)+O(\sigma^4).$$
A sine predictor and its local tangent agree near the reference input but diverge as the perturbation moves farther away.
Scroll the figure horizontally on small screens.
Near $x$, $f(x)=\sin(\omega x)$ matches its tangent line, but over a wider noise neighborhood $\sigma$, curvature $f''(x)$ shifts the expected loss.

3. Lab: exact noisy loss vs. first- and second-order expansions

Gaussian perturbations of a curved predictor

Use $f(x)=\sin(\omega x)$ and target $y=0.4$. Compare exact Gaussian expected squared loss with the locally linear and second-order approximations. The selected noise level also runs 20,000 seeded Monte Carlo draws as an independent numerical check.

4. Consistency regularization vs. explicit gradient penalties

Instead of comparing $f(x+\xi)$ to the label $y$, consistency regularization (such as Mean Teacher, which compares the student on one perturbed view against an EMA teacher on another) penalizes the change in prediction itself:

$$\mathbb E_\xi\bigl[\|f(x+\xi)-f(x)\|_2^2\bigr]\approx\mathbb E_\xi\bigl[\|J_f(x)\xi\|_2^2\bigr]=\sigma^2\|J_f(x)\|_F^2,$$

where $\|J_f(x)\|_F^2=\sum_{i,j}(\partial f_i/\partial x_j)^2$ is the squared Frobenius norm of the input–output Jacobian (with no $r\operatorname{tr}H_f$ cross term because $r=0$ when comparing $f(x+\xi)$ to $f(x)$).

PenaltyFormulaWhat it enforces
Contractive / Jacobian penalty$\|J_f(x)\|_F^2$Pushes all first-order input sensitivities toward $0$ locally.
R1 GAN penalty$\frac\gamma2\mathbb E_{x\sim p_{\rm data}}\bigl[\|\nabla_x D(x)\|_2^2\bigr]$Zeroes discriminator slope on the real data manifold to prevent oscillations near Nash equilibrium.
WGAN-GP$\lambda\mathbb E_{\hat x}\bigl[(\|\nabla_{\hat x}D(\hat x)\|_2-1)^2\bigr]$Pins discriminator gradient norm to $1$ along real–fake interpolants $\hat x$ (1-Lipschitz constraint).
# R1 gradient penalty: create_graph=True keeps second derivatives for optimizer.step()
x_real = x_real.detach().requires_grad_(True)
d_real = discriminator(x_real)
grad_x = torch.autograd.grad(d_real.sum(), x_real, create_graph=True)[0]
r1_penalty = 0.5 * gamma * grad_x.flatten(1).square().sum(dim=1).mean()

5. Questions worth answering

Why does noisy supervised loss $\mathbb E[(f(x+\xi)-y)^2]$ contain a Hessian trace term while consistency loss $\mathbb E[\|f(x+\xi)-f(x)\|_2^2]$ does not?

In $(f(x+\xi)-y)^2=(r+\nabla f^\top\xi+\frac12\xi^\top H_f\xi)^2$, the Hessian enters through the cross product $2r(\frac12\xi^\top H_f\xi)$ with the clean residual $r=f(x)-y$. Consistency compares $f(x+\xi)$ to $f(x)$, where the baseline residual is identically zero.

Why must `torch.autograd.grad(..., create_graph=True)` be set when computing an input gradient penalty?

Because the penalty depends on $\nabla_x D_\theta(x)$, and updating the weights $\theta$ requires differentiating that penalty with respect to $\theta$ (computing mixed second partials $\frac{\partial}{\partial\theta}\nabla_x D_\theta(x)$).

Sources

Bishop, Training with Noise is Equivalent to Tikhonov Regularization; Tarvainen and Valpola, Mean Teachers are Better Role Models; Mescheder et al., Which Training Methods for GANs Do Actually Converge?; Gulrajani et al., Improved Training of Wasserstein GANs.