# DDPMScheduler

Jonathan Ho、Ajay Jain、Pieter Abbeel による [Denoising Diffusion Probabilistic Models](https://huggingface.co/papers/2006.11239)（DDPM）は、同名の拡散ベースのモデルを提案しています。🤗 Diffusers ライブラリの文脈では、DDPM は論文に記述された離散型ノイズ除去スケジューラおよびパイプラインを指します。

論文の抄録は以下の通りです：

*非平衡熱力学の考察に着想を得た潜在変数モデルの一種である拡散確率モデルを用いた、高品質な画像合成の結果を提示します。私たちの最良の結果は、拡散確率モデルと Langevin 動力学を用いた去噪スコアマッチングとの新たな関連性に基づいて設計された重み付き変分境界で訓練することで得られ、私たちのモデルは、自己回帰デコーディングの一般化と解釈できる漸進的有圧縮スキームを自然に許容します。無条件 CIFAR10 データセットでは、Inception スコア 9.46 と、最先端の FID スコア 3.17 を達成しました。256x256 LSUN では、ProgressiveGAN と同様のサンプル品質を得ました。実装は [この https URL](https://github.com/hojonathanho/diffusion) で利用可能です。*

## DDPMScheduler[[diffusers.DDPMScheduler]]

#### diffusers.DDPMScheduler[[diffusers.DDPMScheduler]]

```python
diffusers.DDPMScheduler(num_train_timesteps: int = 1000, beta_start: float = 0.0001, beta_end: float = 0.02, beta_schedule: typing.Literal['linear', 'scaled_linear', 'squaredcos_cap_v2', 'sigmoid'] = 'linear', trained_betas: numpy.ndarray | list[float] | None = None, variance_type: typing.Literal['fixed_small', 'fixed_small_log', 'fixed_large', 'fixed_large_log', 'learned', 'learned_range'] = 'fixed_small', clip_sample: bool = True, prediction_type: typing.Literal['epsilon', 'sample', 'v_prediction'] = 'epsilon', thresholding: bool = False, dynamic_thresholding_ratio: float = 0.995, clip_sample_range: float = 1.0, sample_max_value: float = 1.0, timestep_spacing: typing.Literal['linspace', 'leading', 'trailing'] = 'leading', steps_offset: int = 0, rescale_betas_zero_snr: bool = False)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L137)

**パラメータ:**

num_train_timesteps (`int`、デフォルトは`1000`) : モデルを学習するための拡散ステップ数。

beta_start (`float`、デフォルトは`0.0001`) : 推論の初期`beta`値。

beta_end (`float`、デフォルトは `0.02`) : 最終的な `beta` の値。

beta_schedule (`"linear"`、`"scaled_linear"`、`"squaredcos_cap_v2"`、または `"sigmoid"`、デフォルトは `"linear"`) : ベータスケジュールです。ベータの範囲を、モデルのステップ処理に使用するベータのシーケンスへマッピングします。

trained_betas (`np.ndarray`、*任意*) : `beta_start` と `beta_end` を使わずに、コンストラクタに直接渡すためのベータの配列。

variance_type (`"fixed_small"`、`"fixed_small_log"`、`"fixed_large"`、`"fixed_large_log"`、`"learned"`、または `"learned_range"`、デフォルトは `"fixed_small"`) : 去噪サンプルにノイズを追加する際に、分散をクリップします。

clip_sample (`bool`、デフォルトは`True`) : 数値の安定性を確保するために、予測サンプルをクリップします。

clip_sample_range (`float`、デフォルトは`1.0`) : サンプルクリッピングの最大振幅。`clip_sample=True`の場合にのみ有効です。

prediction_type (`"epsilon"`, `"sample"`, or `"v_prediction"`, defaults to `"epsilon"`) : Prediction type of the scheduler function; can be `epsilon` (predicts the noise of the diffusion process), `sample` (directly predicts the noisy sample`) or `v_prediction` (see section 2.4 of [Imagen Video](https://huggingface.co/papers/2210.02303) paper).

thresholding (`bool`、デフォルトは`False`) : 「動的閾値処理」メソッドを使用するかどうか。Stable Diffusion などの潜在空間拡散モデルには適していません。

dynamic_thresholding_ratio (`float`、デフォルトは`0.995`) : 動的閾値化手法における比率。`thresholding=True`の場合にのみ有効です。

sample_max_value (`float`、デフォルトは`1.0`) : 動的閾値処理の閾値。`thresholding=True`の場合にのみ有効です。

timestep_spacing (`"linspace"`, `"leading"`, or `"trailing"`, defaults to `"leading"`) : The way the timesteps should be scaled. Refer to Table 2 of the [Common Diffusion Noise Schedules and Sample Steps are Flawed](https://huggingface.co/papers/2305.08891) for more information.

steps_offset (`int`、デフォルトは`0`) : 一部のモデルファミリーで必要とされる、推論ステップに追加されるオフセットです。

rescale_betas_zero_snr (`bool`、デフォルトは`False`) : ベータを再スケーリングして、終端SNRをゼロにするかどうか。これにより、モデルは中間の明るさに限定されず、非常に明るく暗いサンプルを生成できるようになります。[`--offset_noise`](https://github.com/huggingface/diffusers/blob/74fd735eb073eb1d774b1ab4154a0876eb82f055/examples/dreambooth/train_dreambooth.py#L506)とゆるく関連しています。

`DDPMScheduler` は、デノイジング・スコア・マッチングとランジュバン力学サンプリングの間の関連性を検討します。

このモデルは [SchedulerMixin](/docs/diffusers/main/ja/api/schedulers/overview#diffusers.SchedulerMixin) と [ConfigMixin](/docs/diffusers/main/ja/api/configuration#diffusers.ConfigMixin) を継承しています。読み込みや保存など、ライブラリがすべてのスケジューラに対して実装している汎用的なメソッドについては、スーパークラスのドキュメントを参照してください。

#### add_noise[[diffusers.DDPMScheduler.add_noise]]

```python
add_noise(original_samples: Tensor, noise: Tensor, timesteps: IntTensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L569)

**パラメータ:**

original_samples (`torch.Tensor`) : ノイズが加えられる元のサンプル。

noise (`torch.Tensor`) : サンプルに追加するノイズ。

timesteps (`torch.IntTensor`) : 各サンプルのノイズレベルを示すタイムステップ。

**戻り値:** `torch.Tensor`

ノイズ付きサンプル。

各タイムステップにおけるノイズの大きさに従って、元のサンプルにノイズを追加します（これはフォワード拡散プロセスです）。

#### get_velocity[[diffusers.DDPMScheduler.get_velocity]]

```python
get_velocity(sample: Tensor, noise: Tensor, timesteps: IntTensor)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L611)

**パラメータ:**

sample (`torch.Tensor`) : 入力サンプル。

noise (`torch.Tensor`) : ノイズのテンソル。

timesteps (`torch.IntTensor`) : 速度計算用のタイムステップ。

**戻り値:** `torch.Tensor`

計算された速度。

速度の式に従って、サンプルとノイズから速度予測を計算します。

#### previous_timestep[[diffusers.DDPMScheduler.previous_timestep]]

```python
previous_timestep(timestep: int)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L648)

**パラメータ:**

timestep (`int`) : 現在のタイムステップ。

**戻り値:** `int` または `torch.Tensor`

直前のタイムステップ。

拡散チェーンにおける直前のタイムステップを計算します。

#### scale_model_input[[diffusers.DDPMScheduler.scale_model_input]]

```python
scale_model_input(sample: Tensor, timestep: int | None = None)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L257)

**パラメータ:**

sample (`torch.Tensor`) : 入力サンプル。

timestep (`int`, *任意*) : 拡散チェーンにおける現在のタイムステップ。

**戻り値:** `torch.Tensor`

スケーリングされた入力サンプル。

現在のタイムステップに応じて、デノイジングモデルの入力をスケーリングする必要があるスケジューラとの相互運用性を保証します。

#### set_timesteps[[diffusers.DDPMScheduler.set_timesteps]]

```python
set_timesteps(num_inference_steps: int = None, device: typing.Union[str, torch.device] = None, timesteps: list[int] | None = None)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L274)

**パラメータ:**

num_inference_steps (`int`、*任意*) : 事前学習済みモデルでサンプルを生成する際に使用する拡散ステップ数。使用する場合、`timesteps` は `None` である必要があります。

device (`str` または `torch.device`、*任意*) : timesteps を移動先とするデバイスです。`None` の場合、timesteps は移動されません。

timesteps (`list[int]`、*任意*) : 任意の間隔でタイムステップをサポートするために使用するカスタムタイムステップ。`None` の場合、タイムステップ間の等間隔というデフォルトのタイムステップ間隔戦略が使用されます。`timesteps` が渡された場合、`num_inference_steps` は `None` でなければなりません。

拡散チェーンで使用される離散タイムステップを設定します（推論の実行前に実行する必要があります）。

#### ステップ[[diffusers.DDPMScheduler.step]]

```python
step(model_output: Tensor, timestep: int, sample: Tensor, generator: typing.Optional[torch.Generator] = None, return_dict: bool = True)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L461)

**パラメータ:**

model_output (`torch.Tensor`) : 学習済み拡散モデルからの直接の出力です。

timestep (`int`) : 拡散チェーンにおける現在の離散タイムステップ。

sample (`torch.Tensor`) : 拡散プロセスによって生成されたサンプルの現在のインスタンス。

generator (`torch.Generator`, *任意*) : 乱数生成器。

return_dict (`bool`、デフォルトは `True`) : [DDPMSchedulerOutput](/docs/diffusers/main/ja/api/schedulers/ddpm#diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput) を返すか、`tuple` を返すかを指定します。

**戻り値:** [DDPMSchedulerOutput](/docs/diffusers/main/ja/api/schedulers/ddpm#diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput) または `tuple`

`True` の場合、[DDPMSchedulerOutput](/docs/diffusers/main/ja/api/schedulers/ddpm#diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput) が返され、それ以外の場合は、最初の要素がサンプルテンソルであるタプルが返されます。

前回のタイムステップからのサンプルを、SDEを逆方向に適用することで予測します。この関数は、学習済みモデルの出力（最もよく使われるのは予測ノイズ）から拡散過程を伝播させます。

## DDPMSchedulerOutput[[diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput]]

#### diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput[[diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput]]

```python
diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput(prev_sample: Tensor, pred_original_sample: typing.Optional[torch.Tensor] = None)
```

[ソース](https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_ddpm.py#L31)

**パラメータ:**

prev_sample (`torch.Tensor`、画像の場合は `(batch_size, num_channels, height, width)` の形状) : 前のタイムステップの計算済みサンプル `(x_{t-1})`。`prev_sample` は、デノイジングループの次のモデル入力として使用する必要があります。

pred_original_sample (`torch.Tensor`、画像の場合は形状 `(batch_size, num_channels, height, width)`) : 現在のタイムステップにおけるモデルの出力に基づいて予測された、ノイズ除去されたサンプル `(x_{0})`。`pred_original_sample` は、進捗のプレビューやガイダンスに使用できます。

スケジューラの`step`関数の出力用のクラス。

