[Autoencoder] Variational Sparse Coding (VSC)論文筆記

tags: Literature Reading Sparse Coding Autoencoder Paper

Github

論文概覽(以2019年ICLR為主要參考依據)

Variational Sparse Coding. (Under review as a conference paper at ICLR 2019)

​​​​2019 under review這篇有比較多的圖解與詳細的公式推導說明(附錄)

摘要

變異自動編碼器(VAEs)在其他難以處理的生成模型中進行近似推理時提供了一種可行的方法。然而,標準的VAEs經常產生分散的、缺乏可解釋性的潛伏代碼,從而使產生的表示不適合輔助任務(如分類)和人類解釋。我們通過融合變異自動編碼器和稀疏編碼的思想來解決這些問題,並建議用Spike和Slab先驗分佈對VAE的潛在空間進行明確的稀疏建模。我們使用離散混合識別函數(mixture recognition function)推導出證據下限(evidence lower bound),從而使近似的後驗推斷與標準的VAE情況一樣具有計算效率。通過新的方法,我們能夠推斷出真正的稀疏表徵,並具有一般難以解決的非線性概率模型。我們證明了這些稀疏表徵在兩個基準分類任務(MNIST和Fashion-MNIST)中比標準VAE表徵更有優勢,證明了分類精度的提高和對潛伏維數的穩健性的明顯增加。此外,我們還從質量上證明了稀疏元素捕捉了主觀上可以理解的變化來源。

Variational auto-encoders (VAEs) offer a tractable approach when performing approximate inference in otherwise intractable generative models. However, standard VAEs often produce latent codes that are disperse and lack interpretability, thus making the resulting representations unsuitable for auxiliary tasks (e.g. classification) and human interpretation. We address these issues by merging ideas from variational auto-encoders and sparse coding, and propose to explicitly model sparsity in the latent space of a VAE with a Spike and Slab prior distribution. We derive the evidence lower bound using a discrete mixture recognition function thereby making approximate posterior inference as computational efficient as in the standard VAE case. With the new approach, we are able to infer truly sparse representations with generally intractable non-linear probabilistic models. We show that these sparse representations are advantageous over standard VAE representations on two benchmark classification tasks (MNIST and Fashion-MNIST) by demonstrating improved classification accuracy and significantly increased robustness to the number of latent dimensions. Furthermore, we demonstrate qualitatively that the sparse elements capture subjectively understandable sources of variation.


1 INTRODUCTION

  • 在經過Autoencoder壓縮、學習到的高維表徵(representations),往往會分散在大量維度中,大而冗餘的潛在空間(維度)往往不具資訊、編碼效率低落,維度與特徵間可能是低度相關、不易解釋
  • 稀疏編碼(sparse coding)為上述問題提供了一個優雅的解決方案:
    • 表示空間被誘導為稀疏的。在這種情況下,鼓勵編碼函數使用描述每個觀察結果所需的最小數量的非零元素,並將信息濃縮在少數幾個活躍的變量中
    • 由於其表示效率高,稀疏代碼已被用於許多學習和識別系統中,因為它們提供了更容易的解釋

2.2 VARIATIONAL AUTO-ENCODERS

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

圖1:變異稀疏編碼模型(右)與標準VAE(左)的示意圖。在這兩種情況下,觀察到的變量\(x_i\)被認為是由未觀察到的變量\(z_i\)產生的。然而,變異稀疏編碼是用Spike和Slab先驗分佈對潛伏空間的稀疏性進行建模。每個先驗的例子都是以MNIST數據集的一個樣本為例。

ELBO

  • ELBO(簡寫為ELB, paper中以\(L(\theta, \phi;x_i)\)表示) = prior term + reconstruction term
    • prior term
      • 最小化潛向量(z, latent vectors)與指定的機率prior(或稱後驗分布,例如常態分佈)分布的距離,即最小化 \(D_{KL}(q_φ(z|x)||p_{s}(z))\)
    • reconstruction term
      • 最大化資料分布的期望值
        • 白話的理解是,希望原始影像與(經過encoder壓縮、decoder還原後的)重建影像盡可能接近
        • maximises the expectation of the data likelihood under the recognition function
    • \(\theta, \phi\)分別代表decoder與encoder與的參數,透過不斷優化、最大化ELB學習得到
  • \(p_{s}(z)\) : 我們希望latents vectors(z)服從的機率分布(底線s代表Spike and Slab),由於\(p_{s}(z)\) 很複雜且無法公式化求解,因此我們用較為簡單且可公式化的 \(q_φ(z|x)\)去近似。
  • 並希望\(p_{s}(z)\)\(q_φ(z|x)\)盡可能接近,因此我們用度量兩個分布是否近似的KL Divergence,即\(D_{KL}(q_φ(z|x)||p_{s}(z))\)去衡量兩個分布的相似程度,距離越小代表分布越近似。
  • ELBO(Evidence Lower Bound,證據下界)。這裡的證據指資料或可觀測變量的機率密度。利用變分推斷(Variational Inference )去近似\(D_{KL}(q_φ(z|x)||p_{s}(z))\),得到\[ELB = -D_{KL} + log_p(x|z)\]

