CS236 第 12 講 · 不用取樣訓練 EBM
Stanford CS236 · Fall 2023 · Lecture 12

不用從模型取樣也能訓練 EBM:score matching 與 noise contrastive estimation

上一講用 contrastive divergence 訓練 EBM,但每一步都要從模型取樣,而 MCMC(包括這講開頭講的 Langevin)在高維度非常慢。這講換掉「怎麼比較資料和模型」這一環,找不需要 \(Z(\theta)\)、也不需要模型樣本的目標:score matching 比較兩個分佈的 \(\nabla_{\mathbf x}\log p\)(Fisher divergence),用分部積分消掉未知的資料 score;noise contrastive estimation 訓練一個分辨資料和雜訊的分類器,順便學到 \(Z\)。Score matching 正是下一講 diffusion model 的基石。

讀完這頁你應該能夠

  1. 用細緻平衡說明 Metropolis MCMC 為什麼以 \(p_\theta\) 為穩態分佈,並寫出 Langevin MCMC 的更新式。
  2. 說明 score function \(\nabla_{\mathbf x}\log p_\theta(\mathbf x)\) 為什麼不依賴 \(Z(\theta)\),並算出高斯與 gamma 分佈的 score。
  3. 寫出 Fisher divergence,用分部積分推導出不需要資料 score 的 score matching 目標(一維與多維)。
  4. 說明 score matching 的計算瓶頸(Hessian 的 trace)和它的直覺(讓資料點成為 log-likelihood 的局部最大值)。
  5. 推導 NCE:把分類器參數化成 \(p_\theta/(p_\theta+p_n)\),並說明為什麼可學的 \(Z\) 會收斂到真正的 partition function。
  6. 比較 NCE 和 GAN 的異同,說明 flow contrastive estimation 的動機。

前置知識檢查

  • 第 11 講:EBM、partition function、contrastive divergence、Metropolis MCMC。
  • 第 9 講:最佳 discriminator \(D^* = p_{\text{data}}/(p_{\text{data}}+p_\theta)\)。
  • 微積分:分部積分 \(\int f'g = fg - \int g'f\);梯度、Hessian、trace。
  • Logistic regression 與 cross-entropy loss。

本頁對照過投影片圖片;標「出處」的補充來自 Song & Kingma (2021)〈How to Train Your Energy-Based Models〉(TeX 原始檔,課堂最後一張投影片推薦的參考)、Yang Song〈Sliced Score Matching〉部落格、Lai et al. (2026)(arXiv 2510.21890v3)§3.1、Hyvärinen (2005)、Gutmann & Hyvärinen (2010)、Lyu (2009)。

01回顧:Metropolis MCMC 為什麼會收斂(投影片 2–3)

EBM:\(p_\theta(\mathbf x) = \exp\{f_\theta(\mathbf x)\}/Z(\theta)\)。\(Z(\theta)\) 算不出來,所以沒有 likelihood;但比較兩點很容易,\(p_\theta(\mathbf x')/p_\theta(\mathbf x) = \exp(f_\theta(\mathbf x') - f_\theta(\mathbf x))\)。用 contrastive divergence 做 MLE 時,\(\nabla_\theta\log Z(\theta)\approx\nabla_\theta f_\theta(\mathbf x_{\text{sample}})\),需要 \(\mathbf x_{\text{sample}}\sim p_\theta\)。05:17

上一講的 Metropolis–Hastings MCMC(投影片 3):從 \(\mathbf x^0\sim\pi(\mathbf x)\) 出發(從哪裡出發都可以,例如純雜訊的圖),每步提議 \(\mathbf x' = \mathbf x^t + \text{noise}\);變好就接受,變差就以 \(\exp\{f_\theta(\mathbf x') - f_\theta(\mathbf x^t)\}\) 的機率接受。08:21

