棒棒生

讓學習變成一種習慣


  • 首頁

  • 分類

  • 關於

  • 歸檔

  • 標籤

Score Matching 系列 (五) SM 加上 Langevin Dynamics 變成生成模型

發表於 2022-03-26 | 分類於 ML

主要內容為這篇文章 “Generative Modeling by Estimating Gradients of the Data Distribution“

背景知識


Score-based generative modeling 的兩個核心概念:

  1. Score matching (SM):
    使用 score matching loss 讓 NN 直接學 score function: $\nabla_x\log p_{data}(x)$, 其中 $p_{data}(x)$ 為 data p.d.f.
    因此我們有一個 NN: $s_\theta(x)\approx \nabla_x\log p_{data}(x)$
    Score matching 在做什麼, 請參考系列文章:
    • Score Matching 系列 (一) Non-normalized 模型估計
    • Score Matching 系列 (二) Denoising Score Matching (DSM) 改善效率並可 Scalable
    • Score Matching 系列 (三) Sliced Score Matching (SSM) 同時保持效率和效果
    • Score Matching 系列 (四) SM 的 Toy Example in PyTorch
  2. Langevin dynamics:
    Langevin dynamics 可以使用 score function, i.e. $\nabla_x\log p_{data}(x)$, 來取 sample, 取出來的 sample 具有 $p_{data}(x)$ 的分佈
    而我們已經用一個 NN $s_\theta(x)$ 來逼近 score function 了, 因此可以用 $s_\theta(x)$ 來取 sample, 步驟如下:
    給定一個固定的 step size $\epsilon>0$, initial value $z=\tilde{x}_0\sim\pi(x)$, 其中 $\pi(x)$ 是固定的 prior distribution, and $z_t\sim\mathcal{N}(0,I)$

    $$\tilde{x}_t = \tilde{x}_{t-1}+\frac{\epsilon}{2}\nabla_x\log p_{data}(\tilde{x}_{t-1})+\sqrt{\epsilon}z_t \\ \approx \tilde{x}_{t-1}+\frac{\epsilon}{2}s_\theta(\tilde{x}_{t-1})+\sqrt{\epsilon}z_t$$ 當 $\epsilon\rightarrow 0$, $T\rightarrow\infty$ 則 $\tilde{x}_T$ 等同於從 $p_{data}(x)$ 取樣!
    我們在這篇文章 忘記物理也要搞懂的 Hamiltonian Monte Carlo (HMC) 筆記
    非常仔細得解釋了為什麼可以這麼做, 注意到其實這裡涉及了很多知識, 依序包含 MCMC, Metropolis Hastings, Hamiltonian Dynamic, 最後才關聯到 Langevin Dynamics. 不過只想單純使用的話, 上面的描述就足夠.

藉由 Score Matching + Langevin Dynamics (SMLD), 我們發現如果成功學到 score function, 則可以從 random noise $z$ 產生符合 data distribution 的 sample.
而這正是 generative model 在做的事情, 此種方法論文稱為 SMLD
但是直接訓練出來的 SMLD 在真實資料上會有兩個問題導致做不好, 接著論文裡說明是什麼原因以及解決方法

閱讀全文 »

Score Matching 系列 (四) SM 的 Toy Example in PyTorch

發表於 2022-03-26 | 分類於 ML

看了一些 score matching 的論文後, 就在網路上找了一下有沒有範例, 然後找到了這個 repo: [toy_gradlogp]
非常清晰易懂, 重點實作了:

  • Denoising Score Matching (DSM)
  • Deep Energy Estimator Networks (DEEN, 我怎麼覺得跟 DSM 等價?!)
  • Sliced Score Matching (SSM)
  • Sliced Score Matching with Variance Reduction (SSM-VR)
  • Langevin Dynamics (可以只根據 score function 取 sample)

雖然主要都是 PyTorch, 但 data pipeline 仍然使用 tensorflow
因此我就改寫了一下, 變成純 PyTorch, 並且也改成我習慣的資料夾結構和 config 使用 Hydra

