credit.diffusion
================

.. py:module:: credit.diffusion


Classes
-------

.. autoapisummary::

   credit.diffusion.ModelPrediction
   credit.diffusion.GaussianDiffusion
   credit.diffusion.ModifiedGaussianDiffusion


Functions
---------

.. autoapisummary::

   credit.diffusion.extract
   credit.diffusion.standard_normal_noise
   credit.diffusion.log_uniform_noise
   credit.diffusion.linear_beta_schedule
   credit.diffusion.cosine_beta_schedule
   credit.diffusion.sigmoid_beta_schedule


Module Contents
---------------

.. py:class:: ModelPrediction

   Bases: :py:obj:`tuple`


   .. py:attribute:: pred_noise


   .. py:attribute:: pred_x_start


.. py:function:: extract(a, t, x_shape)

.. py:function:: 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.

   :param shape: Shape for the output tensor.
   :type shape: tuple, optional
   :param tensor: Reference tensor to match shape and device.
   :type tensor: torch.Tensor, optional
   :param device: Device for output. Optional if tensor is provided.
   :type device: torch.device or str, optional

   :returns: Noise sampled from N(0, 1).
   :rtype: torch.Tensor


.. py:function:: 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.

   :param shape: Shape of the output noise tensor. Required if `tensor` is not provided.
   :type shape: tuple, optional
   :param tensor: Reference tensor to match shape. Used if `shape` is not provided.
   :type tensor: torch.Tensor, optional
   :param sigma_min: Minimum standard deviation.
   :type sigma_min: float
   :param sigma_max: Maximum standard deviation.
   :type sigma_max: float
   :param device: Device for the output. If None, inferred from `tensor` or defaults to 'cpu'.
   :type device: torch.device or str, optional

   :returns: Noise tensor sampled from N(0, σ^2), with σ ~ log-uniform.
   :rtype: torch.Tensor


.. py:function:: linear_beta_schedule(timesteps)

   linear schedule, proposed in original ddpm paper


.. py:function:: cosine_beta_schedule(timesteps, s=0.008)

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


.. py:function:: 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


.. py:class:: 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: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: model


   .. py:attribute:: channels


   .. py:attribute:: self_condition


   .. py:attribute:: condition


   .. py:attribute:: image_size


   .. py:attribute:: objective
      :value: 'pred_v'



   .. py:attribute:: num_timesteps


   .. py:attribute:: sampling_timesteps
      :value: None



   .. py:attribute:: is_ddim_sampling


   .. py:attribute:: ddim_sampling_eta
      :value: 0.0



   .. py:attribute:: immiscible
      :value: False



   .. py:attribute:: offset_noise_strength
      :value: 0.0



   .. py:attribute:: normalize


   .. py:attribute:: unnormalize


   .. py:property:: device


   .. py:method:: predict_start_from_noise(x_t, t, noise)


   .. py:method:: predict_noise_from_start(x_t, t, x0)


   .. py:method:: predict_v(x_start, t, noise)


   .. py:method:: predict_start_from_v(x_t, t, v)


   .. py:method:: q_posterior(x_start, x_t, t)


   .. py:method:: model_predictions(x, t, x_self_cond=None, x_cond=None, clip_x_start=False, rederive_pred_noise=False)


   .. py:method:: p_mean_variance(x, t, x_self_cond=None, x_cond=None, clip_denoised=True)


   .. py:method:: p_sample(x, t: int, x_self_cond=None, x_cond=None)


   .. py:method:: p_sample_loop(shape, x_cond, return_all_timesteps=False)


   .. py:method:: ddim_sample(shape, x_cond, return_all_timesteps=False, disable_tqdm=True)


   .. py:method:: sample(x_cond, batch_size=16, return_all_timesteps=False)


   .. py:method:: interpolate(x1, x2, x_cond=None, t=None, lam=0.5)


   .. py:method:: noise_assignment(x_start, noise)


   .. py:method:: q_sample(x_start, t, noise=None)


   .. py:method:: p_losses(x_start, t, x_cond, noise=None, offset_noise_strength=None)


   .. py:method:: forward(img, x_cond, *args, **kwargs)


.. py:class:: ModifiedGaussianDiffusion(*args, **kwargs)

   Bases: :py:obj:`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 :meth:`to`, etc.

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

   :ivar training: Boolean represents whether this module is in training or
                   evaluation mode.
   :vartype training: bool


   .. py:attribute:: channels


   .. py:attribute:: history_len


   .. py:attribute:: criterion
      :value: None



   .. py:method:: load_loss(criterion)


   .. py:method:: forward(img, x_cond=None, *args, **kwargs)


   .. py:method:: p_losses(x_start, t, x_cond, noise=None, offset_noise_strength=None)


   .. py:method:: model_predictions(x, t, x_self_cond=None, x_cond=None, clip_x_start=False, rederive_pred_noise=False)


