Sam Babichenko

Reading notes / Diffusion models

Generalization in diffusion models

Kadkhodaie et al., 2024

Context. A diffusion model is a denoiser trained by least squares at every noise level. Over all functions, the objective has a useless exact minimizer: for NN training images xix_i at noise level σ\sigma, the best function is the posterior mean under the empirical measure, f^(y)=∑iwi(y) xi\hat f(y)=\sum_iw_i(y)\,x_i with wi∝e−∥y−xi∥2/2σ2w_i\propto e^{-\|y-x_i\|^2/2\sigma^2}, whose score is that of the Gaussian-blurred training set and whose reverse diffusion returns training images (a standard computation, not made in the paper). Models trained on large sets visibly do not do this, so their novelty comes from the network's inductive bias. This paper measures when that happens and what the bias is. A trained denoiser acts at each noisy image as shrinkage in an orthonormal basis adapted to that image, keeping coefficients above the noise and suppressing the rest, and trained networks tend toward bases of oscillations along contours and inside smooth regions (geometry-adaptive harmonic bases, GAHBs). Photographs are sparse in such bases, so a modest number of images determines essentially the same denoiser.

Setting. Observations y=x+zy=x+z, z∼N(0,σ2Id)z\sim N(0,\sigma^2I_d), with pσ=p∗N(0,σ2I)p_\sigma=p*N(0,\sigma^2I). Miyasawa's identity (Tweedie's formula) and its second-order version give f⋆(y)=E[x∣y]=y+σ2∇log⁡pσ(y),∇f⋆(y)=I+σ2∇2log⁡pσ(y)=σ−2 Cov[x∣y].f^\star(y)=\mathbb E[x\mid y]=y+\sigma^2\nabla\log p_\sigma(y),\qquad \nabla f^\star(y)=I+\sigma^2\nabla^2\log p_\sigma(y)=\sigma^{-2}\,\mathrm{Cov}[x\mid y]. Networks fθf_\theta minimize MSE(f,σ2)=E∥x−f(y)∥2\mathrm{MSE}(f,\sigma^2)=\mathbb E\|x-f(y)\|^2 over σ∈[0,1]\sigma\in[0,1] (pixel range [0,1][0,1]). They are blind (no σ\sigma input) and bias-free (no additive constants anywhere): a UNet and a smaller BF-CNN, sampled by a reverse diffusion driven by the residual f(y)−yf(y)-y. The data are CelebA faces and LSUN bedrooms at 80×8080\times80, and synthetic classes whose optimal denoiser is known asymptotically. Generalization means vanishing model variance (two networks trained on disjoint sets compute the same function), which can be measured without knowing f⋆f^\star; model bias is assessed separately on the synthetic classes.

Main findings. All results are empirical, and GAHBs are described by pictures and never defined. As the training set grows from 1 to 10510^5 faces, denoisers move from memorization to generalization (Step 2). The generalizing denoiser is shrinkage in the eigenbasis of its Jacobian, and that basis is a GAHB. Where a GAHB is optimal, on geometric CαC^\alpha images (α\alpha-smooth regions separated by α\alpha-smooth contours) for which bandlets are near optimal, the networks nearly reach the optimal asymptotic PSNR slope α/(α+1)\alpha/(\alpha+1) (the slope of output PSNR against input PSNR at small noise) (measured 0.46, 0.61, 0.75, 0.83 against 0.50, 0.67, 0.75, 0.83 for α=1,2,3,5\alpha=1,2,3,5). Where it is not, on classes supported on low-dimensional manifolds, the networks still keep GAHB components and fall short of the optimal slope, and on faces scrambled by a fixed pixel permutation they do much worse than on the same faces unscrambled.

Step 1

The density model is as good as the denoiser, so studying the density means studying the denoiser. Substitute sθ=(fθ(y)−y)/σ2s_\theta=(f_\theta(y)-y)/\sigma^2 into Miyasawa's identity. By the Pythagorean decomposition around E[x∣y]\mathbb E[x\mid y], the score error becomes a denoising gap, E∥∇log⁡pσ−sθ∥2=σ−4(MSE(fθ,σ2)−MSE(f⋆,σ2))\mathbb E\|\nabla\log p_\sigma-s_\theta\|^2=\sigma^{-4}\big(\mathrm{MSE}(f_\theta,\sigma^2)-\mathrm{MSE}(f^\star,\sigma^2)\big), and the likelihood bound for score-based models, for the variance-exploding SDE, gives DKL(p ∥ pθ)≤∫0∞(MSE(fθ,σ2)−MSE(f⋆,σ2)) σ−3 dσ.D_{\rm KL}(p\,\|\,p_\theta)\le\int_0^\infty\big(\mathrm{MSE}(f_\theta,\sigma^2)-\mathrm{MSE}(f^\star,\sigma^2)\big)\,\sigma^{-3}\,d\sigma . So learning the density is optimal denoising at every noise level, with small noise weighted heavily. The second-order identity says what optimal denoising looks like: MSE(f⋆,σ2)=E tr Cov[x∣y]=σ2 E tr ∇f⋆(y)\mathrm{MSE}(f^\star,\sigma^2)=\mathbb E\,\mathrm{tr}\,\mathrm{Cov}[x\mid y]=\sigma^2\,\mathbb E\,\mathrm{tr}\,\nabla f^\star(y), so a small error means a Jacobian with few non-negligible eigenvalues. SURE states the same trade-off for any denoiser: fit the noisy image while keeping the Jacobian's trace, an effective rank, small. The bound is never evaluated for real images, where f⋆f^\star is unknown.

