score-based method

Generative Modeling by Estimating Gradients of the Data Distribution

https://yang-song.net/blog/2021/score/#the-score-function-score-based-models-and-score-matching

基于最大似然的生成模型,由于配分函数难以解决。所以对于传统的方法,主要有两种解决方法:1.控制模型结构,使配分函数可以求得(如自回归模型,流模型)。2.近似配分函数,典型的方法是变分自编码器(使用mcmc采样)。

隐式的生成模型,代表是GAN,可以直接学习数据分布。但训练困难,不稳定。

建模函数 s θ ( x ) = ∇ x log ⁡ p ( x ) s_{\theta}(x)=\nabla_x \log p(x) sθ(x)=xlogp(x),优化目标是 fisher divergence:

E p ( x ) [ ∥ ∇ x log ⁡ p ( x ) − s θ ( x ) ∥ 2 2 ] \mathbb{E}_{p(\mathbf{x})}\left[\left\|\nabla_{\mathbf{x}} \log p(\mathbf{x})-\mathbf{s}_{\theta}(\mathbf{x})\right\|_{2}^{2}\right] Ep(x)[xlogp(x)sθ(x)22]

由于没有办法求未知的分布 ∇ x log ⁡ p ( x ) \nabla_{\mathbf{x}} \log p(\mathbf{x}) xlogp(x),所以使用score matching的方法。

s θ ( x ) s_\theta(x) sθ(x)是score-base model。In fact, the only requirement on the score-based model is that it should be a vector-valued function with the same input and output dimensionality, which is easy to satisfy in practice.

summary:

As a brief summary, we can represent a distribution by modeling its score function, which can be estimated by training a score-based model of free-form architectures with score matching.

使用Langevin dynamics的方法进行采样求解

Langevin dynamics

Langevin dynamics是一种类似mcmc采样的方法,可以直接从 ∇ x log ⁡ p ( x ) \nabla_x \log p(x) xlogp(x)中采样 x ∼ p ( x ) x\sim p(x) xp(x),公式为:

x i + 1 ← x i + ϵ ∇ x log ⁡ p ( x ) + 2 ϵ z i , i = 0 , 1 , ⋯   , K \mathbf{x}_{i+1} \leftarrow \mathbf{x}_{i}+\epsilon \nabla_{\mathbf{x}} \log p(\mathbf{x})+\sqrt{2 \epsilon} \mathbf{z}_{i}, \quad i=0,1, \cdots, K xi+1xi+ϵxlogp(x)+2ϵ zi,i=0,1,,K

其中 x 0 x_0 x0服从任意先验分布, z i ∼ N ( 0 , I ) \mathbf{z}_{i} \sim \mathcal{N}(0, I) ziN(0,I) ϵ → 0 \epsilon\rightarrow 0 ϵ0 K → ∞ K\rightarrow \infty K,此时 x K ∼ p ( x ) x_K\sim p(x) xKp(x)

所以可以将 s θ ( x ) ≈ ∇ x log ⁡ p ( x ) s_{\theta}(x)\approx \nabla_x \log p(x) sθ(x)xlogp(x)带入上面方程,则可以采样得到服从于 p ( x ) p(x) p(x)的样本 x K x_K xK

Naive score-based generative modeling and its pitfalls

上面方法描述了如何通过langevin dynamic来获得样本和训练模型,但是这里仍然存在一些问题。

在这里插入图片描述

优化的目标:

E p ( x ) [ ∥ ∇ x log ⁡ p ( x ) − s θ ( x ) ∥ 2 2 ] = ∫ p ( x ) ∥ ∇ x log ⁡ p ( x ) − s θ ( x ) ∥ 2 2   d x \mathbb{E}_{p(\mathbf{x})}\left[\left\|\nabla_{\mathbf{x}} \log p(\mathbf{x})-\mathbf{s}_{\theta}(\mathbf{x})\right\|_{2}^{2}\right]=\int p(\mathbf{x})\left\|\nabla_{\mathbf{x}} \log p(\mathbf{x})-\mathbf{s}_{\theta}(\mathbf{x})\right\|_{2}^{2} \mathrm{~d} \mathbf{x} Ep(x)[xlogp(x)sθ(x)22]=p(x)xlogp(x)sθ(x)22 dx

当在 p ( x ) p(x) p(x)的低密度区域采样数据会比较少,则估计自然不准。当使用langevin dynamic采样,对于高维数据来说,很容易采样到 p ( x ) p(x) p(x)的低密度区域,使其无法生成代表数据的高质量样本。

Score-based generative modeling with multiple noise perturbations