改寫後的在這: [Score Matching Practicing in PyTorch]

以下接著說明兩個重點:

  1. Langevin Dynamics 簡介
  2. 怎麼把 gradient 也當成 loss?
閱讀全文 »

Score Matching 系列 (三) Sliced Score Matching (SSM) 同時保持效率和效果

發表於 2022-03-06 | 分類於 ML

這是一篇論文筆記: “Sliced-Score-Matching-A-Scalable-Approach-to-Density-and-Score-Estimation”
建議看本文前請先參前兩篇: Score Matching 系列 (一) 和 Score Matching 系列 (二)

雖然 DSM (文章在系列二) 比起 SM 可以非常有效率的訓練, 但最多只能還原到 noisy 的分布, 且加噪的強度不易調整.
本篇 SSM or SSM-VR 則不會有此缺點, 且效果可以接近原來的 SM.

閱讀全文 »

Score Matching 系列 (二) Denoising Score Matching (DSM) 改善效率並可 Scalable

發表於 2022-03-06 | 分類於 ML

這是一篇論文筆記: “A Connection Between Score Matching and Denoising Autoencoders”
建議看本文前請先參前一篇: Score Matching 系列 (一) Non-normalized 模型估計

前言

基於 Score Matching, 提出 Denoising Score Matching (DSM) 的目標函式, 好處是在 energy-based model 下:

  1. 不用將 score function 的 gradients 也納入 loss 去計算 (避免二次微分做 backprop 提高效率)
  2. 當 input $x$ 的維度很大也沒問題 (可以 scalable)

但缺點是:

  1. 最多只能學到加 noise 後的分布
  2. 加 noise 的 level 不好調整

這兩個缺點在下一篇 Sliced Score Matching (SSM) 可以得到改善
這篇論文最後也點出了 denoising autoencoder 跟 score matching 的關係 (實際上就是 DSM loss)

以下正文開始

閱讀全文 »

Score Matching 系列 (一) Non-normalized 模型估計

發表於 2022-01-08 | 分類於 ML

這是一篇論文筆記: “Estimation of Non-Normalized Statistical Models by Score Matching”, 其實推薦直接讀論文, 數學式很清楚, 表達也明確, 只是想順著自己的說法做一下筆記

動機介紹

在 Machine Learning 中, 我們常常希望用參數 $\theta$ 估出來的 pdf $p(.;\theta)$ 能跟真實 data (training data) 的 pdf $p_x(.)$ 愈像愈好.
由於是 pdf $p(.;\theta)$, 必須滿足機率形式, i.e. 積分所有 outcomes 等於 1, 因此引入一個 normalization term $Z(\theta)$
$$p(\xi;\theta)=\frac{1}{Z(\theta)}q(\xi;\theta)$$
其中 $\xi\in\mathbb{R}^n$ 為一個 data point
假設我們有 $T$ 個 observations $\{x_1,...,x_T\}$, 套用 empirical expecation 並對 likelihood estimation 找出最佳 $\theta$ (MLE):
$$\theta_{mle}=\arg\max_\theta \sum_{t=1}^T \log p(x_t;\theta)$$
計算 gradient, 會發現由於存在 $Z(\theta)$ 變得很難計算, 導致 gradient-based optimization 也很困難.

山不轉路轉, 如果我們能換個想法:

閱讀全文 »

Score Function and Fisher Information Matrix

發表於 2022-01-07 | 分類於 ML

Bayesian statistics 視 dataset $\mathcal{D}=\{x_1,...,x_n\}$ 為固定, 而 model parameter $\theta$ 為 random variables, 透過假設 prior $p(\theta)$, 利用 Bayes rule 可得 posterior $p(\theta|\mathcal{D})$. 而估計的參數就是 posterior 的 mode, i.e. $\theta_{map}$ (Maximum A Posterior, MAP)

