https://arxiv.org/pdf/1912.03263v3.pdf
常用的分类器模型都是在建模 p θ ( y ∣ x ) p_{\theta}(y \mid \mathbf{x}) pθ(y∣x),这篇文章从能量的视角解释分类模型,进而得到一个生成模型和分类模型的混合模型。该模型能够同时建模 p θ ( y ∣ x ) p_{\theta}(y \mid \mathbf{x}) pθ(y∣x)和 p θ ( x ) p_{\theta}(\mathbf{x}) pθ(x),从而提高分类精度和样本生成质量。
这篇文章也被用作OOD检测的baseline。
Joint Energy-based Model(JEM)
先来overview一下模型结构:
 
 
一个神经网络分类模型输入到Softmax函数的值称之为 f θ ( x ) f_{\theta}(x) fθ(x),传统的分类器模型用 f θ ( x ) f_{\theta}(x) fθ(x)输入到softmax函数中估计 p ( y ∣ x ) p(y \mid \mathbf{x}) p(y∣x),这篇文章里同时还用 f θ ( x ) f_{\theta}(x) fθ(x)来估计 p ( x , y ) p( \mathbf{x},y) p(x,y)和 p ( x ) p(\mathbf{x}) p(x)。
本文的方法
EBM
Energy-based model:
 
     
      
       
        
         
         
          
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            )
           
           
            =
           
           
            
             
              exp
             
             
              
             
             
              
               (
              
              
               −
              
              
               
                E
               
               
                θ
               
              
              
               (
              
              
               x
              
              
               )
              
              
               )
              
             
            
            
             
              Z
             
             
              (
             
             
              θ
             
             
              )
             
            
           
          
         
         
         
          
           (1)
          
         
        
       
       
         p_{\theta}(\mathrm{x})=\frac{\exp \left(-E_{\theta}(\mathrm{x})\right)}{Z(\theta)} \tag{1} 
       
      
     pθ(x)=Z(θ)exp(−Eθ(x))(1)
 其中
    
     
      
       
        
         E
        
        
         θ
        
       
       
        (
       
       
        x
       
       
        )
       
       
        :
       
       
        
         R
        
        
         D
        
       
       
        →
       
       
        R
       
      
      
       E_{\theta}(\mathrm{x}): \mathbb{R}^{D} \rightarrow \mathbb{R}
      
     
    Eθ(x):RD→R是能量函数,
    
     
      
       
        Z
       
       
        (
       
       
        θ
       
       
        )
       
       
        =
       
       
        
         ∫
        
        
         x
        
       
       
        exp
       
       
        
       
       
        
         (
        
        
         −
        
        
         
          E
         
         
          θ
         
        
        
         (
        
        
         x
        
        
         )
        
        
         )
        
       
      
      
       Z(\theta)=\int_{\mathbf{x}} \exp \left(-E_{\theta}(\mathbf{x})\right)
      
     
    Z(θ)=∫xexp(−Eθ(x))是配分函数(这个不用管)。要训练这个函数可以考虑优化对数似然的方法,对
    
     
      
       
        θ
       
      
      
       \theta
      
     
    θ求梯度(这是本文的两个loss之一):
 
     
      
       
        
         
         
          
           
            
             
              ∂
             
             
              log
             
             
              
             
             
              
               p
              
              
               θ
              
             
             
              (
             
             
              x
             
             
              )
             
            
            
             
              ∂
             
             
              θ
             
            
           
           
            =
           
           
            
             E
            
            
             
              p
             
             
              θ
             
             
              
               (
              
              
               
                x
               
               
                ′
               
              
              
               )
              
             
            
           
           
            
             [
            
            
             
              
               ∂
              
              
               
                E
               
               
                θ
               
              
              
               
                (
               
               
                
                 x
                
                
                 ′
                
               
               
                )
               
              
             
             
              
               ∂
              
              
               θ
              
             
            
            
             ]
            
           
           
            −
           
           
            
             
              ∂
             
             
              
               E
              
              
               θ
              
             
             
              (
             
             
              x
             
             
              )
             
            
            
             
              ∂
             
             
              θ
             
            
           
          
         
         
         
          
           (2)
          
         
        
       
       
         \frac{\partial \log p_{\theta}(\mathrm{x})}{\partial \theta}=\mathbb{E}_{p \theta\left(\mathrm{x}^{\prime}\right)}\left[\frac{\partial E_{\theta}\left(\mathrm{x}^{\prime}\right)}{\partial \theta}\right]-\frac{\partial E_{\theta}(\mathrm{x})}{\partial \theta} \tag{2} 
       
      
     ∂θ∂logpθ(x)=Epθ(x′)[∂θ∂Eθ(x′)]−∂θ∂Eθ(x)(2)
 比较困难的是从
    
     
      
       
        
         p
        
        
         θ
        
       
       
        (
       
       
        x
       
       
        )
       
      
      
       p_{\theta}(x)
      
     
    pθ(x)中采样,早期训练EBM使用MCMC方法,本文用较新的Stochastic Gradient Langevin Dynamics (SGLD):
 
     
      
       
        
         
         
          
           
            
             x
            
            
             0
            
           
           
            ∼
           
           
            
             p
            
            
             0
            
           
           
            (
           
           
            x
           
           
            )
           
           
            ,
           
           
           
            
             x
            
            
             
              i
             
             
              +
             
             
              1
             
            
           
           
            =
           
           
            
             x
            
            
             i
            
           
           
            −
           
           
            
             α
            
            
             2
            
           
           
            
             
              ∂
             
             
              
               E
              
              
               θ
              
             
             
              
               (
              
              
               
                x
               
               
                i
               
              
              
               )
              
             
            
            
             
              ∂
             
             
              
               x
              
              
               i
              
             
            
           
           
            +
           
           
            ϵ
           
           
            ,
           
           
           
            ϵ
           
           
            ∼
           
           
            N
           
           
            (
           
           
            0
           
           
            ,
           
           
            α
           
           
            )
           
          
         
         
         
          
           (3)
          
         
        
       
       
         \mathbf{x}_{0} \sim p_{0}(\mathbf{x}), \quad \mathbf{x}_{i+1}=\mathbf{x}_{i}-\frac{\alpha}{2} \frac{\partial E_{\theta}\left(\mathbf{x}_{i}\right)}{\partial \mathbf{x}_{i}}+\epsilon, \quad \epsilon \sim \mathcal{N}(0, \alpha)\tag{3} 
       
      
     x0∼p0(x),xi+1=xi−2α∂xi∂Eθ(xi)+ϵ,ϵ∼N(0,α)(3)
 这个方法和PGD有些相似,这里直观的解释就是采样的
    
     
      
       
        x
       
      
      
       x
      
     
    x朝着能量低的地方去,一次训练采样
    
     
      
       
        N
       
      
      
       N
      
     
    N次。近期的工作显示SGLD的结果已经接近公式(2)。
