【理论推导】基于分数的生成模型Score-based Model

文章介绍了生成模型的两大类:基于似然的模型(如VAE、正则化流模型)和隐式生成模型(如GAN)。重点讨论了ScoreMatching技术,这是一种用于估计非规范化统计模型的方法,解决了概率分布归一化问题。ScoreMatching通过优化模型参数使其输出的梯度逼近真实数据分布的梯度。此外,文章还提到了SlicedScoreMatching和DenoisingScoreMatching,它们是ScoreMatching的变体,分别解决了计算效率和噪声样本分布的问题。DenoisingScoreMatching通过在原始数据上添加噪声来优化模型,从而生成更准确的数据分布。

生成模型分类

已有的生成模型大致可以分为两类

  • likelihood-based model:以 VAE 和 normalizing flow model 为主,直接拟合数据分布,但通常对网络模型的结构设计提出了较大限制
  • implicit generative model:以 GAN 为主,使用判别器间接判断输出的数据是否符合数据分布,存在训练困难,容易训练失败以及模式崩塌等问题

Score Matching & Non-Normalized Distribution

Score Matching 最早在 2005 年《Estimation of Non-Normalized Statistical Models by Score Matching》提出,主要是用于解决概率分布的归一化的问题。在生成模型中,我们常希望使用含参模型 p θ ( x ) p_\theta(x) pθ(x) 给出的概率密度函数能够拟合真实的概率密度函数 p ( x ) p(x) p(x) p θ ( x ) p_\theta(x) pθ(x) 作为一个概率密度函数须满足归一化性质 ∫ x p θ ( x ) d x = 1 \int_x p_{\theta}(x)dx = 1 xpθ(x)dx=1,因此,我们通常使用模型 q θ ( x ) q_\theta(x) qθ(x) 给出一个未归一化的概率密度函数,然后使用归一化项 Z ( θ ) Z(\theta) Z(θ) 来保证 p θ ( x ) p_\theta(x) pθ(x) 的性质,即
p θ ( x ) = 1 Z ( θ ) q θ ( x ) p_\theta(x) = \frac{1}{Z(\theta)}q_\theta(x) pθ(x)=Z(θ)1qθ(x)
其中 Z ( θ ) Z(\theta) Z(θ) 为与样本无关的一个常量。由于 Z ( θ ) Z(\theta) Z(θ) 的存在,无论是基于梯度的优化过程还是正向推理都变得很难计算,因此,考虑使 p θ ( x ) p_\theta(x) pθ(x) 关于输入的梯度逼近 p ( x ) p(x) p(x) 关于输入的梯度
在这里插入图片描述
▽ x q θ ( x ) = ▽ x p θ ( x ) ≈ ▽ x p ( x ) \triangledown_x q_\theta(x) = \triangledown_x p_\theta(x) \approx \triangledown_x p(x) xqθ(x)=xpθ(x)xp(x)
形式化地,我们定义分数函数 ψ : R n → R n \psi : \R^n\rightarrow \R^n ψ:RnRn 如下
ψ ( x ) = ▽ x p ( x ) \psi(x) = \triangledown_x p(x) ψ(x)=xp(x)
我们利用MSE损失函数找到最优参数 θ \theta θ 的过程称作是 Score Matching,损失函数定义为
J ESM ( θ ) = 1 2 ∫ x p ( x ) ∣ ∣ ψ θ ( x ) − ψ ( x ) ∣ ∣ 2 2 d x = E x ∼ p ( x ) [ 1 2 ∣ ∣ ψ θ ( x ) − ψ ( x ) ∣ ∣ 2 2 ] \begin{align} J_\text{ESM}(\theta) &= \frac{1}{2} \int_{x} p(x) ||\psi_\theta(x) - \psi(x)||_2^2 dx \nonumber \\ &= \mathbb E_{x\sim p(x)} [\frac{1}{2}||\psi_\theta(x) - \psi(x)||_2^2] \end{align} JESM(θ)=21xp(x)∣∣ψθ(x)ψ(x)22dx=Exp(x)[21∣∣ψθ(x)ψ(x)22]
这种损失函数的表达形式也被称为显式分数匹配 (Explicit Score Matching)