最大化ELB 即是最小化\(D_{KL}\),也就是讓z的分布越接近我們指定(約束)的分布

:::

3. VARIATIONAL SPARSE CODING

SPARSE CODING

  • \(p_{s}(z)\) (Probability of z ~ Spike and Slab Distribution)
    • 透過參數α控制spike分布集中(稀疏性)的強度

      • α 趨近1 代表趨近高斯常態分布(slab變數)
      • α 趨近0 代表趨近以零为中心的Delta函數(spike變數)
      • α and (1 − α) respectively and the slab variable has a distribution which is either a Gaussian or a Delta function centered at zero, conditioned on whether the spike variable is one or zero respectively
    • The recognition function \(q_φ(z|x)\) is chosen to be a discrete mixture model of the form

3.1 ELBO PRIOR TERM(詳見附錄B)

  • 對latent vectors的spike and slab分布約束項
Appendix B. PRIOR TERM of Spike and Slab

\(D_{KL}(q_φ(z|x)||p_{s}(z))\) 公式推導

  • Spike and Slab分布的KL divergence分布近似值推導(ELBO)
    • 讓Latent vectors分布近似Spike and Slab分布
def prior_loss()
code
# Reconstruction + KL divergence losses summed over all elements of batch def prior_loss(self, mu, logvar, logspike): # see Appendix B from VSC paper / Formula 6 spike = torch.clamp(logspike.exp(), 1e-6, 1.0 - 1e-6) prior1 = -0.5 * torch.sum(spike.mul(1 + logvar - mu.pow(2) - logvar.exp()), dim=1) prior21 = (1 - spike).mul(torch.log((1 - spike) / (1 - self.alpha))) prior22 = spike.mul(torch.log(spike / self.alpha)) prior2 = torch.sum(prior21 + prior22, dim=1) PRIOR = prior1 + prior2 # Slab + Spike KL Divergence #LOSS = 0.01 * PRIOR LOSS = PRIOR.mean() return LOSS

3.2 ELBO RECONSTRUCTION TERM(詳見附錄C)

Appendix C. SPIKE AND SLAB DRAWS REPARAMETRISATION

控制spike函數活化敏感度

REPARAMETRISATION OF THE DRAWS
C.1 REPARAMETRISATION OF THE DRAWS
  • 透過γ(α)來調控分布近似 spike and slab分布的強度
    • 前半部:以sigmoid活化函去近似的階梯函數\(T(y) = S(cy)\)
    • 後半部:高斯分布\(X \sim N(\mu,\sigma^2)\),在這邊寫作\(\mu_{z,i} + \sigma_{z,i}\odot\epsilon_{t}\)
    • 如果拿掉前半部,其實參數重構的過程就是讓z符合常態分布的VAE,而乘上前半部的階梯函數T(y),則會讓z的分布逼近spike and slab分布
C.2 SPIKE VARIABLE REPARAMETRISATION
  • 透過參數c控制spike函數活化敏感度(輸出為0的閾值邊界)
    • scaled Sigmoid function
      • \(T(y) = S(cy).\) In the \(limit\, c → ∞, S(cy)\) tends to the true binary mapping
      • 透過可縮放的sigmoid函數\(S(cy)\)去近似階梯函數\(T(y)\),將原本的常態分佈轉變為0與1的二項分布,並且透過超參數設定來決定0值的邊界/比例(見附錄圖6)
def reparameterize()
code
def reparameterize(self, mu, logvar, logspike): std = torch.exp(0.5*logvar) eps = torch.randn_like(std) gaussian = eps.mul(std).add_(mu) eta = torch.rand_like(std) #selection = F.sigmoid(125 * (eta + logspike.exp() - 1)) selection = F.sigmoid(self.c * (eta + logspike.exp() - 1)) return selection.mul(gaussian)

3.3 THE VSC EVIDENCE LOWER BOUND

  • 最後得到VSC的Lower Bound(即ELB)
    • Prior term
      • \(-D_{KL}\) of Spike and weighted Slab
    • Reconstruction term
      • is the expectation of the likelihood under the recognition function PDF, estimated stochastically.
    • \(γ_{i,j}\) = α = 1 即標準的VAE

Results

