credit.diffusion

Contents

credit.diffusion#

Classes#

ModelPrediction

GaussianDiffusion

Base class for all neural network modules.

ModifiedGaussianDiffusion

Base class for all neural network modules.

Functions#

extract(a, t, x_shape)

standard_normal_noise([shape, tensor, device])

Generate standard normal noise (mean=0, std=1), either using a shape or a reference tensor.

log_uniform_noise([shape, tensor, sigma_min, ...])

Sample noise from a log-uniform distribution over standard deviations.

linear_beta_schedule(timesteps)

linear schedule, proposed in original ddpm paper

cosine_beta_schedule(timesteps[, s])

cosine schedule

sigmoid_beta_schedule(timesteps[, start, end, tau, ...])

sigmoid schedule

Module Contents#

class credit.diffusion.ModelPrediction#

Bases: tuple

pred_noise#
pred_x_start#
credit.diffusion.extract(a, t, x_shape)#
credit.diffusion.standard_normal_noise(shape=None, tensor=None, device=None)#

Generate standard normal noise (mean=0, std=1), either using a shape or a reference tensor.

Parameters:
  • shape (tuple, optional) – Shape for the output tensor.

  • tensor (torch.Tensor, optional) – Reference tensor to match shape and device.

  • device (torch.device or str, optional) – Device for output. Optional if tensor is provided.

Returns:

Noise sampled from N(0, 1).

Return type:

torch.Tensor

credit.diffusion.log_uniform_noise(shape=None, tensor=None, sigma_min=0.02, sigma_max=200.0, device=None)#

Sample noise from a log-uniform distribution over standard deviations.

Parameters:
  • shape (tuple, optional) – Shape of the output noise tensor. Required if tensor is not provided.

  • tensor (torch.Tensor, optional) – Reference tensor to match shape. Used if shape is not provided.

  • sigma_min (float) – Minimum standard deviation.

  • sigma_max (float) – Maximum standard deviation.

  • device (torch.device or str, optional) – Device for the output. If None, inferred from tensor or defaults to ‘cpu’.

Returns:

Noise tensor sampled from N(0, σ^2), with σ ~ log-uniform.

Return type:

torch.Tensor

credit.diffusion.linear_beta_schedule(timesteps)#

linear schedule, proposed in original ddpm paper

credit.diffusion.cosine_beta_schedule(timesteps, s=0.008)#

cosine schedule as proposed in https://openreview.net/forum?id=-NEXDKk8gZ

credit.diffusion.sigmoid_beta_schedule(timesteps, start=-3, end=3, tau=1, clamp_min=1e-05)#

sigmoid schedule proposed in https://arxiv.org/abs/2212.11972 - Figure 8 better for images > 64x64, when used during training

class credit.diffusion.GaussianDiffusion(model, *, image_size, timesteps=1000, sampling_timesteps=None, objective='pred_v', beta_schedule='sigmoid', noise_type='normal', schedule_fn_kwargs=dict(), ddim_sampling_eta=0.0, auto_normalize=True, offset_noise_strength=0.0, min_snr_loss_weight=False, min_snr_gamma=5, immiscible=False)#

Bases: torch.nn.Module

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

model#
channels#
self_condition#
condition#
image_size#
objective = 'pred_v'#
num_timesteps#
sampling_timesteps = None#
is_ddim_sampling#
ddim_sampling_eta = 0.0#
immiscible = False#
offset_noise_strength = 0.0#
normalize#
unnormalize#
property device#
predict_start_from_noise(x_t, t, noise)#
predict_noise_from_start(x_t, t, x0)#
predict_v(x_start, t, noise)#
predict_start_from_v(x_t, t, v)#
q_posterior(x_start, x_t, t)#
model_predictions(x, t, x_self_cond=None, x_cond=None, clip_x_start=False, rederive_pred_noise=False)#
p_mean_variance(x, t, x_self_cond=None, x_cond=None, clip_denoised=True)#
p_sample(x, t: int, x_self_cond=None, x_cond=None)#
p_sample_loop(shape, x_cond, return_all_timesteps=False)#
ddim_sample(shape, x_cond, return_all_timesteps=False, disable_tqdm=True)#
sample(x_cond, batch_size=16, return_all_timesteps=False)#
interpolate(x1, x2, x_cond=None, t=None, lam=0.5)#
noise_assignment(x_start, noise)#
q_sample(x_start, t, noise=None)#
p_losses(x_start, t, x_cond, noise=None, offset_noise_strength=None)#
forward(img, x_cond, *args, **kwargs)#
class credit.diffusion.ModifiedGaussianDiffusion(*args, **kwargs)#

Bases: GaussianDiffusion

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

channels#
history_len#
criterion = None#
load_loss(criterion)#
forward(img, x_cond=None, *args, **kwargs)#
p_losses(x_start, t, x_cond, noise=None, offset_noise_strength=None)#
model_predictions(x, t, x_self_cond=None, x_cond=None, clip_x_start=False, rederive_pred_noise=False)#