J ESM J_\text{ESM} JESM 进行展开,有
J ( θ ) = 1 2 ∫ x p ( x ) ∣ ∣ ψ θ 2 ( x ) ∣ ∣ 2 2 d x − ∫ x p ( x ) ψ θ T ( x ) ψ ( x ) d x + 1 2 ∫ x p ( x ) ∣ ∣ ψ 2 ( x ) ∣ ∣ 2 2 d x = 1 2 ∫ x p ( x ) ∣ ∣ ψ θ 2 ( x ) ∣ ∣ 2 2 d x − ∫ x p ( x ) ψ θ T ( x ) ψ ( x ) d x + C \begin{align} J(\theta) &= \frac{1}{2} \int_{x} p(x) ||\psi_\theta^2(x)||^2_2dx - \int_{x} p(x) \psi_\theta^T(x)\psi(x)dx + \frac{1}{2} \int_{x} p(x) ||\psi^2(x)||^2_2dx \nonumber \\ &= \frac{1}{2} \int_{x} p(x) ||\psi_\theta^2(x)||^2_2dx - \int_{x} p(x) \psi_\theta^T(x)\psi(x)dx + C \end{align} J(θ)=21xp(x)∣∣ψθ2(x)22dxxp(x)ψθT(x)ψ(x)dx+21xp(x)∣∣ψ2(x)22dx=21xp(x)∣∣ψθ2(x)22dxxp(x)ψθT(x)ψ(x)dx+C
对于第二项进行维度展开,有
∫ x p ( x ) ψ θ T ( x ) ψ ( x ) d x = ∑ i = 1 n ∫ x p ( x ) ψ θ ( i ) ( x ) ψ ( i ) ( x ) d x = ∑ i = 1 n ∫ x p ( x ) ψ θ ( i ) ( x ) ∂ log ⁡ p ( x ) ∂ x ( i ) d x = ∑ i = 1 n ∫ x ψ θ ( i ) ( x ) ∂ p ( x ) ∂ x ( i ) d x \begin{align} \int_{x} p(x) \psi_\theta^T(x)\psi(x)dx &= \sum_{i=1}^n \int_{x} p(x) \psi_\theta^{(i)}(x)\psi^{(i)}(x)dx \nonumber \\&= \sum_{i=1}^n \int_{x} p(x) \psi_\theta^{(i)}(x)\frac{\partial \log p(x)}{\partial x^{(i)}}dx \nonumber \\&= \sum_{i=1}^n \int_{x} \psi_\theta^{(i)}(x)\frac{\partial p(x)}{\partial x^{(i)}}dx \end{align} xp(x)ψθT(x)ψ(x)dx=i=1nxp(x)ψθ(i)(x)ψ(i)(x)dx=i=1nxp(x)ψθ(i)(x)x(i)logp(x)dx=i=1nxψθ(i)(x)x(i)p(x)dx
我们考虑 i = 1 i=1 i=1 时的情况,由分部积分公式
lim ⁡ a → ∞ , b → − ∞ f ( a , x ( 2 ) , . . . , x ( n ) ) g ( a , x ( 2 ) , . . . , x ( n ) ) − f ( b , x ( 2 ) , . . . , x ( n ) ) g ( b , x ( 2 ) , . . . , x ( n ) ) = ∫ − ∞ ∞ f ( x ) ∂ g ( x ) ∂ x ( 1 ) d x ( 1 ) + ∫ − ∞ ∞ g ( x ) ∂ f ( x ) ∂ x ( 1 ) d x ( 1 ) \lim_{a\rightarrow \infty , b\rightarrow -\infty} f(a, x^{(2)}, ... , x^{(n)}) g(a, x^{(2)}, ... , x^{(n)}) - f(b, x^{(2)}, ... , x^{(n)}) g(b, x^{(2)}, ... ,

### 基于SDE的扩散模型公式与实现 基于随机微分方程(Stochastic Differential Equation, SDE)的扩散模型是一种强大的生成建模方法,它通过定义一个前向过程来逐渐破坏数据结构,并利用反向过程恢复原始数据。以下是关于此类模型的核心公式及其可能的实现方式。 #### 核心公式 在基于SDE的扩散模型中,通常会涉及两种主要类型的SDE:Variance Exploding (VE)-SDE 和 Variance Preserving (VP)-SDE[^3]。这两种SDE分别对应不同的扩散机制: - **Variance Exploding (VE)-SDE** 它描述了一种噪声随时间增加的方式,具体形式如下: \[ d\mathbf{x} = \sqrt{\beta(t)}dW_t, \] 其中 \( W_t \) 是标准布朗运动,而 \( \beta(t) \) 控制噪声的增长速率。 - **Variance Preserving (VP)-SDE** 此类SDE保持总方差不变,其表达式为: \[ d\mathbf{x} = -\frac{1}{2}\beta(t)\mathbf{x}dt + \sqrt{\beta(t)}dW_t. \] 对于离散化的版本,可以得到具体的更新规则。例如,在VP-SDE下的一次迭代可写作: \[ \mathbf{x}_{t+1} = \sqrt{1-\beta_{t+1}}\mathbf{x}_t + \sqrt{\beta_{t+1}}\boldsymbol{\epsilon}, \] 其中 \( \boldsymbol{\epsilon} \sim \mathcal{N}(0, I) \)。 #### 实现细节 为了实际应用这些理论,可以通过神经网络估计分数函数并结合数值求解器完成整个流程。下面是一个简单的Python代码示例展示如何模拟这一过程: ```python import torch from torch import nn import numpy as np class ScoreNetwork(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim): super(ScoreNetwork, self).__init__() self.network = nn.Sequential( nn.Linear(input_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, output_dim) ) def forward(self, x, t): """Estimate the score function.""" combined_input = torch.cat([x, t], dim=-1) return self.network(combined_input) def vp_sde_step(x_t, beta_t, epsilon=None): """ Perform one step of VP-SDE discretization. Args: - x_t: Current state at time t. - beta_t: Beta value corresponding to current timestep. - epsilon: Optional noise term sampled from standard normal distribution. Returns: Next state after applying the update rule according to VP-SDE. """ sqrt_beta_t = torch.sqrt(beta_t) if epsilon is None: epsilon = torch.randn_like(x_t) next_x = torch.sqrt(1-beta_t)*x_t + sqrt_beta_t*epsilon return next_x # Example usage input_dim = 10 hidden_dim = 64 output_dim = input_dim score_model = ScoreNetwork(input_dim=input_dim, hidden_dim=hidden_dim, output_dim=output_dim) batch_size = 8 initial_state = torch.randn(batch_size, input_dim) time_steps = torch.linspace(start=0., end=1., steps=batch_size).unsqueeze(-1) noisy_data = initial_state.clone() for i in range(len(time_steps)): noisy_data = vp_sde_step(noisy_data, beta_t=time_steps[i]) ``` 此代码片段展示了如何构建一个基本的分数网络以及执行单步VP-SDE的过程。 #### 相关推导说明 上述公式的推导依赖于将离散分数匹配扩展到连续SDE框架的思想[^1]。这使得我们可以统一处理不同种类的扩散模型,并提供更大的灵活性以适应各种应用场景。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值