4.5.13Generative Models

Score-based generative models

2,590 words12 min readdifficulty · medium2 backlinks

What is the Score Function?

Why the gradient of the log probability?

Let's derive this from first principles. Suppose we have p(x)=p~(x)Zp(x) = \frac{\tilde{p}(x)}{Z} where Z=p~(x)dxZ = \int \tilde{p}(x) dx is the partition function.

xlogp(x)=xlog(p~(x)Z)\nabla_x \log p(x) = \nabla_x \log \left(\frac{\tilde{p}(x)}{Z}\right)

=x[logp~(x)logZ]= \nabla_x [\log \tilde{p}(x) - \log Z]

Since ZZ doesn't depend on xx: =xlogp~(x)= \nabla_x \log \tilde{p}(x)

The magic: The intractable normalization constant ZZ disappears! This is why score-based models are powerful — we never need to compute or estimate ZZ.

Figure — Score-based generative models

Score Matching: Learning the Score

The problem: We don't know xlogpdata(x)\nabla_x \log p_{\text{data}}(x)! That's what we're trying to learn.

Denoising Score Matching

The breakthrough: We can estimate scores by adding noise and learning to denoise.

Derivation from scratch:

  1. Add Gaussian noise: x~=x+σϵ\tilde{x} = x + \sigma \epsilon where ϵN(0,I)\epsilon \sim \mathcal{N}(0, I)
  2. The perturbed distribution is qσ(x~x)=N(x~;x,σ2I)q_\sigma(\tilde{x}|x) = \mathcal{N}(\tilde{x}; x, \sigma^2 I)
  3. The marginal is qσ(x~)=pdata(x)qσ(x~x)dxq_\sigma(\tilde{x}) = \int p_{\text{data}}(x) q_\sigma(\tilde{x}|x) dx

Key insight: For Gaussian noise, the score of qσ(x~x)q_\sigma(\tilde{x}|x) is: x~logqσ(x~x)=x~logN(x~;x,σ2I)\nabla_{\tilde{x}} \log q_\sigma(\tilde{x}|x) = \nabla_{\tilde{x}} \log \mathcal{N}(\tilde{x}; x, \sigma^2 I)

For a Gaussian N(y;μ,Σ)\mathcal{N}(y; \mu, \Sigma), we have: logN(y;μ,Σ)=12(yμ)TΣ1(yμ)+const\log \mathcal{N}(y; \mu, \Sigma) = -\frac{1}{2}(y - \mu)^T \Sigma^{-1}(y - \mu) + \text{const}

Taking the gradient with respect to yy: ylogN(y;μ,Σ)=Σ1(yμ)\nabla_y \log \mathcal{N}(y; \mu, \Sigma) = -\Sigma^{-1}(y - \mu)

Applying to our case (y=x~y = \tilde{x}, μ=x\mu = x, Σ=σ2I\Sigma = \sigma^2 I): x~logqσ(x~x)=1σ2(x~x)=ϵσ\nabla_{\tilde{x}} \log q_\sigma(\tilde{x}|x) = -\frac{1}{\sigma^2}(\tilde{x} - x) = -\frac{\epsilon}{\sigma}

Why this works: By learning to denoise, the network implicitly learns the score function at noise level σ\sigma.

Multi-Scale Score Matching

Real data lives on low-dimensional manifolds in high-dimensional space. A single noise level σ\sigma won't cover the whole distribution well.

Why multiple scales?

  • Large σ\sigma: Ensures connectivity, no isolated modes
  • Small σ\sigma: Captures fine data structure
  • Interpolation: Smooth transition between scales

Sampling via Langevin Dynamics

Once we have the score function, we generate samples using Langevin MCMC:

Derivation: This comes from the Langevin SDE: dx=xlogp(x)dt+2dWdx = \nabla_x \log p(x) dt + \sqrt{2} dW where dWdW is a Wiener process. Discretizing with step size α\alpha: xt+1=xt+αxlogp(xt)+2αϵx_{t+1} = x_t + \alpha \nabla_x \log p(x_t) + \sqrt{2\alpha} \epsilon