ELBO EVALUATION

  • VLB 代表意義:

    • evidence lower bound (ELBO) / VLB
    • 可觀測的資料機率密度,數值越高、\(D_{KL}\)越小,代表簡化、推論的的q分布越近似z的分布 \(p(z|x)\)
  • 圖2: 當給太多冗餘的維度空間時,VAE在分布近似上反而表現不好:相對的VSC透過稀疏約束(將無用的活度去活化為0值),而將Z的分布限縮在較小的空間範圍,反而有較穩定的表現

Appendix E.1 EXAMPLES OF SPARSE CODES AND RECONSTRUCTIONS

  • 不同的稀疏強度(prior sparsity/prior Spike probability)α值設定,對不同資料集潛在空間編碼與影像重建的結果(latent codes and reconstruction)
    • α=1時,基本上就是沒有經過稀疏編碼的約束,即VAE
    • trade-off btw reconstruction and sparsity
      • 當為維度越稀疏、影像的重建或還原度越低,
      • 反之,當影像越複雜,則可能需要越多的維度來儲存訊息(α可能太低)

Appendix E.2 LATENT SPACE SPARSITY

  • MNIST and Fashion-MNIST datasets in 200-dimensional spaces with different values of the prior Spike probability α.
  • x軸 : 不同α. y軸 : 不同維度的稀疏性(latents vectors 維度為0的比例)
  • 大致呈線性關係,α越低,維度的稀疏程度越偏離線性

Variational Sparse Coding.PMLR, 2020

摘要

無監督地發現可解釋的特徵和可控制的高維數據生成是目前機器學習的主要挑戰,在數據可視化、聚類和人工數據合成中都有應用。我們提出了一個基於變異自動編碼器(VAEs)的模型,在這個模型中,解釋是通過潛在的空間稀疏性誘導出來的,並以Spike和Slab分佈的混合作為先驗條件。我們為這個模型推導出一個證據下限(evidence lower bound),並提出了一個具體的訓練方法,以恢復被分解的特徵(disentangled features)作為潛伏向量的稀疏元素。在我們的實驗中,我們證明了在無法估計真實變化源的數量和對象顯示不同的屬性組合時,其解纏性能(disentanglement performance)優於標準VAE方法。此外,新模型提供了獨特的能力,如恢復特征利用(recovering feature exploitation),合成與給定輸入對象共享屬性的樣本,以及在生成時控制離散和連續特徵。

Unsupervised discovery of interpretable features and controllable generation with high-dimensional data are currently major challenges in machine learning, with applications in data visualisation, clustering and artificial data synthesis. We propose a model based on variational auto-encoders (VAEs) in which interpretation is induced through latent space sparsity with a mixture of Spike and Slab distributions as prior. We derive an evidence lower bound for this model and propose a specific training method for recovering disentangled features as sparse elements in latent vectors. In our experiments, we demonstrate superior disentanglement performance to standard VAE approaches when an estimate of the number of true sources of variation is not available and objects display different combinations of attributes. Furthermore, the new model provides unique capabilities, such as recovering feature exploitation, synthesising samples that share attributes with a given input object and controlling both discrete and continuous features upon generation.

重點摘要

相較於2019,補衝了特徵可分解性(FEATURE DISENTANGLEMENT)的分析

4.2 FEATURE DISENTANGLEMENT


圖3:笑臉數據集的源特徵和潛變量之間的相關絕對值。 X軸表示潛變量的指數。變量已被替換為與同一特徵顯示出最高相關性的維度組與同一特徵的最高相關性。如果每個潛在變量與一個源屬性有很強的相關性,但與其他屬性的相關性較弱,那麼就可以實現特徵分離。導致一個塊狀對角線結構。
Figure 3: Absolute value of correlation between source features and latent variables for the Smiley data set. The x-axis indicates the index of the latent variable. Variables have been permuted to group dimensions that display the highest correlation with the same feature. Feature disentanglement is achieved if each latent variable correlates strongly with one source attribute but weakly with the remaining ones thus leading to a block diagonal structure.

  • β-VAE
    • 如果特徵數量與手動指定的維度空間(Latent Variables)相當時。 β-VAE的維度跟這特徵間有很好的相關性如圖3(a),但當為度空間很大時特徵可能會被分配、拆散到不同維度而缺乏相關性©
  • VSC
    • 反之,如圖3(d)所示,VSC模型能夠很好地分解不同的特徵組。因為它被設計成對不同的特徵組合進行建模,它可以根據每個觀察中識別的特徵來激活或停用每個編碼的潛變量,並成功地將每個特徵分解在不同的子空間中。在不同的子空間中,它能成功地分解每個at-tribute,而且相互之間的依賴性很小。無論選擇什麼樣的潛空間維度。VSC模型調整到一個合適的潛變量數量,以代表數據。適當數量的潛在變量來代表數據
    • 在圖3(d)的矩陣中,零列對應於潛在變量持續不活躍的折疊維度。考慮到訓練前有60個潛在維度,並且不知道源維度或稀疏度,模型折疊到20個利用維度來描述由18個源變量獨立產生的數據。分配給每個特徵組的潛變量的數量也以相當好的精度恢復,因為圖3中的VSC相關矩陣呈現出近似塊狀的對角線結構。

