class_docstr: ## Sampler class
class_docstr_tok: ["#","#","Sampler","class"]
docstr: #### Sample an image step-by-step using $       extcolor{lightgreen}{p_ heta}(x_{t-1}|x_t)$
  
  We sample an image step-by-step using $ extcolor{lightgreen}{p_ heta}(x_{t-1}|x_t)$ and at each step
  show the estimate
  $$x_0 pprox \hat{x}_0 = rac{1}{\sqrt{arlpha}}
   \Big( x_t - \sqrt{1 - arlpha_t}      extcolor{lightgreen}{\epsilon_  heta}(x_t, t) \Big)$$
docstr_tok: ["#","#","#","#","Sample","an","image","step-by-step","using","$","extcolor","{","lightgreen","}","{","p_","heta","}","(","x_","{","t-1","}","|x_t",")","$","We","sample","an","image","step-by-step","using","$","extcolor","{","lightgreen","}","{","p_","heta","}","(","x_","{","t-1","}","|x_t",")","$","and","at","each","step","show","the","estimate","$","$","x_0","\u0007pprox","\\hat","{","x","}","_0","=","rac","{","1","}","{","\\sqrt","{","\bar\u0007lpha","}","}","\\Big","(","x_t","-","\\sqrt","{","1","-","\bar\u0007lpha_t","}","extcolor","{","lightgreen","}","{","\\epsilon_","heta","}","(","x_t",",","t",")","\\Big",")","$","$"]
code: def sample_animation(
      self, n_frames: int = 1000, create_video: bool = True
  ):
      """
      #### Sample an image step-by-step using $\textcolor{lightgreen}{p_\theta}(x_{t-1}|x_t)$
  
      We sample an image step-by-step using $\textcolor{lightgreen}{p_\theta}(x_{t-1}|x_t)$ and at each step
      show the estimate
      $$x_0 \approx \hat{x}_0 = \frac{1}{\sqrt{\bar\alpha}}
       \Big( x_t - \sqrt{1 - \bar\alpha_t} \textcolor{lightgreen}{\epsilon_\theta}(x_t, t) \Big)$$
      """
  
      # $x_T \sim p(x_T) = \mathcal{N}(x_T; \mathbf{0}, \mathbf{I})$
      xt = torch.randn(
          [1, self.image_channels, self.image_size, self.image_size],
          device=self.device,
      )
  
      # Interval to log $\hat{x}_0$
      interval = self.n_steps // n_frames
      # Frames for video
      frames = []
      # Sample $T$ steps
      for t_inv in monit.iterate("Denoise", self.n_steps):
          # $t$
          t_ = self.n_steps - t_inv - 1
          # $t$ in a tensor
          t = xt.new_full((1,), t_, dtype=torch.long)
          # $\textcolor{lightgreen}{\epsilon_\theta}(x_t, t)$
          eps_theta = self.eps_model(xt, t)
          if t_ % interval == 0:
              # Get $\hat{x}_0$ and add to frames
              x0 = self.p_x0(xt, t, eps_theta)
              frames.append(x0[0])
              if not create_video:
                  self.show_image(x0[0], f"{t_}")
          # Sample from $\textcolor{lightgreen}{p_\theta}(x_{t-1}|x_t)$
          xt = self.p_sample(xt, t, eps_theta)
  
      # Make video
      if create_video:
          self.make_video(frames)
  
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 4 for calling the Sampler obj's underlying member method code with example usage: obj.sample_animation(n_frames, create_video) without return types