Table of Contents

Class DiffusionNoiseHelper<T>

Namespace
AiDotNet.Helpers
Assembly
AiDotNet.dll

Helper class for noise sampling operations in diffusion models.

public static class DiffusionNoiseHelper<T>

Type Parameters

T

The numeric type used for calculations.

Inheritance
DiffusionNoiseHelper<T>
Inherited Members

Remarks

This static helper provides common noise sampling operations used throughout diffusion models, ensuring consistent implementations and avoiding code duplication.

For Beginners: Diffusion models work by adding and removing noise from data. This helper provides the mathematical operations needed for that process: - Sampling Gaussian (bell-curve) noise - Computing noise schedules - Scaling noise for different timesteps

Methods

AddNoise(Tensor<T>, Tensor<T>, T, T)

Adds noise to a signal at a specified timestep using the scheduler's noise schedule.

public static Tensor<T> AddNoise(Tensor<T> signal, Tensor<T> noise, T sqrtAlphaCumprod, T sqrtOneMinusAlphaCumprod)

Parameters

signal Tensor<T>

The clean signal.

noise Tensor<T>

The noise to add.

sqrtAlphaCumprod T

The square root of cumulative alpha at this timestep.

sqrtOneMinusAlphaCumprod T

The square root of (1 - cumulative alpha) at this timestep.

Returns

Tensor<T>

The noisy signal: sqrt(alpha_cumprod) * signal + sqrt(1 - alpha_cumprod) * noise.

Remarks

For Beginners: This is how we add noise during training: - At timestep 0: Almost no noise (mostly signal) - At timestep T: Almost all noise (almost no signal) - The alphas control this blend based on timestep

ComputeSNR(T)

Computes the signal-to-noise ratio (SNR) for a given timestep.

public static T ComputeSNR(T alphaCumprod)

Parameters

alphaCumprod T

The cumulative alpha product at this timestep.

Returns

T

The SNR value: alpha / (1 - alpha).

ComputeTimestepEmbedding(int, int)

Computes sinusoidal embedding for a single timestep.

public static Vector<T> ComputeTimestepEmbedding(int timestep, int embeddingDim)

Parameters

timestep int

The timestep to embed.

embeddingDim int

The dimension of the embedding.

Returns

Vector<T>

Vector of length embeddingDim containing the embedding.

ComputeTimestepEmbeddings(int[], int)

Computes sinusoidal timestep embeddings (like in Transformers).

public static Tensor<T> ComputeTimestepEmbeddings(int[] timesteps, int embeddingDim)

Parameters

timesteps int[]

Array of timesteps to embed.

embeddingDim int

The dimension of each embedding.

Returns

Tensor<T>

Tensor of shape [batchSize, embeddingDim] containing the embeddings.

Remarks

For Beginners: This converts timestep numbers (like 100, 500, 999) into high-dimensional vectors that the neural network can understand. The sinusoidal pattern helps the network distinguish between nearby and distant timesteps.

LerpNoise(Tensor<T>, Tensor<T>, double)

Linearly interpolates between two noise tensors.

public static Tensor<T> LerpNoise(Tensor<T> noise1, Tensor<T> noise2, double t)

Parameters

noise1 Tensor<T>

First noise tensor.

noise2 Tensor<T>

Second noise tensor.

t double

Interpolation factor (0 = noise1, 1 = noise2).

Returns

Tensor<T>

Interpolated noise tensor.

SampleGaussian(int[], int?)

Samples Gaussian noise from a standard normal distribution N(0, 1).

public static Tensor<T> SampleGaussian(int[] shape, int? seed = null)

Parameters

shape int[]

The shape of the noise tensor to generate.

seed int?

Optional random seed for reproducibility.

Returns

Tensor<T>

A tensor filled with Gaussian noise.

Remarks

Uses the Box-Muller transform to convert uniform random numbers to Gaussian. The existing RandomHelper is used for thread-safe random number generation.

For Beginners: This creates random "static" like you might see on an old TV. Each value is drawn from a bell curve (Gaussian distribution) centered at 0. Most values will be close to 0, with occasional larger positive or negative values.

SampleGaussian(int[], Random)

Samples Gaussian noise using a provided random number generator.

public static Tensor<T> SampleGaussian(int[] shape, Random rng)

Parameters

shape int[]

The shape of the noise tensor to generate.

rng Random

The random number generator to use.

Returns

Tensor<T>

A tensor filled with Gaussian noise.

SampleGaussianVector(int, int?)

Samples Gaussian noise as a Vector.

public static Vector<T> SampleGaussianVector(int length, int? seed = null)

Parameters

length int

The length of the vector to generate.

seed int?

Optional random seed for reproducibility.

Returns

Vector<T>

A vector filled with Gaussian noise.

SampleGaussianVector(int, Random)

Samples Gaussian noise as a Vector using a provided random number generator.

public static Vector<T> SampleGaussianVector(int length, Random rng)

Parameters

length int

The length of the vector to generate.

rng Random

The random number generator to use.

Returns

Vector<T>

A vector filled with Gaussian noise.

ScaleNoise(Tensor<T>, double)

Scales noise by a given factor.

public static Tensor<T> ScaleNoise(Tensor<T> noise, double scale)

Parameters

noise Tensor<T>

The noise tensor to scale.

scale double

The scaling factor.

Returns

Tensor<T>

Scaled noise tensor.

SlerpNoise(Tensor<T>, Tensor<T>, double)

Spherical linear interpolation between two noise tensors.

public static Tensor<T> SlerpNoise(Tensor<T> noise1, Tensor<T> noise2, double t)

Parameters

noise1 Tensor<T>

First noise tensor.

noise2 Tensor<T>

Second noise tensor.

t double

Interpolation factor (0 = noise1, 1 = noise2).

Returns

Tensor<T>

Spherically interpolated noise tensor.

Remarks

For Beginners: SLERP is like traveling on the surface of a sphere instead of cutting through it. For noise interpolation, this often gives smoother transitions between different random seeds.