Diffusers documentation
ScoreSdeVeScheduler
ScoreSdeVeScheduler
ScoreSdeVeScheduler 是一种方差爆炸型随机微分方程(SDE)调度器。它由 Yang Song、Jascha Sohl-Dickstein、Diederik P. Kingma、Abhishek Kumar、Stefano Ermon 和 Ben Poole 在论文 Score-Based Generative Modeling through Stochastic Differential Equations 中提出。
该论文的摘要如下:
从数据中生成噪声很容易,而从噪声中生成数据则是生成式建模。我们提出了一个随机微分方程(SDE),它通过缓慢注入噪声,将复杂的数据分布平滑地转换为已知的先验分布;以及一个对应的逆时间 SDE,它通过缓慢去除噪声,将先验分布转换回数据分布。关键在于,逆时间 SDE 仅依赖于扰动数据分布的时变梯度场(又称 score)。借助基于 score 的生成式建模的进展,我们可以使用神经网络准确估计这些 score,并利用数值 SDE 求解器生成样本。我们证明,该框架涵盖了基于 score 的生成式建模和扩散概率建模中的先前方法,从而支持新的采样流程和新的建模能力。特别是,我们引入了一个预测器-校正器框架,以校正离散化逆时间 SDE 演化过程中的误差。我们还推导了一个等价的神经 ODE,它从与 SDE 相同的分布中采样,但额外支持精确的似然计算,并提高了采样效率。此外,我们提供了一种使用基于 score 的模型求解逆问题的新方法,并通过类条件生成、图像修复和图像着色实验进行了验证。结合多种架构改进,我们在 CIFAR-10 上的无条件图像生成中取得了突破性性能,Inception score 达到 9.89,FID 为 2.20,似然度为 2.99 bits/dim,并首次展示了基于 score 的生成式模型对 1024 x 1024 图像的高保真生成。
ScoreSdeVeScheduler
class diffusers.ScoreSdeVeScheduler
< source >( num_train_timesteps: int = 2000snr: float = 0.15sigma_min: float = 0.01sigma_max: float = 1348.0sampling_eps: float = 1e-05correct_steps: int = 1 )
参数
- num_train_timesteps (
int,默认为 2000) — 用于训练模型的扩散步数。 - snr (
float,默认为 0.15) — 用于对从model_output样本(来自网络)到随机噪声的步长进行加权的系数。 - sigma_min (
float,默认为 0.01) — 采样过程中 sigma 序列的初始噪声尺度。最小 sigma 值应反映数据的分布。 - sigma_max (
float,默认值为 1348.0) — 用于传入模型的连续时间步范围的最大值。 - sampling_eps (
float,默认为 1e-5) — 采样的结束值,其中时间步从 1 逐步递减至 epsilon。 - correct_steps (
int,默认为 1) — 对生成的样本执行的校正步数。
ScoreSdeVeScheduler 是一个方差爆炸随机微分方程(SDE)调度器。
该模型继承自 SchedulerMixin 和 ConfigMixin。有关该库为所有调度器实现的通用方法(例如加载和保存),请参阅父类文档。
add_noise
< source >( original_samples: Tensornoise: Tensortimesteps: Tensor ) → torch.Tensor
根据指定时间步的噪声调度,向原始样本添加噪声。这是前向扩散过程。
get_adjacent_sigma
< source >( timesteps: Tensort: Tensor ) → torch.Tensor
返回与上一个时间步索引对应的 sigma 值。当时间步为零(即没有上一个时间步)时,返回零。
scale_model_input
< source >( sample: Tensortimestep: int | None = None ) → torch.Tensor
确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。
set_sigmas
< source >( num_inference_steps: intsigma_min: float | None = Nonesigma_max: float | None = Nonesampling_eps: float | None = None )
设置扩散链所使用的噪声尺度(需在推理前运行)。drift 和 diffusion 控制样本更新中这两个分量的权重。
set_timesteps
< source >( num_inference_steps: intsampling_eps: float | None = Nonedevice: typing.Union[str, torch.device, NoneType] = None )
设置用于扩散链的连续时间步(需在推理之前运行)。
step_correct
< source >( model_output: Tensorsample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → SchedulerOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - generator (
torch.Generator,可选) — 随机数生成器。 - return_dict (
bool,可选,默认为True) — 是否返回 SchedulerOutput 或tuple。
返回值
SchedulerOutput 或 tuple
If return_dict is True, SchedulerOutput is returned, otherwise a
tuple is returned where the first element is the sample tensor.
根据网络的 model_output 对预测样本进行修正。这通常会在完成上一步时间步的预测后反复执行。
step_pred
< source >( model_output: Tensortimestep: intsample: Tensorgenerator: typing.Optional[torch.Generator] = Nonereturn_dict: bool = True ) → SdeVeOutput or tuple
参数
- model_output (
torch.Tensor) — 学习到的扩散模型的直接输出。 - timestep (
int) — 扩散链中的当前离散时间步。 - sample (
torch.Tensor) — 扩散过程生成的样本的当前实例。 - generator (
torch.Generator,可选) — 随机数生成器。 - return_dict (
bool,可选,默认为True) — 是否返回 SdeVeOutput 或tuple。
返回值
SdeVeOutput 或 tuple
If return_dict is True, SdeVeOutput is returned, otherwise a tuple
is returned where the first element is the sample tensor.
通过逆向 SDE 预测上一时间步的样本。此函数将扩散过程从学习到的模型输出(通常是预测的噪声)进行传播。
SdeVeOutput
class diffusers.schedulers.scheduling_sde_ve.SdeVeOutput
< source >( prev_sample: Tensorprev_sample_mean: Tensor )
调度器 step 函数输出的输出类。