Probability Distribution
In Diffusion Models, the choice of a probability distribution plays a pivotal role in modeling the noise that guides transitions between time steps. While the Distribution
type is not directly used to parametrize the Model
class, it is used to create custom Noise
and Loss
modules. Modular Diffusion provides you with a set of distribution classes you can use to create your own modules.
Parameter shapes
Distribution parameters are represented as tensors with the same size as a batch. This essentially means that a
Distribution
object functions as a collection of distributions, where each individual element in a batch corresponds to a unique distribution. For instance, in the case of a standard DDPM, each pixel in a batch of images is associated with its ownmu
andsigma
values.
Normal distribution
Continuous probability distribution that is ubiquitously used in Diffusion Models. It has the following density function:
Sampling from a normal distribution is denoted and is equivalent to sampling from a standard normal distribution ( and ) and scaling the result by and shifting it by :
Parameters
mu: Tensor
-> Mean tensor .sigma: Tensor
-> Standard deviation tensor . Must have the same shape asmu
.
Parametrization
Please note that the
sigma
parameter does not correspond to the variance , but the standard deviation .
Example
import torch
from diffusion.distribution import Normal as N
distribution = N(torch.zeros(3), torch.full((3,), 2))
x, epsilon = distribution.sample()
# x = tensor([ 1.1053, 1.9027, -0.2554])
# epsilon = tensor([ 0.5527, 0.9514, -0.1277])
Categorical distribution
Discrete probability distribution that separately specifies the probability of each one of possible categories in a vector . Sampling from a normal distribution is denoted .
Parameters
p: Tensor
-> Probability tensor . All elements must be non-negative and sum to 1 in the last dimension.
Example
import torch
from diffusion.distribution import Categorical as Cat
distribution = Cat(torch.tensor([[.1, .3, .6], [0, 0, 1]]))
x, _ = distribution.sample()
# x = tensor([[0., 1., 0.], [0., 0., 1.]])
Noise tensor
The categorical distribution returns
None
in place of a noise tensor , as it would have no meaningful interpretation. Therefore, you must ignore the second return value when sampling.
If you spot any typo or technical imprecision, please submit an issue or pull request to the library's GitHub repository .