解决上述问题的一个办法,就是往 p ( x ) p(x) p(x)中添加噪声点填充低密度区域。此时的问题是如何选择合适噪声尺度,过小的话达不到效果,过大的话会破坏原本的数据结构。为了解决这两者,同时使用了多个尺度的噪声扰动。具体操作为:

分别添加 L L L个逐渐增大的方差获得 L L L个新的分布 p σ i ( x ) p_{\sigma_i}(x) pσi(x) σ 1 < σ 2 < ⋯ < σ L \sigma_1<\sigma_2<\dots<\sigma_L σ1<σ2<<σL,此时从 p σ i ( x ) p_{\sigma_i}(x) pσi(x)中采样很容易,即 x ∼ p ( x ) x\sim p(x) xp(x),然后计算 x + σ i z , z i ∼ N ( 0 , I ) x+\sigma_{i}z,\mathbf{z}_{i} \sim \mathcal{N}(0, I) x+σiz,ziN(0,I).

下面对噪声扰动的 p ( x ) p(x) p(x)分布的score function, ∇ x log ⁡ p ( x ) \nabla_{\mathbf{x}} \log p(\mathbf{x}) xlogp(x)进行估计,使用score matching方法训练 s θ ( x , i ) \mathbf{s}_{\theta}(\mathbf{x},i) sθ(x,i)Noise Conditional Score-Based Model,又名Noise Conditional Score Network,NCSN)。

在这里插入图片描述

训练 s θ ( x , i ) \mathbf{s}_{\theta}(\mathbf{x},i) sθ(x,i)的目标函数是:

在这里插入图片描述

s θ ( x , i ) \mathbf{s}_{\theta}(\mathbf{x},i) sθ(x,i)训练完成后,则可以使用Langevin dynamics来采样得到样本, i = L , L − 1 , … , 1 i=L, L-1, \dots, 1 i=L,L1,,1,This method is called annealed Langevin dynamics

Score-based generative modeling with stochastic differential equations (SDEs)

当噪声尺度的数量推广到无穷大时,此时可以不仅可以获得高质量的样本还能对对数似然精确求解,和对逆过程的可控生成。

当使用无穷多的噪声来扰动数据样本时,就是连续时间上的随机过程。

使用下述形式来表示这个随机过程:

d x = f ( x , t ) d t + g ( t ) d w dx = f(x, t)dt+g(t)dw dx=f(x,t)dt+g(t)dw

其中 f ( x , t ) : R d → R d f(x, t):\mathbb{R}^d\rightarrow \mathbb{R}^d f(x,t):RdRd,是一个向量函数,被称作drift coefficient。 g ( t ) ∈ R g(t)\in \mathbb{R} g(t)R是一个实值函数,被称作diffusion coefficient。w是布朗运动,dw可以被看作无意义的白噪声。这个随机微分方程的解是随机变量 { x ( t ) } t ∈ [ 0 , T ] \{x(t)\}_{t\in [0,T]} {x(t)}t[0,T]的连续集合。t是从开始时刻0到时刻T中的每一个时刻。

p t ( x ) p_t(x) pt(x) x ( t ) x(t) x(t)的概率密度函数,此时 p t ( x ) p_t(x) pt(x)类似于 p σ i ( x ) p_{\sigma_{i}}(x) pσi(x)。显然 p 0 ( x ) = p ( x ) p_0(x)=p(x) p0(x)=p(x),当添加足够步长 T T T时, p T ( x ) p_T(x) pT(x)就会变成可以tractable的先验概率分布 π ( x ) \pi(x) π(x),在噪声尺度有限时, p T ( x ) p_T(x) pT(x)相当于 p σ L ( x ) p_{\sigma_{L}}(x) pσL(x)。此时相当于对数据添加了极大方差 σ L \sigma_{L} σL扰动。

添加噪声扰动有很多方式,例如下面的SDE:

d x = e t d w dx = e^tdw dx=etdw

这个数据扰动相当于均值为0,方差是指数级增长的噪音。相当于 N ( 0 , σ 1 2 I ) , N ( 0 , σ 2 2 I ) , ⋯   , N ( 0 , σ L 2 I ) \mathcal{N}\left(0, \sigma_{1}^{2} I\right), \mathcal{N}\left(0, \sigma_{2}^{2} I\right), \cdots, \mathcal{N}\left(0, \sigma_{L}^{2} I\right) N(0,σ12I),N(0,σ22I),,N(0,σL2I) σ 1 < σ 2 < ⋯ < σ L \sigma_1<\sigma_2<\dots<\sigma_L σ1<σ2<<σL且是等比数列。所以,还有其他种类的SDE,如the Variance Exploding SDE (VE SDE), the Variance Preserving SDE (VP SDE), and the sub-VP SDE.

