Score-Based Generative Models
Brief Introduction
在从VAE到DDPM一文中,我们从 VAE 出发得到了 DDPM 的理论框架,发现 DDPM 可以看作是具有人为指定的后验分布(即前向过程)的层次化 VAE。这一角度的代表论文是 2015 年的 Sohl-Dickstein et al.[1] 和 2020 年的 DDPM[2]。与此同时,宋飏博士从 score matching + Langevin dynamics (SMLD)[3] 的角度出发得到了与 DDPM 本质相同的模型,称为 score-based generative models. 两个角度出发点截然不同,但殊途同归,也是非常有意思了。
Score Function
考虑一个以
具体到能量模型,将
Score Matching
为了训练模型
Implicit Score Matching
论文[5]的作者指出,我们可以使用一个分部积分去掉
一元函数分部积分
设
设
应用分部积分,并且假设
可以看见,现在的优化目标期望里面已经没有
当然,上述推导过程要成立也有一些条件,比如用分部积分要求
Denoising Score Matching
上文中,我们使用分部积分巧妙地去掉了 torch.autograd.grad()
实现,但反向传播次数增加了
直观上,
Sliced Score Matching
本节其实与以去噪为基础的扩散模型关系不大,但为了完整性,同时考虑到它也是宋飏大佬的工作,就顺便读了读论文。
Denoising score matching 虽然解决了 implicit score matching 无法 scale 到深度网络和高维数据的问题,但是本身也有两个问题:只能学习带有噪声的分布、性能对 noise level 很敏感。而宋飏等人在 2020 年提出的 sliced score matching[7] 是另一种解决方法,其思想是把
与原始 score matching 类似,我们可以用分部积分去掉
于是我们就得到了 SSM 的 implicit 形式:
说了这么多,
另外,每次采样的投影向量个数也是影响效率的重要因素。采样越多,近似越准确,计算量也越大,所以这是一个 trade-off. 论文作者实验发现每次采样 1 个向量效果就不错了。
Langevin Dynamics
在模型学到了 score function 之后,我们怎么从中采样呢?直观上,score function
Noise Perturbations
截至目前,我们用 score matching 训练模型预测 score function,然后通过 Langevin dynamics 采样,看起来似乎已经很完美了。但这个做法在实际中很难 work. 究其原因,罪魁祸首在于 manifold hypothesis,即数据分布在高维空间中的低维流形上。换句话说,空间中绝大部分都是低密度区域。这将导致两个问题:
首先,从训练层面上讲,低密度区域数据量少(甚至没有),无法较好地训练模型,导致模型预测的 score function 不准确:
又由于我们的初始化点极有可能落在低密度区域中,所以不准确的 score function 将对采样过程产生极大的影响。
其次,即便 score function 能被准确预测,但对于被低密度区域隔开的两个分布来说,Langevin dynamics 很难在有限步数内收敛到正确的分布(尽管 Langevin dynamics 在理论上能收敛到正确的分布,但可能需要很小的步长和很多的步数),如图所示:
真实情况是右上的正态分布权重更大,但中图的采样结果显示两个分布被予以了几乎相同的权重。
宋飏等人提出解决这两个问题的办法是:给数据添加高斯噪声,并且随着 Langevin dynamics 的进行逐渐减小 noise level(退火)。添加噪声后的数据遍布整个空间,填充了低密度区域,因此模型能够得到更多的监督信号,Langevin dynamics 也能更快的收敛到高密度区域;同时,逐渐减小的噪声使得带噪分布最终收敛到真实分布,因此也能保证采样质量不受影响。
形式化地说,我们确定一个逐渐减小的噪声序列
怎么选取权重
训练完成后,我们就可以随机初始化采样点,逐渐减小 noise level,在每个 level 下通过 Langevin dynamics 以步长
其中步长设置为
这一代的 NCSN 已然能与 GAN 媲美,但局限在 32x32 的大小。在后续的工作[4]中,宋飏等人探索了更多的技巧,进一步地提升了图像生成质量,并能稳定生成 256x256 的图片。这些技巧包括:
选择
与训练数据中两两之间的最大欧氏距离差不多大。选择
为公比为 的等比序列,且将 NCSN 参数化为
,其中 是一个无条件的网络。选择
(每个 noise level 下 Langevin dynamics 的步数)在可承受范围尽可能大,并选择 (Langevin dynamics 里与步长有关的参数)使得一个复杂的式子(懒得抄了)约等于 .在采样的时候使用 EMA model.
具体内容和分析本文不再叙述,感兴趣的读者可以去看论文。
Connection to DDPM
在引言里,我们说 SMLD 和 DDPM 本质是相同的,现在来看看为什么。
Forward Process
DDPM 与 SMLD 的加噪过程分别为:
为进一步探索二者的关系,考虑对
- 根据
式有: . - 根据
式有: .
Reverse Process
虽然 DDPM 和 SMLD 在加噪过程和优化目标上保持了惊人的一致,但是在生成过程上还是有所区别。对于 DDPM,我们构建逆向的马尔可夫链,每一步从
References
- Sohl-Dickstein, Jascha, Eric Weiss, Niru Maheswaranathan, and Surya Ganguli. Deep unsupervised learning using nonequilibrium thermodynamics. In International Conference on Machine Learning, pp. 2256-2265. PMLR, 2015. ↩︎
- Ho, Jonathan, Ajay Jain, and Pieter Abbeel. Denoising diffusion probabilistic models. Advances in Neural Information Processing Systems 33 (2020): 6840-6851. ↩︎
- Song, Yang, and Stefano Ermon. Generative modeling by estimating gradients of the data distribution. Advances in Neural Information Processing Systems 32 (2019). ↩︎
- Song, Yang, and Stefano Ermon. Improved techniques for training score-based generative models. Advances in neural information processing systems 33 (2020): 12438-12448. ↩︎
- Hyvärinen, Aapo, and Peter Dayan. Estimation of non-normalized statistical models by score matching. Journal of Machine Learning Research 6, no. 4 (2005). ↩︎
- Vincent, Pascal. A connection between score matching and denoising autoencoders. Neural computation 23, no. 7 (2011): 1661-1674. ↩︎
- Song, Yang, Sahaj Garg, Jiaxin Shi, and Stefano Ermon. Sliced score matching: A scalable approach to density and score estimation. In Uncertainty in Artificial Intelligence, pp. 574-584. PMLR, 2020. ↩︎
- Yang Song. Generative Modeling by Estimating Gradients of the Data Distribution. https://yang-song.net/blog/2021/score/ ↩︎
- Denoising Diffusion-based Generative Modeling: Foundations and Applications. https://cvpr2022-tutorial-diffusion-models.github.io ↩︎
- Chih-Sheng Chen. Score Matching 系列 (一) Non-normalized 模型估計. https://bobondemon.github.io/2022/01/08/Estimation-of-Non-Normalized-Statistical-Models-by-Score-Matching/ ↩︎
- Chih-Sheng Chen. Score Matching 系列 (二) Denoising Score Matching (DSM) 改善效率並可 Scalable. https://bobondemon.github.io/2022/03/06/A-Connection-Between-Score-Matching-and-Denoising-Autoencoders/ ↩︎
- Chih-Sheng Chen. Score Matching 系列 (三) Sliced Score Matching (SSM) 同時保持效率和效果. https://bobondemon.github.io/2022/03/06/Sliced-Score-Matching-A-Scalable-Approach-to-Density-and-Score-Estimation/ ↩︎
- 扩散模型与能量模型,Score-Matching和SDE,ODE的关系 - 中森的文章 - 知乎 https://zhuanlan.zhihu.com/p/576779879 ↩︎
- Yang Song. Sliced Score Matching: A Scalable Approach to Density and Score Estimation. https://yang-song.net/blog/2019/ssm/ ↩︎