Table of Contents

Class DDPMModel<T>

Namespace
AiDotNet.NeuralNetworks.Diffusion
Assembly
AiDotNet.dll

DDPM (Denoising Diffusion Probabilistic Models) implementation.

public class DDPMModel<T> : DiffusionModelBase<T>, IDiffusionModel<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>

Type Parameters

T

The numeric type used for calculations.

Inheritance
DDPMModel<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

DDPM is the foundational diffusion model architecture that introduced the modern approach to diffusion-based generation. It learns to reverse a gradual noising process to generate data from pure noise.

For Beginners: DDPM is like learning to restore a damaged photograph.

Training process:

  1. Take a clear photo
  2. Add a specific amount of noise (determined by timestep)
  3. Train a neural network to predict what noise was added
  4. Repeat with different photos and noise levels

Generation process:

  1. Start with pure random noise
  2. Ask the trained model "what noise is in this?"
  3. Remove the predicted noise to get a slightly clearer image
  4. Repeat 1000 times (or use DDIM for faster generation)
  5. End up with a new, never-before-seen image

This implementation provides a minimal but functional DDPM that serves as:

  • A reference implementation for understanding diffusion
  • A base for more sophisticated diffusion models
  • A demonstration of scheduler integration

Reference: "Denoising Diffusion Probabilistic Models" by Ho et al., 2020

Constructors

DDPMModel(INoiseScheduler<T>, Func<Tensor<T>, int, Tensor<T>>?)

Initializes a new instance of the DDPM model with a scheduler only.

public DDPMModel(INoiseScheduler<T> scheduler, Func<Tensor<T>, int, Tensor<T>>? noisePredictor = null)

Parameters

scheduler INoiseScheduler<T>

The step scheduler to use for the diffusion process.

noisePredictor Func<Tensor<T>, int, Tensor<T>>

Optional custom noise prediction function.

Remarks

Convenience constructor for creating a model with a specific scheduler.

DDPMModel(DiffusionModelOptions<T>?, INoiseScheduler<T>?, Func<Tensor<T>, int, Tensor<T>>?)

Initializes a new instance of the DDPM model.

public DDPMModel(DiffusionModelOptions<T>? options = null, INoiseScheduler<T>? scheduler = null, Func<Tensor<T>, int, Tensor<T>>? noisePredictor = null)

Parameters

options DiffusionModelOptions<T>

Configuration options for the diffusion model. If null, uses default options.

scheduler INoiseScheduler<T>

Optional custom scheduler. If null, creates one from options.

noisePredictor Func<Tensor<T>, int, Tensor<T>>

Optional custom noise prediction function. If null, uses a placeholder that returns zeros. In production, this would be a neural network.

Remarks

For Beginners: Create a DDPM model by providing:

  • Options to configure learning rate, timesteps, and noise schedule
  • Optionally, a custom scheduler (otherwise one is created from options)
  • Optionally, a noise predictor (the neural network that learns patterns)
Without a noise predictor, this is a "skeleton" model useful for:
  • Testing the scheduler integration
  • Understanding the diffusion pipeline
  • Serving as a template for custom implementations
// Create a minimal DDPM for testing with defaults
var model = new DDPMModel<double>();

// Or with custom options
var options = new DiffusionModelOptions<double>
{
    LearningRate = 0.0001,
    TrainTimesteps = 1000,
    DefaultInferenceSteps = 50
};
var model = new DDPMModel<double>(options);

// Generate samples (note: without a trained noise predictor, results are random)
var samples = model.Generate(new[] { 1, 3, 64, 64 }, numInferenceSteps: 50);

DDPMModel(int)

Initializes a new instance of the DDPM model with a seed for reproducibility.

public DDPMModel(int seed)

Parameters

seed int

The random seed for reproducible generation.

Remarks

Convenience constructor for creating a model with a specific seed.

Properties

ParameterCount

Gets the number of parameters in the model.

public override int ParameterCount { get; }

Property Value

int

Remarks

This property returns the total count of trainable parameters in the model. It's useful for understanding model complexity and memory requirements.

Methods

Clone()

Creates a deep copy of the model.

public override IDiffusionModel<T> Clone()

Returns

IDiffusionModel<T>

A new instance with the same parameters.

Create(SchedulerConfig<T>, Func<Tensor<T>, int, Tensor<T>>?)

Creates a DDPM model with a custom scheduler configuration.

public static DDPMModel<T> Create(SchedulerConfig<T> config, Func<Tensor<T>, int, Tensor<T>>? noisePredictor = null)

Parameters

config SchedulerConfig<T>

The scheduler configuration.

noisePredictor Func<Tensor<T>, int, Tensor<T>>

Optional custom noise prediction function.

Returns

DDPMModel<T>

A new DDPM model instance.

Remarks

Factory method for creating DDPM models with custom configurations.

// Create with Stable Diffusion-style config
var model = DDPMModel<double>.Create(
    SchedulerConfig<double>.CreateStableDiffusion(),
    myNeuralNetworkPredictor);

DeepCopy()

Creates a deep copy of this object.

public override IFullModel<T, Tensor<T>, Tensor<T>> DeepCopy()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

GetParameters()

Gets the parameters that can be optimized.

public override Vector<T> GetParameters()

Returns

Vector<T>

PredictNoise(Tensor<T>, int)

Predicts the noise in a noisy sample at a given timestep.

public override Tensor<T> PredictNoise(Tensor<T> noisySample, int timestep)

Parameters

noisySample Tensor<T>

The noisy input sample.

timestep int

The current timestep in the diffusion process.

Returns

Tensor<T>

The predicted noise tensor.

Remarks

This is the core prediction that the model learns. Given a noisy sample at timestep t, predict what noise was added to create it.

For Beginners: The model looks at a noisy image and guesses "what noise was added to make it look like this?" This prediction is then used to remove that noise and get a cleaner image.

SetParameters(Vector<T>)

Sets the model parameters.

public override void SetParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The parameter vector to set.

Remarks

This method allows direct modification of the model's internal parameters. This is useful for optimization algorithms that need to update parameters iteratively. If the length of parameters does not match ParameterCount, an ArgumentException should be thrown.

Exceptions

ArgumentException

Thrown when the length of parameters does not match ParameterCount.