背景知識 Autoencoder 與 Variational Auto-encoders (VAEs)

Auto-encoder

Variational Auto-encoder

Kullback-Leibler divergence

  • Hands-On Convolutional Neural Networks with TensorFlow


    KL分歧損失是會產生一個數字,表明兩個分佈之間的接近程度。

    兩個分佈越接近對方,損失就越小。在下圖中,藍色分佈正試圖模擬綠色分佈。隨著藍色分佈越來越接近綠色分佈,KL分歧損失將越來越接近於零。

    The KL divergence loss is one that will produce a number indicating how close two distributions are to each other.

    The closer two distributions get to each other, the lower the loss becomes. In the following graph, the blue distribution is trying to model the green distribution. As the blue distribution comes closer and closer to the green one, the KL divergence loss will get closer to zero.

  • Why Is Cross Entropy Equal to KL-Divergence?

Variational Inference

  • Variational Inference: A Review for Statisticians

  • 漫談 Variational Inference

    Variational Inference 是一種近似複雜分佈的數學方法,我們假設一類計算簡單的候選分佈q(z),並有另一個複雜難以計算的後驗分佈p(z|x),我們希望從候選分佈中找一個最接近p(z|x)的q∗(z),這個等同於最小化KL(q(z)|p(z|x))

  • 变分推断——深度学习第十九章

    基本思路

    在概率模型中,我们常常需要近似难以计算的概率分布,在贝叶斯统计中,所有的对于未知量的推断(inference)问题可以看做是对后验概率(posterior)的计算,而这一概率通常难以计算,之前我们提到了利用MCMC马尔科夫链蒙特卡洛算法(可参考蒙特卡罗方法——深度学习第十七章)做近似,但是对于大量数据,MCMC算法计算较慢,变分推断(Variational Inference)就为我们提供了一种更快更简单的适用于大量数据的近似推断方法。

  • 变分推断中的ELBO(证据下界)

    变分推理的目标是近似潜在变量(latent variables)在观测变量(observed variables)下的条件概率。解决该问题,需要使用优化方法。在变分推断中,需要使用下列优化方法

    \[q^{*}(z)= \operatorname*{arg\,min\,KL}_{q(z)\in \mathfrak{Q}} (q(z)\|p(z|x)).\]

    • In variational inference, we specify a family \(\mathfrak{Q}\) of densities over the latent variables. Each \(q(z) ∈ \mathfrak{Q}\) is a candidate approximation to the exact conditional. Our goal is to find the best candidate, the one closest in KL divergence to the exact conditional.2 Inference now amounts to solving the following optimization problem,
    • q∗(·) is the best approximation of the conditional, within the family Q.
    • 变分推断等价于最小化KL散度。
    • ELBO
      • ELBO,全称为 Evidence Lower Bound,即证据下界。这里的证据指数据或可观测变量的概率密度。

      • 假设 \(x=x_{1:n​}\)表示一系列可观测数据集, \(z=z_{1:m​}\)为一系列隐变量(latent variables)。则可用 \(p(z,x)\)表示联合概率, \(p(z∣x)\)为条件概率,\(p(x)\)为证据。

  • 如何简单易懂地理解变分推断(variational inference)?

    • 目標是根據已有數據去推斷其分布,此數據分布稱之為p,當p很複雜不易表達、無法直接求解時,使簡單的(易表達、可求解)的分布q去近似p,即為便變方推斷的基本概念,至於近似的方式如下:
      • 將原本求分布的推斷方式,改為求"縮小兩個分布的距離",以此為目標函數進行優化求解
      • 下圖黃色區域是目標分布p,較為複雜無法直接求解,看起來有幾分像高斯常態分佈,因此,我們以不同的常態分佈去近似其解(紅色q與黃色q),分別計算q與p的重疊面積,選擇距離較近的q做為p的近似

    原始目标是,需要根据已有数据推断需要的分布p;当p不容易表达,不能直接求解时,可以尝试用变分推断的方法, 即,寻找容易表达和求解的分布q,当q和p的差距很小的时候,q就可以作为p的近似分布,成为输出结果了。在这个过程中,我们的关键点转变了,从“求分布”的推断问题,变成了“缩小距离”的优化问题。

Deep Learning相關筆記

Self-supervised Learning

Object Detection

ViT與Transformer相關

Autoencoder相關

Select a repo