Reversing the SDE for sample generation

对于有限个噪声尺度,可以使用annealed Langevin dynamics方法来获取样本,但是对于无限个噪声尺度,使用reverse SDE方法来采样。

对于任意的SDE,都有其逆SDE,形式如下:

d x = [ f ( x , t ) − g 2 ( t ) ∇ x log ⁡ p t ( x ) ] d t + g ( t ) d w \mathrm{d} \mathbf{x}=\left[\mathbf{f}(\mathbf{x}, t)-g^{2}(t) \nabla_{\mathbf{x}} \log p_{t}(\mathbf{x})\right] \mathrm{d} t+g(t) \mathrm{d} \mathbf{w} dx=[f(x,t)g2(t)xlogpt(x)]dt+g(t)dw

在这里插入图片描述

Estimating the reverse SDE with score-based models and score matching

要求解上面的reverse SDE,需要知道最终的概率分布函数 p T ( x ) p_T(x) pT(x)和score function ∇ x log ⁡ p t ( x ) \nabla_x\log p_t(x) xlogpt(x)。其中 p T ( x ) = π ( x ) p_T(x)=\pi(x) pT(x)=π(x),是设定的先验分布。为了估计 ∇ x log ⁡ p t ( x ) \nabla_x\log p_t(x) xlogpt(x),我们训练了一个******Time-Dependent Score-Based Model s θ ( x , t ) ≈ ∇ x log ⁡ p t ( x ) s_\theta(x, t)\approx \nabla_x\log p_t(x) sθ(x,t)xlogpt(x),**这个函数类似于上面的对于有限的噪声尺度的 s θ ( x , i ) s_\theta(x, i) sθ(x,i),所以 s θ ( x , i ) ≈ ∇ x log ⁡ p σ i ( x ) s_\theta(x, i)\approx \nabla_x\log p_{\sigma_i}(x) sθ(x,i)xlogpσi(x)

最后的训练目标是连续状态下的加权Fisher divergences

E t ∈ U ( 0 , T ) E p t ( x ) [ λ ( t ) ∥ ∇ x log ⁡ p t ( x ) − s θ ( x , t ) ∥ 2 2 ] , \mathbb{E}_{t \in \mathcal{U}(0, T)} \mathbb{E}_{p_{t}(\mathbf{x})}\left[\lambda(t)\left\|\nabla_{\mathbf{x}} \log p_{t}(\mathbf{x})-\mathbf{s}_{\theta}(\mathbf{x}, t)\right\|_{2}^{2}\right], EtU(0,T)Ept(x)[λ(t)xlogpt(x)sθ(x,t)22],

其中 U ( 0 , T ) \mathcal{U}(0, T) U(0,T) [ 0 , T ] [0, T] [0,T]之间的均匀分布。 λ : R → R > 0 \lambda:\mathbb{R}\rightarrow \mathbb{R}_{>0} λ:RR>0是一个权值函数。一般使 λ ( t ) ∝ 1 / E [ ∥ ∇ x ( t ) log ⁡ p ( x ( t ) ∣ x ( 0 ) ) ∥ 2 2 ] \lambda(t) \propto 1 / \mathbb{E}\left[\left\|\nabla_{\mathbf{x}(t)} \log p(\mathbf{x}(t) \mid \mathbf{x}(0))\right\|_{2}^{2}\right] λ(t)1/E[ x(t)logp(x(t)x(0)) 22]来平衡不同时间点上使用score match的损失。

s θ ( x , t ) s_\theta(x, t) sθ(x,t)训练到最优的时候,就可以利用估计出 reverse SDE:

d x = [ f ( x , t ) − g 2 ( t ) s θ ( x , t ) ] d t + g ( t ) d w \mathrm{d} \mathbf{x}=\left[\mathbf{f}(\mathbf{x}, t)-g^{2}(t) s_\theta(x, t)\right] \mathrm{d} t+g(t) \mathrm{d} \mathbf{w} dx=[f(x,t)g2(t)sθ(x,t)]dt+g(t)dw

x ( T ) ∼ π x(T)\sim \pi x(T)π开始,逐步求解上述的reverse SDE公式来获得样本 x ( 0 ) x(0) x(0),记最终是从 p θ p_\theta pθ中获得 x ( 0 ) x(0) x(0)的分布,当 s θ ( x , t ) s_\theta(x, t) sθ(x,t)被训练到最优的时候, p θ ≈ p 0 p_\theta\approx p_0 pθp0,此时可以看作 x ( 0 ) ∼ p 0 x(0)\sim p_0 x(0)p0