Why the 2α\sqrt{2\alpha} noise term? It's the discretization of the diffusion term 2dW\sqrt{2}dW. The variance of dWdW over time dtdt is dtdt, so over step α\alpha it's 2α\sqrt{2\alpha}.

Annealing schedule: Start at σ1\sigma_1 (coarse), progressively decrease to σL\sigma_L (fine). This ensures:

  1. Initial exploration in high-noise regime
  2. Refinement in low-noise regime
  3. Avoidance of local minima

Connection to Diffusion Models

Derivation: Given xt=αtx0+σtϵx_t = \alpha_t x_0 + \sigma_t \epsilon, the conditional distribution is: q(xtx0)=N(xt;αtx0,σt2I)q(x_t | x_0) = \mathcal{N}(x_t; \alpha_t x_0, \sigma_t^2 I)

The score is: xtlogq(xtx0)=xtαtx0σt2=σtϵσt2=ϵσt\nabla_{x_t} \log q(x_t | x_0) = -\frac{x_t - \alpha_t x_0}{\sigma_t^2} = -\frac{\sigma_t \epsilon}{\sigma_t^2} = -\frac{\epsilon}{\sigma_t}

Therefore, learning to predict ϵ\epsilon is equivalent to learning the score sθ(xt,t)ϵ/σts_\theta(x_t, t) \approx -\epsilon/\sigma_t.

Recall Explain to a 12-year-old

Imagine you're lost in the mountains in thick fog. You can't see the valley where you need to go, but you have a magic compass that always points downhill.

Score-based models are like that compass. Instead of trying to memorize the exact shape of every mountain (which would be impossible), we learn a "which way is down" function everywhere. Then to get to the valley (generate realistic data), we just start anywhere random and follow the compass downhill, taking small steps.

The trick is that we learn this compass by playing a game: we take real valley locations, throw them randomly up the mountain (add noise), then train our compass to point back toward where they came from. By doing this thousands of times at different heights (noise levels), our compass learns the right direction everywhere.

Connections

  • Diffusion Models: Score-based models are the continuous-time limit of discrete diffusion
  • VAE: Both learn latent structures, but scores model p(x)p(x) directly vs. encoder-decoder
  • GAN: Both generate samples, but scores use gradients vs. adversarial training
  • Energy-Based Models: Score is the gradient of the energy function E(x)=logp(x)E(x) = -\log p(x)
  • Langevin Dynamics: The MCMC sampling procedure used for generation
  • Stochastic Differential Equations: Modern formulation treats diffusion as reverse-time SDE

#flashcards/ai-ml

What is the score function in score-based generative models? :: The gradient of the log-probability: s(x)=xlogp(x)s(x) = \nabla_x \log p(x). It points in the direction of increasing probability density.

Why do we model the score instead of the density directly?
(1) The intractable normalization constant ZZ in p(x)=p~(x)/Zp(x) = \tilde{p}(x)/Z cancels out when taking the gradient of log pp. (2) Scores are often easier to estimate in high dimensions.
What is the key insight of denoising score matching?
Add Gaussian noise x~=x+σϵ\tilde{x} = x + \sigma\epsilon to data. The score of the noised distribution is x~logq(x~x)=ϵ/σ\nabla_{\tilde{x}} \log q(\tilde{x}|x) = -\epsilon/\sigma, which we can compute directly and use as training target.

Write the denoising score matching objective :: J(θ)=Ex,ϵ[12sθ(x+σϵ)+ϵ/σ2]J(\theta) = \mathbb{E}_{x, \epsilon}[\frac{1}{2} \|s_\theta(x + \sigma\epsilon) + \epsilon/\sigma\|^2] where ϵN(0,I)\epsilon \sim \mathcal{N}(0, I).