Step 2

Denoisers trained on more images fit their training set worse and agree with each other more. UNets are trained on N∈{1,10,…,105}N\in\{1,10,\dots,10^5\} CelebA faces. At N=1N=1 and 10 training PSNR rises with unit slope while test PSNR is low and flat, which is what a denoiser that returns stored images produces; at N=105N=10^5 the curves coincide, training PSNR having fallen as test PSNR rose. Two networks trained on disjoint sets and sampled from the same noise agree with this: for N≤100N\le100 each returns an image from its own training set, and at N=105N=10^5 nearly the same image as the other network, unlike the nearest training images. Two things follow. The networks are far from the exact empirical minimizer, whose training error vanishes at small noise, so architecture and optimization set their behavior; the objective alone would memorize. And agreement across disjoint data shows vanishing model variance directly, which train-test parity alone does not.

Step 3

A bias-free network is exactly adaptive shrinkage, and its basis can be read off. With ReLUs and no additive constants the map is positively homogeneous and piecewise linear, so f(y)=∇f(y) yf(y)=\nabla f(y)\,y. Diagonalizing the Jacobian, f(y)=∑kλk(y) ⟨y,ek(y)⟩ ek(y),f(y)=\sum_k\lambda_k(y)\,\langle y,e_k(y)\rangle\,e_k(y), a shrinkage whose factors λk(y)\lambda_k(y) and basis ek(y)e_k(y) both depend on the noisy image. For a UNet trained on 10510^5 faces, the clean image's coefficients ⟨x,ek(y)⟩\langle x,e_k(y)\rangle decay quickly, so the image lies mostly in the span of the directions the denoiser keeps, and the factors fall with kk from slightly above 1 toward 0. The eigenvectors are oscillations that follow the contours of the face and fill its smooth regions, with frequency rising as λk\lambda_k falls.

Step 4

Approximation theory rates a basis by the decay of the image's coefficients in it. Fix an orthonormal basis and let an oracle choose λk\lambda_k from the clean image. Its risk ∑k(1−λk)2ck2+λk2σ2\sum_k(1-\lambda_k)^2c_k^2+\lambda_k^2\sigma^2, with ck=⟨x,ek⟩c_k=\langle x,e_k\rangle, is minimized at λk=ck2/(ck2+σ2)\lambda_k=c_k^2/(c_k^2+\sigma^2), the Bayes linear factor for a coefficient of prior variance ck2c_k^2, and the minimal risk is within a factor 2 of an MM-term approximation error, ∑kσ2ck2ck2+σ2 ≍ ∑kmin⁡(ck2,σ2)=Mσ2+∥x−xM∥2,\sum_k\frac{\sigma^2c_k^2}{c_k^2+\sigma^2}\ \asymp\ \sum_k\min(c_k^2,\sigma^2)=M\sigma^2+\|x-x_M\|^2, where MM counts coefficients above the noise. If the sorted coefficients obey ck2≍k−(α+1)c_k^2\asymp k^{-(\alpha+1)}, then M≍σ−2/(α+1)M\asymp\sigma^{-2/(\alpha+1)}, both terms are of order σ2α/(α+1)\sigma^{2\alpha/(\alpha+1)}, and the PSNR curve has asymptotic slope α/(α+1)\alpha/(\alpha+1); soft thresholding of the noisy coefficients reaches this rate up to a log factor (Donoho and Johnstone). Adapting the basis to the image can only steepen the slope, but the basis must be chosen from yy. Choosing the best basis for yy from a dictionary of polynomially many vectors and thresholding attains the oracle rate, while unlimited adaptivity such as e1(y)=y/∥y∥e_1(y)=y/\|y\| has too much variance. The question for networks is which dictionary their bias implements.

Step 5

Where a GAHB is optimal, the networks nearly reach the optimal rate. Geometric CαC^\alpha images are α\alpha-regular on regions separated by α\alpha-regular contours. Their optimal sparse representation is a bandlet basis, harmonics in the smooth regions and along the contours, whose sorted coefficients decay as k−(α+1)k^{-(\alpha+1)}, so by Step 4 the optimal PSNR slope is α/(α+1)\alpha/(\alpha+1). UNets trained on 10510^5 such images come close to it (the slopes in Main findings), and their top eigenvectors are harmonics on the two regions and along the boundary, a bandlet-like basis with more flexible geometry. Model bias is therefore small on this class in the sense of the rate, though the slope fixes only the exponent and says nothing about the constant.