提出的JEM
考虑一个
    
     
      
       
        K
       
      
      
       K
      
     
    K分类问题,
    
     
      
       
        
         f
        
        
         θ
        
       
       
        :
       
       
        
         R
        
        
         D
        
       
       
        →
       
       
        
         R
        
        
         K
        
       
      
      
       f_θ : R^D → R^K
      
     
    fθ:RD→RK,其能将每个数据点 
    
     
      
       
        x
       
       
        ∈
       
       
        
         R
        
        
         D
        
       
      
      
       x ∈ R^D
      
     
    x∈RD 映射成被称为 logit 的实数值。使用所谓的 softmax 迁移函数,可将这些 logit 用于对类别分布执行参数化:
 
     
      
       
        
         
         
          
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            y
           
           
            ∣
           
           
            x
           
           
            )
           
           
            =
           
           
            
             
              exp
             
             
              
             
             
              
               (
              
              
               
                f
               
               
                θ
               
              
              
               (
              
              
               x
              
              
               )
              
              
               [
              
              
               y
              
              
               ]
              
              
               )
              
             
            
            
             
              
               ∑
              
              
               
                y
               
               
                ′
               
              
             
             
              exp
             
             
              
             
             
              
               (
              
              
               
                f
               
               
                θ
               
              
              
               (
              
              
               x
              
              
               )
              
              
               
                [
               
               
                
                 y
                
                
                 ′
                
               
               
                ]
               
              
              
               )
              
             
            
           
          
         
         
         
          
           (4)
          
         
        
       
       
         p_{\theta}(y \mid \mathbf{x})=\frac{\exp \left(f_{\theta}(\mathbf{x})[y]\right)}{\sum_{y^{\prime}} \exp \left(f_{\theta}(\mathbf{x})\left[y^{\prime}\right]\right)} \tag{4} 
       
      
     pθ(y∣x)=∑y′exp(fθ(x)[y′])exp(fθ(x)[y])(4)
 其中
    
     
      
       
        
         f
        
        
         θ
        
       
       
        (
       
       
        x
       
       
        )
       
       
        [
       
       
        y
       
       
        ]
       
      
      
       f_{\theta}(x)[y]
      
     
    fθ(x)[y]是网络输出向量的第
    
     
      
       
        k
       
      
      
       k
      
     
    k个分量。用这些logit,无需改变模型,为x和y的联合分布重新定义一个基于能量的模型:
 
     
      
       
        
         
         
          
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            ,
           
           
            y
           
           
            )
           
           
            =
           
           
            
             
              exp
             
             
              
             
             
              
               (
              
              
               
                f
               
               
                θ
               
              
              
               (
              
              
               x
              
              
               )
              
              
               [
              
              
               y
              
              
               ]
              
              
               )
              
             
            
            
             
              Z
             
             
              (
             
             
              θ
             
             
              )
             
            
           
          
         
         
         
          
           (5)
          
         
        
       
       
         p_{\theta}(\mathbf{x}, y)=\frac{\exp \left(f_{\theta}(\mathbf{x})[y]\right)}{Z(\theta)} \tag{5} 
       
      
     pθ(x,y)=Z(θ)exp(fθ(x)[y])(5)
 通过对
    
     
      
       
        y
       
      
      
       y
      
     
    y边缘化(积分),也可为
    
     
      
       
        x
       
      
      
       x
      
     
    x获得一个非归一化的密度模型:
 
     
      
       
        
         
         
          
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            )
           
           
            =
           
           
            
             ∑
            
            
             y
            
           
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            ,
           
           
            y
           
           
            )
           
           
            =
           
           
            
             
              
               ∑
              
              
               y
              
             
             
              exp
             
             
              
             
             
              
               (
              
              
               
                f
               
               
                θ
               
              
              
               (
              
              
               x
              
              
               )
              
              
               [
              
              
               y
              
              
               ]
              
              
               )
              
             
            
            
             
              Z
             
             
              (
             
             
              θ
             
             
              )
             
            
           
          
         
         
         
          
           (6)
          
         
        
       
       
         p_{\theta}(\mathbf{x})=\sum_{y} p_{\theta}(\mathbf{x}, y)=\frac{\sum_{y} \exp \left(f_{\theta}(\mathbf{x})[y]\right)}{Z(\theta)}\tag{6} 
       
      
     pθ(x)=y∑pθ(x,y)=Z(θ)∑yexp(fθ(x)[y])(6)
 某个数据
    
     
      
       
        x
       
      
      
       x
      
     
    x的能量为:
 
     
      
       
        
         
         
          
           
            
             E
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            )
           
           
            =
           
           
            −
           
           
            log
           
           
            
           
           
            
             SumExp
            
            
             
            
            
             y
            
           
           
            
             (
            
            
             
              f
             
             
              θ
             
            
            
             (
            
            
             x
            
            
             )
            
            
             [
            
            
             y
            
            
             ]
            
            
             )
            
           
           
            =
           
           
            −
           
           
            log
           
           
            
           
           
            
             ∑
            
            
             y
            
           
           
            exp
           
           
            
           
           
            
             (
            
            
             
              f
             
             
              θ
             
            
            
             (
            
            
             x
            
            
             )
            
            
             [
            
            
             y
            
            
             ]
            
            
             )
            
           
          
         
         
         
          
           (7)
          
         
        
       
       
         E_{\theta}(\mathbf{x})=-\log \operatorname{SumExp}_{y}\left(f_{\theta}(\mathbf{x})[y]\right)=-\log \sum_{y} \exp \left(f_{\theta}(\mathbf{x})[y]\right)\tag{7} 
       
      
     Eθ(x)=−logSumExpy(fθ(x)[y])=−logy∑exp(fθ(x)[y])(7)
 定义完了来优化我们的模型,我们的优化目标是最大化似然
    
     
      
       
        p
       
       
        (
       
       
        x
       
       
        ,
       
       
        y
       
       
        )
       
      
      
       p(x,y)
      
     
    p(x,y),对其做分解:
 
     
      
       
        
         
         
          
           
            log
           
           
            
           
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            ,
           
           
            y
           
           
            )
           
           
            =
           
           
            log
           
           
            
           
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            x
           
           
            )
           
           
            +
           
           
            log
           
           
            
           
           
            
             p
            
            
             θ
            
           
           
            (
           
           
            y
           
           
            ∣
           
           
            x
           
           
            )
           
          
         
         
         
          
           (8)
          
         
        
       
       
         \log p_{\theta}(\mathbf{x}, y)=\log p_{\theta}(\mathbf{x})+\log p_{\theta}(y \mid \mathbf{x}) \tag{8} 
       
      
     logpθ(x,y)=logpθ(x)+logpθ(y∣x)(8)
 通过优化后两项来达到优化目标,
    
     
      
       
        log
       
       
        
       
       
        
         p
        
        
         θ
        
       
       
        (
       
       
        y
       
       
        ∣
       
       
        x
       
       
        )
       
      
      
       \log p_{\theta}(y \mid \mathbf{x})
      
     
    logpθ(y∣x)用标准的交叉熵优化,
    
     
      
       
        log
       
       
        
       
       
        
         p
        
        
         θ
        
       
       
        (
       
       
        x
       
       
        )
       
      
      
       \log p_{\theta}(\mathbf{x})
      
     
    logpθ(x)用SGLD的公式(2)优化。
