class_docstr: ## Sampler class
class_docstr_tok: ["#","#","Sampler","class"]
docstr: * `diffusion` is the `DenoiseDiffusion` instance
  * `image_channels` is the number of channels in the image
  * `image_size` is the image size
  * `device` is the device of the model
docstr_tok: ["*","`","diffusion","`","is","the","`","DenoiseDiffusion","`","instance","*","`","image_channels","`","is","the","number","of","channels","in","the","image","*","`","image_size","`","is","the","image","size","*","`","device","`","is","the","device","of","the","model"]
code: def __init__(
      self,
      diffusion: DenoiseDiffusion,
      image_channels: int,
      image_size: int,
      device: torch.device,
  ):
      """
      * `diffusion` is the `DenoiseDiffusion` instance
      * `image_channels` is the number of channels in the image
      * `image_size` is the image size
      * `device` is the device of the model
      """
      self.device = device
      self.image_size = image_size
      self.image_channels = image_channels
      self.diffusion = diffusion
  
      # $T$
      self.n_steps = diffusion.n_steps
      # $\textcolor{lightgreen}{\epsilon_\theta}(x_t, t)$
      self.eps_model = diffusion.eps_model
      # $\beta_t$
      self.beta = diffusion.beta
      # $\alpha_t$
      self.alpha = diffusion.alpha
      # $\bar\alpha_t$
      self.alpha_bar = diffusion.alpha_bar
      # $\bar\alpha_{t-1}$
      alpha_bar_tm1 = torch.cat(
          [self.alpha_bar.new_ones((1,)), self.alpha_bar[:-1]]
      )
  
      # To calculate
      #
      # \begin{align}
      # q(x_{t-1}|x_t, x_0) &= \mathcal{N} \Big(x_{t-1}; \tilde\mu_t(x_t, x_0), \tilde\beta_t \mathbf{I} \Big) \\
      # \tilde\mu_t(x_t, x_0) &= \frac{\sqrt{\bar\alpha_{t-1}}\beta_t}{1 - \bar\alpha_t}x_0
      #                          + \frac{\sqrt{\alpha_t}(1 - \bar\alpha_{t-1})}{1-\bar\alpha_t}x_t \\
      # \tilde\beta_t &= \frac{1 - \bar\alpha_{t-1}}{1 - \bar\alpha_t} \beta_t
      # \end{align}
  
      # $$\tilde\beta_t = \frac{1 - \bar\alpha_{t-1}}{1 - \bar\alpha_t} \beta_t$$
      self.beta_tilde = (
          self.beta * (1 - alpha_bar_tm1) / (1 - self.alpha_bar)
      )
      # $$\frac{\sqrt{\bar\alpha_{t-1}}\beta_t}{1 - \bar\alpha_t}$$
      self.mu_tilde_coef1 = (
          self.beta * (alpha_bar_tm1**0.5) / (1 - self.alpha_bar)
      )
      # $$\frac{\sqrt{\alpha_t}(1 - \bar\alpha_{t-1}}{1-\bar\alpha_t}$$
      self.mu_tilde_coef2 = (
          (self.alpha**0.5)
          * (1 - alpha_bar_tm1)
          / (1 - self.alpha_bar)
      )
      # $\sigma^2 = \beta$
      self.sigma2 = self.beta
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/diffusion/ddpm/evaluate.py
imports: import numpy
  import torch
  from matplotlib import pyplot
  from torchvision.transforms.functional import to_pil_image, resize
  from labml import experiment, monit
  from labml_nn.diffusion.ddpm import DenoiseDiffusion, gather
  from labml_nn.diffusion.ddpm.experiment import Configs
label_desc: Use image node_id 1 to create a new Sampler object  with example: obj = Sampler(diffusion, image_channels, image_size, device)