Step 6

Where a GAHB is not optimal, the networks still use one, which identifies the bias. Disks with random center, radius and foreground and background intensities form a five-dimensional curved manifold. For noise small against the curvature the optimal denoiser projects onto the five-dimensional tangent space, with error about 5σ25\sigma^2 and PSNR slope 1. A UNet trained on 10510^5 disks gets the first five eigenvectors right (they match the tangent basis) but keeps further GAHB eigenvectors with non-negligible λk\lambda_k, oscillating along the rim and across the background, whose number grows as σ\sigma decreases, so the slope stays below 1. Faces scrambled by one fixed pixel permutation have exactly the optimal error of ordinary faces (unscramble, denoise, rescramble), yet the network trained on them has a much lower PSNR slope and noisier bases. So the preference is specifically for geometry and locality in pixel space; low-dimensional structure in the data is not enough.

Consequences and limits. Memorization and generalization are two regimes of one model. Small sets are fitted closely and sampling returns training images; on large sets the denoiser settles on a function that does not depend on which 10510^5 images were used. That function is adaptive harmonic shrinkage, which explains both the high sample quality on photographs and CαC^\alpha images and the measurable suboptimality on manifolds and scrambled images. Here diffusion models avoid the curse of dimensionality because the network's approximation class suits images, and nothing in the objective supplies that. The limits are that the evidence is empirical, on small images and on blind, bias-free networks with one sampler; that vanishing model variance does not show that the shared model is the true density; that the KL bound of Step 1 is never evaluated on real data; and that GAHBs have no formal definition, with why convolutions and ReLUs produce them left open.

Terms

f⋆(y)=y+σ2∇log⁡pσ(y)f^\star(y)=y+\sigma^2\nabla\log p_\sigma(y)
Miyasawa (Tweedie): the optimal denoiser gives the score
∇f⋆=σ−2Cov[x∣y]\nabla f^\star=\sigma^{-2}\mathrm{Cov}[x\mid y]
optimal Jacobian is the posterior covariance; MSE⋆=σ2E tr ∇f⋆\mathrm{MSE}^\star=\sigma^2\mathbb E\,\mathrm{tr}\,\nabla f^\star
KL bound
DKL(p∥pθ)≤∫(MSE(fθ)−MSE(f⋆))σ−3dσD_{\rm KL}(p\|p_\theta)\le\int(\mathrm{MSE}(f_\theta)-\mathrm{MSE}(f^\star))\sigma^{-3}d\sigma
SURE
MSE=E[∥y−f(y)∥2+2σ2tr∇f]−σ2d\mathrm{MSE}=\mathbb E[\|y-f(y)\|^2+2\sigma^2\mathrm{tr}\nabla f]-\sigma^2d: fit versus effective rank
Empirical minimizer
∑iwi(y)xi\sum_iw_i(y)x_i, softmax weights; reproduces the training set (not computed in the paper)
Model variance
disagreement of networks trained on disjoint sets; its vanishing is the paper's generalization
Bias-free denoiser
no additive constants, so f(y)=∇f(y)yf(y)=\nabla f(y)y and the Jacobian describes the map
Adaptive shrinkage
f(y)=∑kλk(y)⟨y,ek(y)⟩ek(y)f(y)=\sum_k\lambda_k(y)\langle y,e_k(y)\rangle e_k(y)
Oracle factor
λk=ck2/(ck2+σ2)\lambda_k=c_k^2/(c_k^2+\sigma^2); risk ≍Mσ2+∥x−xM∥2\asymp M\sigma^2+\|x-x_M\|^2
PSNR slope α/(α+1)\alpha/(\alpha+1)
rate for sorted coefficients ck2≍k−(α+1)c_k^2\asymp k^{-(\alpha+1)}; optimal for CαC^\alpha images
GAHB
geometry-adaptive harmonic basis: oscillations along contours and in smooth regions

Quiz

  1. Why does the paper treat generalization of a diffusion model as a question about the network's inductive bias?
  2. What does MSE(f⋆,σ2)=σ2 E tr ∇f⋆(y)\mathrm{MSE}(f^\star,\sigma^2)=\sigma^2\,\mathbb E\,\mathrm{tr}\,\nabla f^\star(y) say about good denoisers?
  3. Faces scrambled by one fixed pixel permutation have the same optimal denoising error as ordinary faces. What does the network's much worse performance on them show?
  4. On geometric CαC^\alpha images, UNets trained on 10510^5 examples reach PSNR slopes close to the optimal α/(α+1)\alpha/(\alpha+1). What does this establish?

Sources