Diffusers documentation

ScoreSdeVeScheduler

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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

< >

( 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

< >

( original_samples: Tensornoise: Tensortimesteps: Tensor ) → torch.Tensor

参数

  • original_samples (torch.Tensor) — 将添加噪声的原始样本。
  • noise (torch.Tensor) — 要添加到原始样本中的噪声张量。
  • timesteps (torch.Tensor) — 用于添加噪声的时间步,决定调度器中的噪声水平。

返回值

torch.Tensor

根据时间步调度对添加的噪声进行缩放后的含噪样本。

根据指定时间步的噪声调度,向原始样本添加噪声。这是前向扩散过程。

get_adjacent_sigma

< >

( timesteps: Tensort: Tensor ) → torch.Tensor

参数

  • timesteps (torch.Tensor) — 离散时间步索引的张量。
  • t (torch.Tensor) — 连续时间步值的张量。

返回值

torch.Tensor

相邻(前一个)时间步索引的 sigma 值。

返回与上一个时间步索引对应的 sigma 值。当时间步为零(即没有上一个时间步)时,返回零。

scale_model_input

< >

( sample: Tensortimestep: int | None = None ) → torch.Tensor

参数

  • sample (torch.Tensor) — 输入样本。
  • timestep (int, 可选) — 扩散链中的当前时间步。

返回值

torch.Tensor

经过缩放的输入样本。

确保与那些需要根据当前时间步对去噪模型输入进行缩放的调度器之间的可互换性。

set_sigmas

< >

( num_inference_steps: intsigma_min: float | None = Nonesigma_max: float | None = Nonesampling_eps: float | None = None )

参数

  • num_inference_steps (int) — 使用预训练模型生成样本时所用的扩散步数。
  • sigma_min (float, 可选) — 初始噪声尺度值(覆盖在调度器实例化时给定的值)。
  • sigma_max (float, 可选) — 最终的噪声尺度值(覆盖在调度器实例化时给定的值)。
  • sampling_eps (float, 可选) — 最终时间步的值(覆盖在调度器实例化时给定的值)。

设置扩散链所使用的噪声尺度(需在推理前运行)。drift 和 diffusion 控制样本更新中这两个分量的权重。

set_timesteps

< >

( num_inference_steps: intsampling_eps: float | None = Nonedevice: typing.Union[str, torch.device, NoneType] = None )

参数

  • num_inference_steps (int) — 使用预训练模型生成样本时所用的扩散步数。
  • sampling_eps (float, 可选) — 最终时间步的值(覆盖在调度器实例化时给定的值)。
  • device (str 或 torch.device,可选) — 时间步应移动到的设备。如果为 None,则不移动时间步。

设置用于扩散链的连续时间步(需在推理之前运行)。

step_correct

< >

( 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

< >

( 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

< >

( prev_sample: Tensorprev_sample_mean: Tensor )

参数

  • prev_sample(对于图像,形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 前一时间步的已计算样本 (x_{t-1})。在去噪循环中,应将 prev_sample 用作下一个模型输入。
  • prev_sample_mean(对于图像,形状为 (batch_size, num_channels, height, width) 的 torch.Tensor)— 之前时间步上 prev_sample 的平均值。

调度器 step 函数输出的输出类。

Update on GitHub