Why do we use multiple noise scales in score-based models?
(1) Large σ\sigma ensures coverage of the entire space. (2) Small σ\sigma captures fine data details. (3) Intermediate scales bridge the gap, avoiding mode collapse and poor mixing.
What is annealed Langevin dynamics?
An MCMC sampling procedure that starts at high noise σ1\sigma_1 and progressively decreases to low noise σL\sigma_L, at each level running: xt+1=xt+αsθ(xt,σ)+2αztx_{t+1} = x_t + \alpha s_\theta(x_t, \sigma) + \sqrt{2\alpha} z_t where ztN(0,I)z_t \sim \mathcal{N}(0, I).
Why weight the score matching loss by σ2\sigma^2?
Without weighting, small σ\sigma (large scores) dominate the gradient. Weighting by σ2\sigma^2 makes the loss scale-invariant and balances learning across all noise levels.
How are score-based models related to diffusion models?
In diffusion, xt=αtx0+σtϵx_t = \alpha_t x_0 + \sigma_t \epsilon. The score is logp(xt)=ϵ/σt\nabla \log p(x_t) = -\epsilon/\sigma_t. So predicting noise ϵ\epsilon (diffusion objective) is equivalent to predicting the score (up to a scale factor).
What is the Fisher divergence minimized in score matching?
Epdata[sθ(x)xlogpdata(x)2]\mathbb{E}_{p_{\text{data}}}[\|s_\theta(x) - \nabla_x \log p_{\text{data}}(x)\|^2]. It measures the squared difference between the model score and the true score, averaged over the data distribution.
Why does the 2α\sqrt{2\alpha} noise term appear in Langevin dynamics?
It comes from discretizing the Langevin SDE dx=logp(x)dt+2dWdx = \nabla \log p(x) dt + \sqrt{2} dW. The diffusion term 2dW\sqrt{2}dW has variance 2dt2dt over time step dtdt, so over discrete step α\alpha it becomes 2α\sqrt{2\alpha}.

Concept Map

hard to learn with Z

definition

Z cancels

learn via

minimizes

problem: true score unknown

solved by

add Gaussian noise

Gaussian score

tractable target for

follow uphill from noise

Density p of x

Score function grad log p

s of x = grad log p

No partition constant Z

Score Matching

Fisher divergence J theta

Cannot access grad log p_data

Denoising Score Matching

x tilde = x + sigma epsilon

grad log q = -epsilon over sigma

Generate realistic data

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo, ise simple tarike se samajhte hain. Normally generative models mein hum p(x)p(x) yaani exact probability density seekhne ki koshish karte hain, lekin ye high dimensions mein bahut mushkil hota hai, kyunki usme ek normalizing constant ZZ aa jaata hai jise calculate karna almost impossible hai. Score-based models ka jugaad ye hai ki hum density ke bajaye uska score function seekhte hain, matlab xlogp(x)\nabla_x \log p(x). Ise aise socho jaise pahaad par khade ho aur tumhe har jagah ki exact height nahi pata, par tumhe ye pata hai ki "upar" kis direction mein hai. Bas isi slope ko follow karte hue tum random noise se shuru karke dheere-dheere realistic data tak pahunch sakte ho.

Ab magic ye hai — jab tum logp(x)\log p(x) ka gradient lete ho, to woh painful normalizing constant ZZ apne aap gayab ho jaata hai, kyunki ZZ ka xx se koi lena-dena nahi hota. Isliye hume kabhi ZZ calculate karne ki zaroorat hi nahi padti, jo ki ek badi jeet hai. Lekin ek problem hai: true score to hume pata hi nahi hai, kyunki wahi to seekhna hai. Iska solution hai denoising score matching — hum data mein thoda Gaussian noise daalte hain aur network ko sikhaate hain ki us noise ko kaise hataana hai. Gaussian ke liye score ka formula seedha ϵ/σ-\epsilon/\sigma nikalta hai, to network basically noise predict karna seekh jaata hai aur isi process mein woh score function bhi seekh leta hai.

Ye baat isliye important hai kyunki real-world data usually high-dimensional space mein ek chhoti si "manifold" par hota hai, aur ek hi noise level se poori distribution cover nahi hoti. Isliye hum multiple noise levels use karte hain — bade σ\sigma se poora space cover hota hai aur chhote σ\sigma se data ke paas ki fine details aati hain. Yahi core idea aage jaake diffusion models ka foundation banta hai, jo aaj Stable Diffusion aur DALL-E jaise powerful image generators chalate hain. To ye topic samajhna future ke advanced generative AI ke liye ekdum zaroori hai.

Go deeper — visual, from zero

Test yourself — Generative Models

Connections