為什麼會收斂:細緻平衡(DETAILED BALANCE) $$p_\theta(\mathbf x)\,T_{\mathbf x\to\mathbf x'} = p_\theta(\mathbf x')\,T_{\mathbf x'\to\mathbf x}$$ \(T_{\mathbf x\to\mathbf x'}\) 是從 \(\mathbf x\) 轉移到 \(\mathbf x'\) 的機率。假設 \(p_\theta(\mathbf x')>p_\theta(\mathbf x)\):往上走一定接受,往下走以 \(p_\theta(\mathbf x)/p_\theta(\mathbf x')\) 接受(提議本身是對稱的),代入兩邊就相等。12:37

滿足細緻平衡,\(p_\theta\) 就是這個轉移算子的不動點:如果 \(\mathbf x^t\sim p_\theta\),那 \(\mathbf x^{t+1}\sim p_\theta\)。在一些條件下,不管從哪裡出發都會收斂到它。14:13

為什麼細緻平衡推得出「不動點」?

設 \(\mathbf x^t\sim p_\theta\),則 \(\mathbf x^{t+1}\) 的分佈是 \(\sum_{\mathbf x}p_\theta(\mathbf x)T_{\mathbf x\to\mathbf x'}\)。用細緻平衡把每一項換掉:

$$\sum_{\mathbf x}p_\theta(\mathbf x)T_{\mathbf x\to\mathbf x'} = \sum_{\mathbf x}p_\theta(\mathbf x')T_{\mathbf x'\to\mathbf x} = p_\theta(\mathbf x')\sum_{\mathbf x}T_{\mathbf x'\to\mathbf x} = p_\theta(\mathbf x')$$

最後一步是因為從 \(\mathbf x'\) 出發一定會轉移到某處(包括留在原地),機率總和是 1。細緻平衡是「穩態」的充分條件但非必要;「從任何地方出發都會收斂」還需要鏈是不可約且非週期的(ergodic),在這個高斯提議的設定下通常成立。

實務上:需要非常多步,而且收斂速度隨維度指數變慢。從隨機像素出發一次改一點,要改非常多次才會有正確的結構。就算模型已經訓練好,取樣也很貴;用 contrastive divergence 訓練時,每一步都要做一次,更是貴上加貴。15:17

延伸 補充課 D §4–5 detailed balance 與 Metropolis–Hastings:為什麼 Metropolis 會收斂到 p

02Langevin MCMC(投影片 4)

改良版:提議時不要亂走,而是往能提高機率的方向走。16:19

UNADJUSTED LANGEVIN MCMC
  1. \(\mathbf x^0\sim\pi(\mathbf x)\)
  2. 重複 \(t = 0,1,\dots,T-1\):\(\mathbf z^t\sim\mathcal N(0,I)\), $$\mathbf x^{t+1} = \mathbf x^t + \epsilon\,\nabla_{\mathbf x}\log p_\theta(\mathbf x)\big|_{\mathbf x = \mathbf x^t} + \sqrt{2\epsilon}\,\mathbf z^t$$
當 \(T\to\infty\)、\(\epsilon\to0\) 時,\(\mathbf x^T\) 收斂到 \(p_\theta(\mathbf x)\) 的樣本。
  • 就是帶雜訊的梯度上升:沿 log-likelihood 的梯度走(只適用於連續空間),再加一點高斯雜訊。加雜訊的理由和上一講一樣:不要貪心,要探索。17:20
  • 這個版本永遠接受移動,不做接受/拒絕。18:22
  • 對連續 EBM,\(\nabla_{\mathbf x}\log p_\theta(\mathbf x) = \nabla_{\mathbf x}f_\theta(\mathbf x)\):\(Z(\theta)\) 依賴 \(\theta\) 但不依賴 \(\mathbf x\),對 \(\mathbf x\) 微分就消失了。23:06
  • 維度越高收斂越慢。
課堂問答 · 19:24為什麼雜訊是 \(\sqrt{2\epsilon}\)?梯度項和雜訊項的比例(訊號對雜訊)必須這樣搭配,才能保證收斂到正確的分佈。理論上只在 \(\epsilon\to0\) 時成立,實務上用小的步長並希望它夠好。19:24
課堂問答 · 21:28\(\epsilon\) 不夠小會怎樣?這叫 unadjusted 版本。也可以把「梯度一步加雜訊」當成提議,再像 Metropolis 一樣做接受/拒絕,就是 adjusted 版本(MALA),它在有限步長下也正確。但步長太大時一階 Taylor 展開不準,提議常被拒絕,鏈會卡住。21:28
課堂問答 · 22:00比 Metropolis 快嗎?理論上仍可能需要很多步,但實務上好得多:以前是盲目地改圖,現在有梯度資訊告訴你往哪改。22:00

推論時跑 1,000 甚至 10,000 步(每步評估一次大網路)也許還能忍受;但 contrastive divergence 要在每個訓練步驟都跑一條這樣的鏈,就太貴了。24:07

\(\sqrt{2\epsilon}\) 從哪來?連續時間的 Langevin 方程

Langevin MCMC 是隨機微分方程 \(d\mathbf x = \nabla_{\mathbf x}\log p(\mathbf x)\,dt + \sqrt2\,d\mathbf W_t\) 的 Euler–Maruyama 離散化(步長 \(\epsilon\)):布朗運動在時間 \(\epsilon\) 內的增量是 \(\sqrt\epsilon\,\mathbf z\),乘上 \(\sqrt2\) 就是 \(\sqrt{2\epsilon}\,\mathbf z\)。對應的 Fokker–Planck 方程是

$$\partial_t q = -\nabla\cdot(q\,\nabla\log p) + \Delta q$$

代入 \(q = p\):\(\nabla\cdot(p\nabla\log p) = \nabla\cdot\nabla p = \Delta p\),兩項抵消,所以 \(p\) 是穩態。如果雜訊的係數不是 \(\sqrt2\)(例如 \(\sqrt{2\epsilon}\) 換成 \(\sqrt\epsilon\)),兩項就不會抵消,穩態會變成正比於 \(p^2\) 的分佈(溫度變了)。離散化本身有 \(O(\epsilon)\) 的誤差,這就是 unadjusted 版本的偏差來源。

出處:Lai et al. (2026) §3.1.4 "Langevin Sampling with Score Functions"(離散與連續時間 Langevin);Song & Kingma (2021) §3(MALA 修正)。

延伸 補充課 F §3–4 Langevin、ULA 與 MALA:用 Fokker–Planck 證明穩態是 p,以及步長造成的偏差

03今天:訓練時不要取樣(投影片 5)

Contrastive divergence 是 KL divergence(maximum likelihood)的近似。老招:換一個比較資料和模型的方式,讓 loss 不含 partition function,訓練就快得多。26:12

  • Score matching(diffusion model 的關鍵積木)
  • Noise contrastive estimation
  • Adversarial training(課堂上沒時間講,見第 9 節)

先分清楚兩件事:訓練(找 \(\theta\))與取樣(用 \(\theta\))

EBM 的流程分成先後兩步,各用一種微分:

1. 訓練2. 取樣
目的找 \(\theta\),讓 \(p_\theta\approx p_{\text{data}}\)從學好的 \(p_{\theta^*}\) 產生新樣本
改動的是參數 \(\theta\),用 \(\nabla_\theta\)樣本 \(\mathbf x\),用 \(\nabla_{\mathbf x}\);\(\theta\) 固定
方法MLE(contrastive divergence)、score matching、NCELangevin dynamics 等 MCMC(§2)

Langevin 需要每一點的 score \(\nabla_{\mathbf x}\log p_{\text{data}}(\mathbf x)\),但我們只有資料樣本,沒有 \(p_{\text{data}}\) 的公式,算不出來。所以先用訓練學一個模型 \(\nabla_{\mathbf x}f_\theta(\mathbf x)\) 去逼近它(決定 \(\theta\)),再把學好的 \(\nabla_{\mathbf x}f_{\theta^*}\) 交給 Langevin:

$$\mathbf x_{k+1} = \mathbf x_k + \epsilon\,\nabla_{\mathbf x}f_{\theta^*}(\mathbf x_k) + \sqrt{2\epsilon}\,\mathbf z_k,\qquad\mathbf z_k\sim\mathcal N(0, I)$$

\(\theta\) 決定「Langevin 最後會收斂到哪個分佈」:訓練得越好,取樣得到的分佈越接近資料。本講的三種方法都在處理第 1 步;第 2 步不論用哪種方法訓練,都一樣要跑 MCMC。

為什麼用 KL(MLE)訓練要取樣,用 Fisher divergence 訓練不用?

差別在 \(Z(\theta)\) 會不會留在梯度裡。\(\log p_\theta(\mathbf x) = f_\theta(\mathbf x) - \log Z(\theta)\)。

KL / MLE 對 \(\theta\) 微分,\(\log Z\) 的梯度變成一個對模型分佈的期望:

$$\nabla_\theta\log p_\theta(\mathbf x) = \nabla_\theta f_\theta(\mathbf x) - \underbrace{\mathbb E_{\mathbf x'\sim p_\theta}\big[\nabla_\theta f_\theta(\mathbf x')\big]}_{\nabla_\theta\log Z(\theta)}$$

第二項要從模型自己抽樣本 \(\mathbf x'\),也就是第 11 講 contrastive divergence 的負樣本,只能靠 MCMC,訓練的每一步都要跑。

Fisher divergence 比的是對 \(\mathbf x\) 的梯度,\(Z(\theta)\) 不隨 \(\mathbf x\) 改變,一開始就消失了:

$$\nabla_{\mathbf x}\log p_\theta(\mathbf x) = \nabla_{\mathbf x}f_\theta(\mathbf x) - \underbrace{\nabla_{\mathbf x}\log Z(\theta)}_{=\,0}$$

分部積分之後(§5),loss 只需要在資料點上算 \(f_\theta\) 的一階、二階導數,不需要模型的樣本。

比喻:\(\log p_\theta\) 是一張地形圖,\(-\log Z\) 是海平面。KL 比的是海拔(\(\log p\) 本身),要知道海平面就得看遍整張地圖;Fisher 比的是坡度(\(\nabla_{\mathbf x}\log p\)),海平面整體上下平移不影響坡度,站在資料點上量就夠了。代價是只看局部坡度,看不出分得很開的兩個峰各佔多少比例(§5 最後的盲點、Demo C)。

04Score function(投影片 6)

(STEIN)SCORE FUNCTION $$\mathbf s_\theta(\mathbf x) := \nabla_{\mathbf x}\log p_\theta(\mathbf x) = \nabla_{\mathbf x}f_\theta(\mathbf x) - \underbrace{\nabla_{\mathbf x}\log Z(\theta)}_{=0} = \nabla_{\mathbf x}f_\theta(\mathbf x)$$ 注意是對 \(\mathbf x\) 微分,不是對參數 \(\theta\):問的是「樣本本身改一點,機率怎麼變」。27:44
  • 高斯:\(p_\theta(x) = \frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(x-\mu)^2}{2\sigma^2}}\) \(\Rightarrow\) \(s_\theta(x) = -\frac{x-\mu}{\sigma^2}\)。前面的常數消失了。
  • Gamma:\(p_\theta(x) = \frac{\beta^\alpha}{\Gamma(\alpha)}x^{\alpha-1}e^{-\beta x}\) \(\Rightarrow\) \(s_\theta(x) = \frac{\alpha-1}{x} - \beta\)。討厭的 \(\Gamma(\alpha)\) 也消失了。29:16

投影片 6 的圖:「\(p_\theta(\mathbf x)\) vs. \(\mathbf s_\theta(\mathbf x)\)」。二維平面上畫了兩個高斯混合的等高線(左下角一個大的、右上角一個小的,紅色的同心曲線),上面疊著一整片小黑箭頭,每個箭頭指向該點 log 密度上升最快的方向:都指向兩個高斯的中心。30:20

Likelihood 是純量函數(每個 \(\mathbf x\) 一個高度);score 是向量場(每個 \(\mathbf x\) 一個箭頭)。兩者描述同一個物件,沒有資訊損失:像電位和電場的關係,\(f_\theta\) 是電位,\(\mathbf s_\theta\) 是它的梯度。換成 score 的觀點,只是為了計算上不用管 partition function。33:56

課堂問答 · 33:26score 一定是向量場嗎?依定義它是梯度,所以是向量場,而且是保守場(有一個底層的能量函數)。到了 score-based model,會直接用任意神經網路建模這個向量場,不一定是某個函數的梯度。33:26

延伸 補充課 A §2 梯度與 score:score 是指向高機率區的向量場,Z 在梯度中消失

Demo AScore 場與 Langevin 取樣

兩個高斯的混合(權重 0.3 和 0.7,形狀不同)。灰色等高線是 \(p(\mathbf x)\),箭頭是 \(\nabla_{\mathbf x}\log p(\mathbf x)\)(長度做了截斷)。把 \(p\) 乘上任何常數,箭頭都不變。按「執行」,1,000 個粒子從均勻分佈出發,沿著 score 走並加雜訊。

粒子位置(彩色)vs. 真實密度(等高線)
  • 步長小、步數夠(預設 ε ≈ 0.008、T = 1,000):右上 mode 附近粒子的 x 方向標準差 0.80,和真實分佈的 0.80 一致。粒子分到兩個 mode 的比例是 67%(正確 70%),不完全對:一開始落在哪個 mode 的吸引區域,就很容易一直待在那裡,兩個 mode 之間的低谷很難跨。
  • 步數不夠(同樣 ε,T = 100):標準差 1.09,粒子還沒走到高機率區域就停了,雲比真實分佈散。
  • 步長大:unadjusted 版本每一步都偏離太多,粒子雲比真實分佈更散,這是離散化偏差:ε = 0.3 時標準差 1.02,ε = 0.5 時 2.85,連 mode 的比例都錯了(44%)。切到 MALA,接受/拒絕把偏差修正回來(0.80 和 0.79),代價是接受率降到 55% 和 30%。

05Score matching(投影片 7–11)

觀察:\(\mathbf s_\theta(\mathbf x) = \nabla_{\mathbf x}\log p_\theta(\mathbf x)\) 不依賴 \(Z(\theta)\)。那就用 score 來比較兩個分佈:如果 \(p\) 和 \(q\) 很像,它們的梯度場也該很像。34:27

FISHER DIVERGENCE $$D_F(p,q) := \frac12\,\mathbb E_{\mathbf x\sim p}\big[\Vert\nabla_{\mathbf x}\log p(\mathbf x) - \nabla_{\mathbf x}\log q(\mathbf x)\Vert_2^2\big]$$ 在每個點比較兩個方向向量的差,取平方範數變成純量,再對 \(p\) 取平均。\(p = q\) 時處處為 0。

Score matching:最小化 \(p_{\text{data}}\) 和 EBM \(p_\theta\propto\exp\{f_\theta\}\) 之間的 Fisher divergence:38:08

$$\frac12\,\mathbb E_{\mathbf x\sim p_{\text{data}}}\big[\Vert\nabla_{\mathbf x}\log p_{\text{data}}(\mathbf x) - \mathbf s_\theta(\mathbf x)\Vert_2^2\big] = \frac12\,\mathbb E_{\mathbf x\sim p_{\text{data}}}\big[\Vert\nabla_{\mathbf x}\log p_{\text{data}}(\mathbf x) - \nabla_{\mathbf x}f_\theta(\mathbf x)\Vert_2^2\big]$$

只適用於連續密度。38:40

課堂問答 · 37:07Fisher divergence 和 KL 有關係嗎?有:某種意義上,Fisher divergence 是 KL 的導數。把兩個密度都和高斯雜訊做卷積,KL 對雜訊大小的導數就是 Fisher divergence。但先把它當成另一種散度就好。37:07
Fisher divergence 是 KL 的導數:精確的說法

令 \(p_t = p * \mathcal N(0,tI)\)、\(q_t = q * \mathcal N(0,tI)\)(兩者都加上變異數 \(t\) 的高斯雜訊)。那麼

$$\frac{d}{dt}D_{\mathrm{KL}}(p_t\Vert q_t) = -\frac12\,\mathbb E_{p_t}\big[\Vert\nabla\log p_t - \nabla\log q_t\Vert^2\big] = -D_F(p_t,q_t)$$

這是資訊理論中 de Bruijn 恆等式的推廣(Lyu 2009)。直覺:兩個分佈一起被加雜訊模糊,KL 只會減少(資料處理不等式),減少的速度就是此刻的 Fisher divergence。這個「加不同程度的雜訊」的想法,在下一講的 denoising score matching 和 diffusion model 會變成主角。

出處:Lyu (2009), "Interpretation and Generalization of Score Matching", UAI, arXiv 1205.2629;Song & Kingma (2021) §6.1。

問題:\(\nabla_{\mathbf x}\log p_{\text{data}}\) 未知。解法:分部積分(投影片 8)

先看一維。把平方展開成三項:42:20

$$\begin{aligned} \frac12\mathbb E_{p_{\text{data}}}\big[(\nabla_x\log p_{\text{data}} - \nabla_x\log p_\theta)^2\big] = &\ \textcolor{#2f5fd0}{\frac12\int p_{\text{data}}(\nabla_x\log p_{\text{data}})^2dx} + \textcolor{#2a9a6a}{\frac12\int p_{\text{data}}(\nabla_x\log p_\theta)^2dx}\\ &\ \textcolor{#b3261e}{-\int p_{\text{data}}\,\nabla_x\log p_{\text{data}}\,\nabla_x\log p_\theta\,dx} \end{aligned}$$
  • 藍色項不依賴 \(\theta\),可以忽略(為什麼可以,見下方的補充)。
  • 綠色項只依賴模型,沒問題。
  • 紅色項還有資料的 score,要處理掉。42:54

用 \(\nabla_x\log p_{\text{data}} = \frac{1}{p_{\text{data}}}\nabla_xp_{\text{data}}\),\(p_{\text{data}}\) 約掉,再用分部積分 \(\int f'g = fg - \int g'f\):43:56

$$\begin{aligned} -\int p_{\text{data}}\,\nabla_x\log p_{\text{data}}\,\nabla_x\log p_\theta\,dx &= -\int\nabla_xp_{\text{data}}(x)\,\nabla_x\log p_\theta(x)\,dx\\ &= \underbrace{-p_{\text{data}}(x)\nabla_x\log p_\theta(x)\Big|_{x=-\infty}^{\infty}}_{=0} + \textcolor{#d0622f}{\int p_{\text{data}}(x)\,\nabla_x^2\log p_\theta(x)\,dx} \end{aligned}$$

邊界項為 0 需要假設 \(p_{\text{data}}\) 衰減得夠快:\(x\to\pm\infty\) 時 \(p_{\text{data}}(x)\to0\)。45:32 合起來(投影片 9):

為什麼藍色項可以忽略?式子裡不是在對 \(x\) 微分嗎?

兩層微分的角色不同。\(\nabla_x\) 是在每個點比較兩個分佈的斜率;比完之後外面還有對 \(x\) 的期望,\(x\) 被積分掉了。所以整個目標函數只是 \(\theta\) 的函數:

$$J(\theta) = \frac12\,\mathbb E_{x\sim p_{\text{data}}}\Big[\big(\nabla_x\log p_{\text{data}}(x) - \nabla_x\log p_\theta(x)\big)^2\Big]$$

訓練要找的是 \(\arg\min_\theta J(\theta)\),梯度下降用的是 \(\nabla_\theta J\)。

藍色項是和 \(\theta\) 無關的常數。\(\frac12\int p_{\text{data}}(\nabla_x\log p_{\text{data}})^2dx\) 裡雖然有 \(\nabla_x\),但只牽涉資料分佈,積分完是一個固定的數(資料分佈的 Fisher information)。它對 \(\theta\) 的梯度是 0,也不改變最小值的位置:\(\arg\min_\theta J(\theta) = \arg\min_\theta\big(J(\theta) - c\big)\)。

代價:拿掉它之後,loss 的數值等於「Fisher divergence 減去一個未知的常數」,可以是負的,不能當成兩個分佈之間真正的距離;但用來訓練、比較不同的 \(\theta\) 完全沒問題。第 4 講的 MLE 丟掉資料的熵 \(H(p_{\text{data}})\),是同一個道理。

算得出來的例子:\(p_{\text{data}} = \mathcal N(0, 1)\)、\(p_\theta = \mathcal N(\mu, 1)\),score 分別是 \(-x\) 和 \(-(x - \mu)\),\(\nabla_x^2\log p_\theta = -1\)。

$$\underbrace{\tfrac12\mu^2}_{\text{Fisher divergence}}\qquad\underbrace{\tfrac12}_{\text{藍色項}}\qquad\underbrace{\mathbb E\big[\tfrac12(x - \mu)^2 - 1\big] = \tfrac12\mu^2 - \tfrac12}_{\text{score matching loss}}$$

兩條曲線只差藍色項 \(\tfrac12\),都在 \(\mu = 0\) 取最小。實際訓練用的是樣本平均,它的最小值落在樣本平均 \(\bar x\):

Fisher divergence ½μ²score matching loss ½μ² − ½(差一個常數)用 n 個樣本估計的 loss
一維 SCORE MATCHING $$\frac12\mathbb E_{p_{\text{data}}}\big[(\nabla_x\log p_{\text{data}}(x) - \nabla_x\log p_\theta(x))^2\big] = \mathbb E_{x\sim p_{\text{data}}}\Big[\frac12(\nabla_x\log p_\theta(x))^2 + \nabla_x^2\log p_\theta(x)\Big] + \text{const.}$$
多維 SCORE MATCHING(用 Gauss 定理做分部積分) $$\frac12\mathbb E_{p_{\text{data}}}\big[\Vert\nabla_{\mathbf x}\log p_{\text{data}}(\mathbf x) - \nabla_{\mathbf x}\log p_\theta(\mathbf x)\Vert_2^2\big] = \mathbb E_{\mathbf x\sim p_{\text{data}}}\Big[\frac12\Vert\nabla_{\mathbf x}\log p_\theta(\mathbf x)\Vert_2^2 + \mathrm{tr}\big(\underbrace{\nabla_{\mathbf x}^2\log p_\theta(\mathbf x)}_{\text{Hessian}}\big)\Big] + \text{const.}$$ 48:10

演算法(投影片 10)

  1. 抽一個 mini-batch 的資料 \(\{\mathbf x_1,\dots,\mathbf x_n\}\sim p_{\text{data}}\)。
  2. 用樣本平均估計 loss:\(\frac1n\sum_i\big[\frac12\Vert\nabla_{\mathbf x}f_\theta(\mathbf x_i)\Vert_2^2 + \mathrm{tr}(\nabla_{\mathbf x}^2f_\theta(\mathbf x_i))\big]\)。
  3. 隨機梯度下降。
  4. 不需要從 EBM 取樣!50:20
注意(投影片 10 的 caveat):大模型的 Hessian trace \(\mathrm{tr}(\nabla_{\mathbf x}^2\log p_\theta(\mathbf x))\) 一般非常貴。神經網路沒有它的封閉形式,只能用自動微分;而 trace 需要 \(n\) 個對角元素,等於做 \(n\) 次 backprop,\(n\) 是資料維度。下一講的 denoising score matching(Vincent 2011)和 sliced score matching(Song et al. 2019)解決這個問題。54:58
老師怎麼說 · 52:26這個 loss 很合理。第一項要求資料點上的梯度小:資料點應該是 log-likelihood 的駐點,稍微擾動它不該讓機率大增。第二項(二階導數)要求它是局部最大值而不是最小值(二階導數為負)。所以:選參數讓資料點成為 log-likelihood 的局部最大值。52:26
課堂問答 · 53:58怎麼避開 Hessian?兩條路:sliced score matching,沿隨機方向檢查 likelihood 是升是降,等同於估計矩陣 trace 的 Hutchinson 技巧;denoising score matching,在資料點附近加一點雜訊,檢查 likelihood 是升是降。兩者都能隨維度擴展。53:58
課堂問答 · 56:02它會不會偏好一個又尖又低的峰,而不是平緩但高的峰?不會:剛才證明了它和 Fisher divergence 只差一個常數,而 Fisher divergence 為 0 若且唯若兩個密度相同。無限資料、全域最佳化下,它會給出正確的資料分佈。56:02
為什麼「score 相等」就代表「分佈相等」?以及一個實務上的盲點

若兩個可微函數的一階導數處處相等,它們只差一個常數。對兩個 log 密度來說,歸一化條件又把這個常數固定成 0,所以 score 相同的兩個(處處為正的)密度一定相同。這是 score matching 一致性的根據。

盲點:「處處」是關鍵。Fisher divergence 是對 \(p_{\text{data}}\) 取期望,資料密度幾乎為 0 的地方幾乎不被檢查。如果資料有兩個離得很遠的 mode,中間的低谷幾乎沒有樣本,score matching 就很難知道兩個 mode 的相對權重:每個 mode 內部的 score 和權重無關(權重只是那一區 log 密度的一個常數平移)。有限樣本下,學到的權重可能偏很多。Demo C 可以直接看到這個現象。Song & Ermon (2019) 把它列為直接用 score matching 訓練生成模型的困難之一,並用「加不同程度的雜訊」解決(下一講)。

指數族的封閉解:如果 \(f_\theta(\mathbf x) = \theta^\top\boldsymbol\phi(\mathbf x)\) 對 \(\theta\) 是線性的,score matching 的 loss 是 \(\theta\) 的二次式:\(\frac12\theta^\top A\theta + \theta^\top\mathbf b\),其中 \(A = \mathbb E[\nabla\boldsymbol\phi\nabla\boldsymbol\phi^\top]\)、\(\mathbf b = \mathbb E[\Delta\boldsymbol\phi]\),所以 \(\theta^* = -A^{-1}\mathbf b\),解一個線性方程組就好(Hyvärinen 2007)。Demo C 用的就是這個。

出處:Song & Kingma (2021) §4 開頭(score 相等 ⇒ 分佈相等);Hyvärinen (2005), JMLR;Song & Ermon (2019), "Generative Modeling by Estimating Gradients of the Data Distribution" §3;Yang Song〈Sliced Score Matching〉部落格。

回顧:訓練 EBM 用過的兩種距離(投影片 11)

  • KL divergence = maximum likelihood:\(\nabla_\theta f_\theta(\mathbf x_{\text{data}}) - \nabla_\theta f_\theta(\mathbf x_{\text{sample}})\)(contrastive divergence),需要取樣。
  • Fisher divergence = score matching:\(\frac12\mathbb E_{p_{\text{data}}}[\Vert\nabla_{\mathbf x}\log p_{\text{data}} - \nabla_{\mathbf x}f_\theta\Vert_2^2]\),不需要取樣。

Demo B不算 Hessian 也能估計它的 trace:Hutchinson 估計量

對任何方陣 \(H\),若隨機向量 \(\mathbf v\) 滿足 \(\mathbb E[\mathbf v\mathbf v^\top] = I\)(例如標準高斯,或每個分量隨機取 ±1 的 Rademacher 向量),則 \(\mathbb E[\mathbf v^\top H\mathbf v] = \mathrm{tr}(H)\)。而 \(\mathbf v^\top H\mathbf v\) 可以用一次 Hessian–vector product(約兩次 backprop)算出,不必建出整個 \(H\)。這就是 sliced score matching 的核心。下面是一個隨機的 \(n\times n\) 對稱矩陣,用 \(M\) 個隨機向量的平均估計它的 trace,重複 500 次看估計值的分佈。

500 次估計的直方圖真正的 trace

估計量永遠是不偏的(直方圖以真值為中心),但單一個向量的變異數可能很大;用 \(M\) 個向量平均,標準差按 \(1/\sqrt M\) 下降。Rademacher 向量的變異數比高斯小:它的變異數只來自非對角元素,高斯還多了對角元素的貢獻。這個隨機矩陣的對角線不大,所以差距不明顯(n = 100、M = 1 時標準差 14.1 vs. 15.3;M = 16 時降到 3.4,約是 14.1/4)。代價是:要算 \(M\) 次 Hessian–vector product,而不是 \(n\) 次 backprop。

出處:Hutchinson (1989);Yang Song〈Sliced Score Matching〉部落格(Hessian–vector product 只需 O(1) 次 backprop)。

06Noise contrastive estimation(投影片 12–16)

另一種不用取樣的方法,有點像 GAN:不是拿資料和模型的樣本比較,而是和一個雜訊分佈的樣本比較。57:33

  • 資料分佈 \(p_{\text{data}}(\mathbf x)\)。
  • 雜訊分佈 \(p_n(\mathbf x)\):要能解析地算密度,也要容易取樣。
  • 訓練一個 discriminator \(D_\theta(\mathbf x)\in[0,1]\) 分辨資料和雜訊: $$\max_\theta\ \mathbb E_{\mathbf x\sim p_{\text{data}}}[\log D_\theta(\mathbf x)] + \mathbb E_{\mathbf x\sim p_n}[\log(1-D_\theta(\mathbf x))]$$
  • 最佳 discriminator(第 9 講,「期中考的好複習」):\(D^*_\theta(\mathbf x) = \frac{p_{\text{data}}(\mathbf x)}{p_{\text{data}}(\mathbf x) + p_n(\mathbf x)}\)。59:09

關鍵:用生成模型參數化 discriminator(投影片 13)

不用任意的網路當 discriminator,而是規定它必須這樣算:59:45

$$D_\theta(\mathbf x) = \frac{p_\theta(\mathbf x)}{p_\theta(\mathbf x) + p_n(\mathbf x)}$$

最佳的 discriminator 滿足 \(\frac{p_{\theta^*}}{p_{\theta^*} + p_n} = \frac{p_{\text{data}}}{p_{\text{data}} + p_n}\),所以 \(p_{\theta^*} = p_{\text{data}}\)。分類器只能透過 \(p_\theta\) 做判斷,要分得好就只能讓 \(p_\theta\) 學到資料分佈。這特別適合「只定義到差一個常數」的模型,也就是 EBM。61:22

等價地,\(p_{\theta^*}(\mathbf x) = \frac{p_n(\mathbf x)D^*_\theta(\mathbf x)}{1 - D^*_\theta(\mathbf x)} = p_{\text{data}}(\mathbf x)\):分類器可以用來修正 \(p_n\) 的密度估計。這也能用來改進一個基礎生成模型(Boosted Generative Models,Grover & Ermon 2018)。

把 Z 當成可學的參數(投影片 14)

EBM 的 \(Z(\theta) = \int e^{f_\theta(\mathbf x)}d\mathbf x\) 這個限制很難滿足。解法:用一個額外的可學參數 \(Z\) 取代它,不明確要求 \(Z = \int e^{f_\theta}\):63:26

$$p_{\theta,Z}(\mathbf x) = \frac{e^{f_\theta(\mathbf x)}}{Z}$$

NCE 的最佳解滿足 \(p_{\theta^*,Z^*} = p_{\text{data}}\)。兩邊積分:\(\int\frac{e^{f_{\theta^*}(\mathbf x)}}{Z^*}d\mathbf x = \int p_{\text{data}} = 1\),所以 \(Z^* = \int e^{f_{\theta^*}(\mathbf x)}d\mathbf x\):最佳的 \(Z\) 自動就是正確的 partition function。65:02

Loss 與演算法(投影片 15–16)

代入,分子分母同乘 \(Z\):

$$D_{\theta,Z}(\mathbf x) = \frac{e^{f_\theta(\mathbf x)}/Z}{e^{f_\theta(\mathbf x)}/Z + p_n(\mathbf x)} = \frac{e^{f_\theta(\mathbf x)}}{e^{f_\theta(\mathbf x)} + p_n(\mathbf x)Z}$$
$$\max_{\theta,Z}\ \mathbb E_{\mathbf x\sim p_{\text{data}}}\big[f_\theta(\mathbf x) - \log(e^{f_\theta(\mathbf x)} + Zp_n(\mathbf x))\big] + \mathbb E_{\mathbf x\sim p_n}\big[\log(Zp_n(\mathbf x)) - \log(e^{f_\theta(\mathbf x)} + Zp_n(\mathbf x))\big]$$

數值穩定:\(\log(e^{f_\theta(\mathbf x)} + Zp_n(\mathbf x)) = \mathrm{logsumexp}\big(f_\theta(\mathbf x),\ \log Z + \log p_n(\mathbf x)\big)\)。75:52

NCE 訓練 EBM
  1. 抽資料 \(\mathbf x_1,\dots,\mathbf x_n\sim p_{\text{data}}\)。
  2. 抽雜訊 \(\mathbf y_1,\dots,\mathbf y_n\sim p_n\)。
  3. 估計 NCE loss: $$\frac1n\sum_{i=1}^n\Big[f_\theta(\mathbf x_i) - \mathrm{lse}\big(f_\theta(\mathbf x_i),\log Z + \log p_n(\mathbf x_i)\big) + \log Z + \log p_n(\mathbf y_i) - \mathrm{lse}\big(f_\theta(\mathbf y_i),\log Z + \log p_n(\mathbf y_i)\big)\Big]$$
  4. 對 \(\theta\) 和 \(Z\) 做隨機梯度上升。
  5. 不需要從 EBM 取樣!76:52

(投影片 16 的公式中雜訊項寫成「\(+\log Z + p_n(\mathbf y_i)\)」,少了一個 log,應為 \(\log p_n(\mathbf y_i)\);上面已更正。)

\(Z\) 對 loss 的影響不是單調的:不是越大或越小越好,所以 \(\theta\) 和 \(Z\) 之間有真正的學習。77:24

課堂問答 · 68:07分辨「爛樣本」和雜訊不是很容易嗎,學到的模型怎麼會好?理論上用任何雜訊分佈都行;實務上,雜訊分佈要接近資料分佈,分類器才會被迫學到真實樣本的結構。結果高度依賴雜訊分佈選得好不好。68:07
課堂問答 · 71:17訓練完要怎麼取樣?還是得用 MCMC / Langevin。NCE 只在訓練時省掉取樣,推論時沒有好處。71:17
課堂問答 · 71:47 / 72:49讓 \(Z\) 自由變化公平嗎?只在極限下(無限資料、完美最佳化)\(Z\) 才等於真正的 partition function。實務上得到的 \(Z\) 只是估計,模型也因此次佳。但 \(f_\theta\) 本身仍然定義了一個合法的 EBM,只是它真正的 partition function 不是學到的那個 \(Z\)。71:47
課堂問答 · 74:22NCE 和 score matching 比哪個好?它們其實很相關:如果雜訊分佈是「把資料加一點高斯雜訊」,NCE 就恰好變成 denoising score matching。所以要看雜訊分佈怎麼選。74:22
NCE 就是一個 logistic regression

寫成 logit:\(D_{\theta,Z}(\mathbf x) = \sigma\big(f_\theta(\mathbf x) - \log Z - \log p_n(\mathbf x)\big)\),其中 \(\sigma\) 是 sigmoid。所以 NCE 就是一個 logistic regression:logit 是「模型的未歸一化 log 密度」減去「雜訊的 log 密度」,\(-\log Z\) 就是 bias 項,\(-\log p_n(\mathbf x)\) 是已知的偏移。如果 \(f_\theta(\mathbf x) = \theta^\top\boldsymbol\phi(\mathbf x)\) 對參數是線性的,這個目標對 \((\theta,\log Z)\) 是凹的,梯度上升一定收斂到全域最佳。

更一般地,Gutmann & Hyvärinen 允許雜訊樣本數是資料的 \(\nu\) 倍;雜訊越多、雜訊分佈越接近資料,估計的變異數越小。Gutmann & Hyvärinen (2012) 也證明,取「把資料平移一個小量 \(\mathbf v\)」當雜訊,並把分類器寫成 \(p_\theta(\mathbf x-\mathbf v)/(p_\theta(\mathbf x)+p_\theta(\mathbf x-\mathbf v))\),\(\Vert\mathbf v\Vert\to0\) 時,NCE 的目標在 Taylor 展開下等於沿方向 \(\mathbf v\) 的 score matching,再對 \(\mathbf v\) 的方向取平均,就是 sliced score matching。

出處:Gutmann & Hyvärinen (2010), AISTATS;Song & Kingma (2021) §5 與 §5.1 "Connection to Score Matching"(TeX 原始檔)。

Demo C同一個 EBM,三種訓練方法

資料是一維的雙峰分佈(左峰權重 0.3、右峰 0.7,各自標準差 0.5),2,000 個樣本。模型是對參數線性的 EBM:\(f_\theta(x) = \theta_1x + \theta_2x^2 + \sum_{k=1}^{16}\theta_{k+2}\,e^{-(x-c_k)^2/(2\cdot0.6^2)}\)(16 個均勻排在 \([-5,5]\) 的高斯凸塊)。三種訓練方法:

  • Score matching:loss 是 \(\theta\) 的二次式,直接解線性方程組(加一點 ridge)。
  • NCE:雜訊分佈是高斯,對 \((\theta,\log Z)\) 做 1,500 步 Adam。
  • MLE(對照組):一維才能做到,在格點上精確算 \(Z(\theta)\) 和 \(\mathbb E_{p_\theta}[\boldsymbol\phi]\),做 1,500 步梯度上升。
NCE 的雜訊分佈
真正的 p_datascore matchingNCEMLENCE 的雜訊分佈(虛線)

以下數字是用這頁同一份程式在 Node.js 上跑種子 1–6 的結果(頁面上第一次按「訓練」用的是種子 1,數字相同;「重新抽資料」會隨機換種子,畫面上會顯示種子編號):

  • 兩峰靠近(距離 2):score matching 和 MLE 在 6 個種子都很好,左峰質量分別是 0.30–0.32 和 0.30–0.32(正確 0.3)。NCE 在 5 個種子是 0.30–0.33,但種子 2 失敗:左峰質量 0.98,學到的 \(\log Z = 0.54\),同一個 \(f_\theta\) 真正的 \(\log Z = 3.91\)。原因是兩峰靠近時資料很集中,對齊資料的雜訊高斯也很窄,最左邊、中心在 \(x = -5\) 的那個高斯凸塊附近既沒有資料、也幾乎沒有雜訊樣本,它的權重幾乎不受約束。Adam 會把極小的梯度放大成正常大小的步伐,這個權重一路漂高,模型就在資料以外的地方放了大部分質量(另一個隨機種子 267 也一樣,權重漂到 14,\(x<-3\) 的質量占 64%)。這和下面「雜訊太窄」是同一個教訓:NCE 只約束雜訊樣本到得了的地方。
  • 兩峰分開(距離 6):MLE 0.28–0.32、NCE 0.28–0.33,都正確;score matching 卻是 0.19–0.61,隨種子亂跳(距離 4 時也一樣,0.05–0.59)。每個峰的「形狀」它學得很好,但兩峰的相對權重幾乎沒被約束:這正是上面補充框說的盲點。
  • NCE 的雜訊(距離 4,種子 1–6):對齊資料的高斯,左峰質量 0.28–0.32,學到的 \(\log Z\) 和真值差不到 0.05;很寬的高斯 \(\mathcal N(0,6^2)\),左峰質量 0.26–0.32,在這個一維例子裡差別不大,只是 \(\log Z\) 的誤差多半較大(0.01–0.10;對齊資料時都不到 0.05),因為大部分雜訊樣本離資料很遠、很容易分辨,提供的資訊較少;太窄的高斯 \(\mathcal N(0,0.6^2)\)(沒涵蓋資料的範圍)完全失敗:資料所在而雜訊沒到的地方完全沒被約束,\(f_\theta\) 在那裡任意長大,種子 1 的真正 \(\log Z\) 變成 33,學到的只有 1.8,test log-likelihood 掉到 −31。雜訊分佈必須涵蓋資料(以及模型可能放質量的地方),而且越接近越好。

07NCE vs. GAN(投影片 17)

相同
  • 都訓練一個 discriminator 做二元分類,用 cross-entropy loss。
  • 都是 likelihood-free(EBM 的 likelihood 本來就算不出來)。
不同
  • GAN 需要對抗訓練(minimax),NCE 不需要:雜訊分佈固定,只對 \(\theta,Z\) 做最大化,非凸但沒有 minimax、沒有不穩定性。
  • NCE 需要能算雜訊分佈的 likelihood;GAN 只要能從 generator 取樣。
  • NCE 訓練出一個 EBM(從 discriminator 取出 \(f_\theta\));GAN 訓練出一個確定性的樣本生成器。

77:54

08Flow contrastive estimation(投影片 18–19)

觀察:雜訊分佈要同時能算機率和有效取樣;而且我們希望分類問題盡量難,也就是 \(p_n\) 接近 \(p_{\text{data}}\)(但不完全相同)。那就邊訓練邊調整雜訊分佈。79:57

  • 用 normalizing flow \(p_{n,\phi}(\mathbf x)\) 參數化雜訊分佈:flow 正好兩個要求都滿足。
  • discriminator 變成 \(D_{\theta,Z,\phi}(\mathbf x) = \frac{e^{f_\theta(\mathbf x)}}{e^{f_\theta(\mathbf x)} + p_{n,\phi}(\mathbf x)Z}\)。
  • 訓練 flow 去混淆 discriminator,等於最小化 \(D_{\mathrm{JS}}(p_{\text{data}},p_{n,\phi})\): $$\min_\phi\max_{\theta,Z}\ \mathbb E_{\mathbf x\sim p_{\text{data}}}[\log D_{\theta,Z,\phi}(\mathbf x)] + \mathbb E_{\mathbf x\sim p_{n,\phi}}[\log(1 - D_{\theta,Z,\phi}(\mathbf x))]$$ 這又回到了 GAN 式的 minimax:雜訊分佈是一個被對抗訓練的 flow。81:28

投影片 19 的圖(Gao et al. 2020):三格樣本,分別是 SVHN 門牌數字、CIFAR-10 的小物體、CelebA 人臉,都是 32×32 左右的小圖。

課堂問答 · 81:59最後用的是 flow 還是 EBM?這些樣本是 flow 產生的。技術上兩個都得到:一個 EBM 和一個 flow,論文也顯示某些任務用 EBM 比較好。81:59

09時間不夠沒講到的投影片:EBM 的對抗式訓練與總結(投影片 20–21)

影片在 flow contrastive estimation 的樣本之後結束;第 13 講直接開始講 score-based model,沒有回頭講這兩張。以下是整理。

對抗式訓練(投影片 20)

引入一個變分分佈 \(q_\phi(\mathbf x)\),用 Jensen 不等式給 log-likelihood 一個上界:

$$\begin{aligned} \mathbb E_{\mathbf x\sim p_{\text{data}}}[\log p_\theta(\mathbf x)] &= \mathbb E_{p_{\text{data}}}[f_\theta(\mathbf x)] - \log\int e^{f_\theta(\mathbf x)}d\mathbf x = \mathbb E_{p_{\text{data}}}[f_\theta(\mathbf x)] - \log\int q_\phi(\mathbf x)\frac{e^{f_\theta(\mathbf x)}}{q_\phi(\mathbf x)}d\mathbf x\\ &\le\mathbb E_{p_{\text{data}}}[f_\theta(\mathbf x)] - \int q_\phi(\mathbf x)\log\frac{e^{f_\theta(\mathbf x)}}{q_\phi(\mathbf x)}d\mathbf x = \mathbb E_{p_{\text{data}}}[f_\theta(\mathbf x)] - \mathbb E_{q_\phi}[f_\theta(\mathbf x)] - H(q_\phi(\mathbf x)) \end{aligned}$$

(\(\log\) 是凹函數,\(\log\mathbb E\ge\mathbb E\log\),前面有負號所以變成上界。這和 ELBO 用的是同一個不等式,方向因為負號而相反。)先對 \(q_\phi\) 最小化這個上界讓它貼近真正的 likelihood,再對 \(\theta\) 最大化:

對抗式訓練$$\max_\theta\min_\phi\ \mathbb E_{\mathbf x\sim p_{\text{data}}}[f_\theta(\mathbf x)] - \mathbb E_{\mathbf x\sim q_\phi}[f_\theta(\mathbf x)] - H(q_\phi(\mathbf x))$$

投影片最後問:\(q_\phi\) 需要什麼性質?答案:要能快速取樣(估計 \(\mathbb E_{q_\phi}[f_\theta]\)),也要能算熵 \(H(q_\phi)\)。所以通常選可逆模型,例如 IAF、NICE/RealNVP(第 8 講)。最內層 \(\min_\phi\) 等價於 \(\min_\phi D_{\mathrm{KL}}(q_\phi\Vert p_\theta)\):讓 \(q_\phi\) 去近似 EBM 本身,等於「學一個快速的取樣器」來取代 MCMC。注意 \(f_\theta\) 那兩項的結構正是 contrastive divergence 的梯度(資料項減樣本項),只是樣本改由 \(q_\phi\) 提供。

出處:Song & Kingma (2021) §6.3 "Adversarial Training"(TeX 原始檔),其中也介紹了用 GAN 式的 generator 當 \(q_\phi\)、但熵算不出來時的各種近似。

總結(投影片 21)

  • EBM 是非常有彈性的機率模型,但 partition function 算不出來。
  • 取樣很難,通常需要迭代的 MCMC。
  • 算 likelihood 很難;比較兩個點的機率很容易。
  • Maximum likelihood 訓練用 contrastive divergence,每個訓練步驟都要取樣。
  • 不用取樣的訓練:score matching。
  • 不用取樣的訓練:noise contrastive estimation,還附帶給出 partition function 的估計。
  • 不用取樣的訓練:對抗式最佳化。
  • 參考:Yang Song & Durk Kingma,〈How to Train Your Energy-Based Models〉。

常見誤解

誤解:「score 是 log-likelihood 對參數的梯度」。這裡的 score 是對輸入 \(\mathbf x\) 的梯度。統計學裡的 Fisher score 是對參數的梯度,名字一樣但是不同的東西。
誤解:「score matching 需要知道資料的 score」。原始的 Fisher divergence 需要,但分部積分後的等價形式只含模型的一階和二階導數,資料只出現在期望裡。
誤解:「score matching 是在跑 Langevin 取樣時做的」。兩者有先後:先用 score matching 訓練出 \(\theta^*\)(只用資料,不跑 Langevin),再把學好的 \(\nabla_{\mathbf x}f_{\theta^*}\) 交給 Langevin 取樣(§3)。
誤解:「score matching loss 的數值就是 Fisher divergence」。兩者差一個和 \(\theta\) 無關的常數(藍色項),loss 可以是負的;它只適合用來找最好的 \(\theta\),不能當成分佈之間的距離(§5 的補充)。
誤解:「score matching 和 NCE 讓取樣也變快了」。它們只在訓練時不需要取樣;訓練完要產生樣本,仍然得跑 MCMC 或 Langevin。
誤解:「NCE 學到的 Z 一定是正確的 partition function」。只在無限資料、完美最佳化時成立;實務上只是估計,品質取決於雜訊分佈(Demo C)。
誤解:「unadjusted Langevin 在任何步長下都正確」。只在 \(\epsilon\to0\) 時收斂到正確分佈;有限步長會有偏差,要用 MALA(加接受/拒絕)修正(Demo A)。
誤解:「Fisher divergence 為 0 若且唯若分佈相同,所以有限樣本下 score matching 也能精確學到 mode 的權重」。理論的保證是「處處」相等;資料稀少的區域幾乎不受約束,分得很開的 mode 之間的權重可能學錯(Demo C)。

自我測驗

1. 用細緻平衡證明 \(p_\theta\) 是 Metropolis 鏈的不動點。
\(\sum_{\mathbf x}p(\mathbf x)T_{\mathbf x\to\mathbf x'} = \sum_{\mathbf x}p(\mathbf x')T_{\mathbf x'\to\mathbf x} = p(\mathbf x')\sum_{\mathbf x}T_{\mathbf x'\to\mathbf x} = p(\mathbf x')\)。
2. 寫出 unadjusted Langevin 的更新式。為什麼它對 EBM 很方便?
\(\mathbf x^{t+1} = \mathbf x^t + \epsilon\nabla_{\mathbf x}\log p_\theta(\mathbf x^t) + \sqrt{2\epsilon}\,\mathbf z^t\)。因為 \(\nabla_{\mathbf x}\log p_\theta = \nabla_{\mathbf x}f_\theta\),不需要 \(Z\)。
3. 為什麼 contrastive divergence 加上 Langevin 在實務上還是太貴?
每個訓練步驟都要跑一條可能上千步的 Langevin 鏈,每步都要評估一次大網路並對 \(\mathbf x\) 做 backprop。
4. 算出 gamma 分佈的 score。
\(\log p = \alpha\log\beta - \log\Gamma(\alpha) + (\alpha-1)\log x - \beta x\),對 \(x\) 微分得 \(\frac{\alpha-1}{x} - \beta\)。
5. 一維 score matching 的分部積分中,哪一項被換掉?需要什麼假設?
交叉項 \(-\int p_{\text{data}}\nabla\log p_{\text{data}}\nabla\log p_\theta\) 換成 \(\int p_{\text{data}}\nabla^2\log p_\theta\)。需要邊界項 \(p_{\text{data}}(x)\nabla_x\log p_\theta(x)\) 在 \(\pm\infty\) 處為 0,也就是資料密度衰減得夠快。
6. 多維 score matching 的 loss 是什麼?瓶頸在哪?
\(\mathbb E_{p_{\text{data}}}[\frac12\Vert\nabla_{\mathbf x}f_\theta\Vert^2 + \mathrm{tr}(\nabla^2_{\mathbf x}f_\theta)]\)。Hessian 的 trace 需要 \(n\) 次 backprop,\(n\) 是資料維度。
7. 用「局部最大值」解釋 score matching 的兩項。
\(\Vert\nabla f\Vert^2\) 要求資料點是駐點(梯度小);\(\mathrm{tr}(\nabla^2f)\) 要求曲率為負,是最大值而非最小值。
8. Hutchinson 估計量是什麼?為什麼它能幫 score matching 擴展到高維?
\(\mathrm{tr}(H) = \mathbb E[\mathbf v^\top H\mathbf v]\),\(\mathbb E[\mathbf v\mathbf v^\top] = I\)。\(\mathbf v^\top H\mathbf v\) 只需一次 Hessian–vector product(O(1) 次 backprop),不隨維度增加。
9. NCE 中,為什麼把 discriminator 寫成 \(p_\theta/(p_\theta+p_n)\) 就能學到 \(p_{\text{data}}\)?
最佳 discriminator 必須等於 \(p_{\text{data}}/(p_{\text{data}}+p_n)\);分類器只能透過 \(p_\theta\) 表達,所以最佳時 \(p_\theta = p_{\text{data}}\)。
10. 為什麼可學的 \(Z\) 在最佳解會等於真正的 partition function?
最佳時 \(e^{f_{\theta^*}}/Z^* = p_{\text{data}}\),兩邊積分得 \(\int e^{f_{\theta^*}}/Z^* = 1\),所以 \(Z^* = \int e^{f_{\theta^*}}\)。
11. 雜訊分佈需要哪些性質?怎麼選比較好?
能算密度、能有效取樣、涵蓋資料的範圍。越接近資料分佈(但不相同),分類問題越難,學到的模型越好。
12. 列出 NCE 和 GAN 的三個差別。
NCE 沒有 minimax(雜訊固定);NCE 需要雜訊的 likelihood,GAN 只需要取樣;NCE 訓練出 EBM,GAN 訓練出確定性的生成器。
13. 投影片 20 的不等式為什麼是上界?\(q_\phi\) 需要什麼?
\(\log\int q\frac{e^f}{q}\ge\int q\log\frac{e^f}{q}\)(Jensen),前面有負號所以 log-likelihood 被上界。\(q_\phi\) 要能快速取樣並算熵,例如 flow。
14. 藍色項 \(\frac12\mathbb E_{p_{\text{data}}}\big[(\nabla_x\log p_{\text{data}})^2\big]\) 裡明明有 \(\nabla_x\),為什麼訓練時可以丟掉?丟掉後 loss 有什麼變化?
\(\nabla_x\) 只是在每個點比斜率,外面對 \(x\) 取期望後,目標函數只是 \(\theta\) 的函數。藍色項只牽涉 \(p_{\text{data}}\),是常數,對 \(\theta\) 的梯度為 0,不改變 \(\arg\min_\theta\)。丟掉後 loss 等於 Fisher divergence 減去這個常數,可以是負的(高斯例子:\(\tfrac12\mu^2-\tfrac12\))。
15. 為什麼 MLE 訓練 EBM 每一步都要從模型取樣,score matching 不用?兩種方法訓練完,產生新樣本時各需要什麼?
MLE 的梯度含 \(\nabla_\theta\log Z(\theta) = \mathbb E_{p_\theta}[\nabla_\theta f_\theta]\),要模型的樣本;score matching 比的是 \(\nabla_{\mathbf x}\log p_\theta\),而 \(\nabla_{\mathbf x}\log Z = 0\),只需在資料點上算 \(f_\theta\) 的導數。訓練完兩者都一樣要用 MCMC(例如 Langevin,用 \(\nabla_{\mathbf x}f_{\theta^*}\))產生樣本。

延伸閱讀