棒棒生

讓學習變成一種習慣


  • 首頁

  • 分類

  • 關於

  • 歸檔

  • 標籤

Why Stochastic Weight Averaging? averaging results V.S. averaging weights

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

由以前這篇文章知道, 對多顆不同 models 的結果取平均通常會得到更好的結果.
但如果對 models 的參數先取平均呢? 一樣會好嗎?
Stochastic Weight Averaging (SWA) 的這篇文章 “Averaging Weights Leads to Wider Optima and Better Generalization“ 嘗試說明這是有效的.
而實務上, PyTorch 和 PyTorch Lightning 也已經直接導入了 SWA 的 API. 甚至在語音辨識業界裡, 有取代 Kaldi 勢頭的 WeNet 裡面也有類似的機制.

本文直接截圖自己的 slides 內容, 而 Pdf 檔案可參考 這裡

投影片內容


直接上圖:

閱讀全文 »

SGD 泛化能力的筆記

發表於 2022-05-28 | 分類於 Optimization

Sharp V.S. Flat Local Minimum 的泛化能力

先簡單介紹這篇文章:
On large-batch training for deep learning: Generalization gap and sharp minima
考慮下圖兩個 minimum, 對於 training loss 來說其 losses 一樣.
從圖可以容易理解到, 如果找到太 sharp 的點, 由於 test and train 的 mismatch, 會導致測試的時候 data 一點偏移就會對 model output 影響很大.
論文用實驗的方式, 去評量一個 local minimum 的 sharpness 程度, 簡單說利用 random perturb 到附近其他點, 然後看看該點 loss 變化的程度如何, 變化愈大, 代表該 local minimum 可能愈 sharp.
然後找兩個 local minimums, 一個估出來比較 sharp 另一個比較 flat. 接著對這兩點連成的線, 線上的參數值對應的 loss 劃出圖來, 長相如下:
這也是目前一個普遍的認知: flat 的 local minimum 泛化能力較好.
所以可以想像, step size (learning rate) 如果愈大, 愈有可能跳出 sharp minimum.
而 batch size 愈小, 表示 gradient 因為 mini-batch 造成的 noise 愈大, 相當於愈有可能”亂跑”跑出 sharp minimum.
但這篇文章僅止於實驗性質上的驗證. Step size and batch size 對於泛化能力, 或是說對於找到比較 flat optimum 的機率會不會比較高? 兩者有什麼關聯呢?
DeepMind 的近期 (2021) 兩篇文章給出了很漂亮的理論分析.

閱讀全文 »

Numerical Methods for Ordinary Differential Equations

發表於 2022-05-15 | 分類於 ML

如果對於 Differential Equation 完全沒概念, 建議先看以下兩分鐘的影片
 - Solving Differential Equations vs. Solving Algebraic Equations
主要筆記了 Prof. Jeffrey Chasnov 在 Coursera 的兩門課 針對 numerical solution 解 ODE 的內容:
 1. Differential Equations for Engineers
 2. Numerical Methods for Engineers
本文介紹:
 1️⃣ Introduction to ODE: linear? ordinary? n-th order?
 2️⃣ Euler Method: 雖然簡單, 但 error 很大
 3️⃣ Modified Euler Method: error $O(\Delta t^3)$, 比 Euler method 小了一個 order
 4️⃣ Runge Kutta Methods: Modified Euler 方法是 Second-order RK 的一個特例
 5️⃣ Higher-order Runge-Kutta Methods: $n$-th order RK 的 error 為 $O(\Delta t^{n+1})$
 6️⃣ Higher-order ODEs and Systems: 以上都只介紹 first-order ODE 逼近法, 那 higher-order ODE 怎解?

👏 那兩門課的講義教授很佛心得都有附上:
Lecture notes: Differential Equations for Engineers
Lecture notes: Numerical Methods for Engineers

閱讀全文 »

忘記物理也要搞懂的 Hamiltonian Monte Carlo (HMC) 筆記

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

  • 2024/07/28 更新 (見本文最後一段): 補充與 Langevin Dynamics 的關係, 這是我們在 [Score Matching 系列 (五) SM 加上 Langevin Dynamics 變成生成模型] 裡提到一旦訓練出 score function 後, 模型使用的採樣技術. 另外 Score Match + Langevin Dynamics (SMLD) 這種生成模型事實上跟 DDPM (Denoising Diffusion Probabilistic Models) 是一樣的! Yang Song 這篇 2021 ICLR best paper award (Score-Based Generative Modeling through Stochastic Differential Equations) 闡明了 SMLD 跟 DDPM 其實是兩種不同的觀點, 都可以用相同的 SDE (Stochastic Differential Equation) 來表達.

先說我物理什麼的都還給老師了, 只能用自己理解的方式, 筆記下 Hamiltonian dynamic.

 💡 如果連我都能懂, 相信大家都能理解 HMC 了

但還是建議先看 MCMC by Gibbs and Metropolis-Hasting Sampling, 因為這篇要說的 Hamiltonian Monte Carlo (HMC) 是 Metropolis-Hastings (MH) 方法的一種, 只是 proposal distribution 從 random walk 改成使用 Hamiltonian dynamics 來做, 因而變的非常有效率 (accept rate 很高), 且對於高維度資料採樣也很有效.

首先粗體字如 $\mathbf{x}, \mathbf{v}, \mathbf{p}$ 都是 column vector, 而非粗體字表 scalar, e.g. $m,t$

閱讀全文 »

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 角度代表什麼意義.

先定義一波

閱讀全文 »
1…345…11
Chih-Sheng Chen

Chih-Sheng Chen

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