以上是提出的方法,有用的公式是(2)(3)(8)。
应用
文章提出的混合模型除了可以分类,还有很多功能,挑三个主要的讲:
还是生成模型
可以生成样本:
 
 
我和家明学长讨论很久,结合文章的代码,我猜是从公式(3)中生成的图片,即采样出的图片
OOD detection
有能量函数,可以用来异常检测是很自然的事情。没用直接用
    
     
      
       
        E
       
       
        (
       
       
        x
       
       
        )
       
      
      
       E(x)
      
     
    E(x)来检测,而是提出了一个指标:
 
     
      
       
        
         
          s
         
         
          θ
         
        
        
         (
        
        
         x
        
        
         )
        
        
         =
        
        
         −
        
        
         
          
           ∥
          
          
           
            
             ∂
            
            
             log
            
            
             
            
            
             
              p
             
             
              θ
             
            
            
             (
            
            
             x
            
            
             )
            
           
           
            
             ∂
            
            
             x
            
           
          
          
           ∥
          
         
         
          2
         
        
       
       
         s_{\theta}(\mathbf{x})=-\left\|\frac{\partial \log p_{\theta}(\mathbf{x})}{\partial \mathbf{x}}\right\|_{2} 
       
      
     sθ(x)=−∥∥∥∥∂x∂logpθ(x)∥∥∥∥2
 效果:
 
 
鲁棒性
公式(3)的SGLD过程本身就很像PGD,采出了很多并不是真实的样本参与训练,能提高鲁棒性也是理所当然的事情。