關於 Fisher information matrix 在 Bayesian 觀點的用途, 其中一個為幫助定義一個特殊的 prior distribution (Jeffreys prior), 使得如果 parameter $\theta$ 重新定義成 $\phi$, 例如 $\phi=f(\theta)$, 則 MAP 解不會改變. 關於這部分還請參考 Machine Learning: a Probabilistic Perspective by Kevin Patrick Murphy. Chapters 5 的 Figure 5.2 圖很清楚

反之 Frequentist statistics 視 dataset $\mathcal{D}=\{X_1,...,X_n\}$ 為 random variables, 透過真實 $\theta^*$ 採樣出 $K$ 組 datasets, 每一組 dataset $\mathcal{D}^k$ 都可以求得一個 $\theta_{mle}^k$ (MLE 表示 Maximum Likelihood Estimator), 則 $\theta_{mle}^k$ 跟 $\theta^*$ 的關係可藉由 Fisher information matrix 看出來

本文探討 score function 和 Fisher information matrix 的定義, 重點會放在怎麼直觀理解. 然後會說明 Fisher information matrix 在 Frequentist statistics 角度代表什麼意義.

先定義一波

閱讀全文 »

Stochastic Processes Week 8 Lévy processes

發表於 2021-12-12 | 分類於 SP

Coursera Stochastic Processes 課程筆記, 共十篇:

  • Week 0: 一些預備知識
  • Week 1: Introduction & Renewal processes
  • Week 2: Poisson Processes
  • Week3: Markov Chains
  • Week 4: Gaussian Processes
  • Week 5: Stationarity and Linear filters
  • Week 6: Ergodicity, differentiability, continuity
  • Week 7: Stochastic integration & Itô formula
  • Week 8: Lévy processes (本文)
  • 整理隨機過程的連續性、微分、積分和Brownian Motion
閱讀全文 »

Stochastic Processes Week 7 Stochastic integration & Itô formula

發表於 2021-12-12 | 分類於 SP

Coursera Stochastic Processes 課程筆記, 共十篇:

  • Week 0: 一些預備知識
  • Week 1: Introduction & Renewal processes
  • Week 2: Poisson Processes
  • Week3: Markov Chains
  • Week 4: Gaussian Processes
  • Week 5: Stationarity and Linear filters
  • Week 6: Ergodicity, differentiability, continuity
  • Week 7: Stochastic integration & Itô formula (本文)
  • Week 8: Lévy processes
  • 整理隨機過程的連續性、微分、積分和Brownian Motion
閱讀全文 »

Stochastic Processes Week 6 Ergodicity, differentiability, continuity

發表於 2021-12-12 | 分類於 SP

Coursera Stochastic Processes 課程筆記, 共十篇:

  • Week 0: 一些預備知識
  • Week 1: Introduction & Renewal processes
  • Week 2: Poisson Processes
  • Week3: Markov Chains
  • Week 4: Gaussian Processes
  • Week 5: Stationarity and Linear filters
  • Week 6: Ergodicity, differentiability, continuity (本文)
  • Week 7: Stochastic integration & Itô formula
  • Week 8: Lévy processes
  • 整理隨機過程的連續性、微分、積分和Brownian Motion
閱讀全文 »

Stochastic Processes Week 5 Stationarity and Linear filters

發表於 2021-12-12 | 分類於 SP

Coursera Stochastic Processes 課程筆記, 共十篇:

  • Week 0: 一些預備知識
  • Week 1: Introduction & Renewal processes
  • Week 2: Poisson Processes
  • Week3: Markov Chains
  • Week 4: Gaussian Processes
  • Week 5: Stationarity and Linear filters (本文)
  • Week 6: Ergodicity, differentiability, continuity
  • Week 7: Stochastic integration & Itô formula
  • Week 8: Lévy processes
  • 整理隨機過程的連續性、微分、積分和Brownian Motion
閱讀全文 »
1…456…12
Chih-Sheng Chen

Chih-Sheng Chen

112 文章
5 分類
231 標籤
© 2025 Chih-Sheng Chen
由 Hexo 強力驅動
主題 - NexT.Mist
[object Object] [object Object]