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
TThe numeric type used for calculations.
- Inheritance
-
DDPMModel<T>
- Implements
- 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:
- Take a clear photo
- Add a specific amount of noise (determined by timestep)
- Train a neural network to predict what noise was added
- Repeat with different photos and noise levels
Generation process:
- Start with pure random noise
- Ask the trained model "what noise is in this?"
- Remove the predicted noise to get a slightly clearer image
- Repeat 1000 times (or use DDIM for faster generation)
- 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
schedulerINoiseScheduler<T>The step scheduler to use for the diffusion process.
noisePredictorFunc<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
optionsDiffusionModelOptions<T>Configuration options for the diffusion model. If null, uses default options.
schedulerINoiseScheduler<T>Optional custom scheduler. If null, creates one from options.
noisePredictorFunc<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)
- 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
seedintThe 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
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
configSchedulerConfig<T>The scheduler configuration.
noisePredictorFunc<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
noisySampleTensor<T>The noisy input sample.
timestepintThe 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
parametersVector<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
parametersdoes not match ParameterCount.