λ ( t ) = g 2 ( t ) \lambda(t)=g^2(t) λ(t)=g2(t)时,就可以得到 p θ p_\theta pθ p 0 p_0 p0之间的Fisher divergence和KL divergence之间重要的关系。

K L ( p 0 ( x ) ∥ p θ ( x ) ) ≤ T 2 E t ∈ U ( 0 , T ) E p t ( x ) [ λ ( t ) ∥ ∇ x log ⁡ p t ( x ) − s θ ( x , t ) ∥ 2 2 ] + K L ( p T ∥ π ) . \mathrm{KL}\left(p_{0}(\mathbf{x}) \| p_{\theta}(\mathbf{x})\right) \leq \frac{T}{2} \mathbb{E}_{t \in \mathcal{U}(0, T)} \mathbb{E}_{p_{t}(\mathbf{x})}\left[\lambda(t)\left\|\nabla_{\mathbf{x}} \log p_{t}(\mathbf{x})-\mathbf{s}_{\theta}(\mathbf{x}, t)\right\|_{2}^{2}\right]+\mathrm{KL}\left(p_{T} \| \pi\right) . KL(p0(x)pθ(x))2TEtU(0,T)Ept(x)[λ(t)xlogpt(x)sθ(x,t)22]+KL(pTπ).

由于极大似然估计和最小化KL散度之间的等价性, λ ( t ) = g 2 ( t ) \lambda(t)=g^2(t) λ(t)=g2(t)被称作为likelihood weighting function。利用这个加权函数,说明可以利用score base的方法来提高概率分布的极大似然估计。

How to solve the reverse SDE

利用数值SDE求解器来求解reverse SDE,有许多方法,如Euler-Maruyama methodMilstein method, 和 stochastic Runge-Kutta methods

reverse SDE有两个重要的特点:

  1. 可以通过******Time-Dependent Score-Based Model s θ ( x , t ) s_\theta(x, t) sθ(x,t)来估计 ∗ ∗ ∗ ∗ ∗ ∗ ∇ x log ⁡ p t ( x ) ******\nabla_x\log p_t(x) xlogpt(x)
  2. 我们需要求的只有边际分布 p t ( x ) p_t(x) pt(x),不同的时间步之间存在任意的相关性。也即是说不需要按reverse SDE的顺序逐步采样来求 p t ( x ) p_t(x) pt(x)

由于这两点,我们可以使用MCMC采样来微调从数值 SDE 求解器获得reverse SDE的解。特别的,提出了Predictor-Corrector samplers。predictor可以是任意的numeric SDE 求解器来根据 x ( t ) ∼ p t ( x ) x(t)\sim p_t(x) x(t)pt(x)预测 x ( t + Δ t ) ∼ p t + Δ t ( x ) x(t+\Delta t)\sim p_{t+\Delta t}(x) x(t+Δt)pt+Δt(x)。corrector是利用score function(例如Langevin dynamicsHamiltonian Monte Carlo)的MCMC步骤。

详细的,在每一个时间步,首先利用predictor去选择一个合适的时间步长 Δ t < 0 \Delta t < 0 Δt<0,然后根据样本 x ( t ) x(t) x(t)来预测 x ( t + Δ t ) x(t+\Delta t) x(t+Δt)。然后corrector根据score-based model s θ ( x , t + Δ t ) s_\theta(x, t+\Delta t) sθ(x,t+Δt)运行几步来提高 x ( t + Δ t ) x(t+\Delta t) x(t+Δt)的质量。最后 x ( t + Δ t ) x(t+\Delta t) x(t+Δt)就会变为来自 p t + Δ t ( x ) p_{t+\Delta t}(x) pt+Δt(x)的高质量样本。

Probability flow ODE

Langevin MCMC方法并无法精确的计算其中的似然函数。但一种基于常微分方程的求解器可以做到。

在不改变随机微分方程中随机过程的边缘分布的情况下,可以将任何的SDE转换为ODE(常微分方程)。因此,通过求解ODE方程,可以从reverse SDE的边缘分布中采样数据,它的ODE方程如下:

d x = [ f ( x , t ) − 1 2 g 2 ( t ) ∇ x log ⁡ p t ( x ) ] d t \mathrm{d} \mathbf{x}=\left[\mathbf{f}(\mathbf{x}, t)-\frac{1}{2} g^{2}(t) \nabla_{\mathbf{x}} \log p_{t}(\mathbf{x})\right] \mathrm{d} t dx=[f(x,t)21g2(t)xlogpt(x)]dt

下图描述了SDE和Probability flow ODE的轨迹,可以看出求解Probability flow ODE的轨迹所获得边缘分布和SDE相同。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值