class_docstr: ## Denoise Diffusion
class_docstr_tok: ["#","#","Denoise","Diffusion"]
docstr: #### Sample from $      extcolor{lightgreen}{p_ heta}(x_{t-1}|x_t)$
  
  egin{align}
          extcolor{lightgreen}{p_ heta}(x_{t-1} | x_t) &= \mathcal{N}ig(x_{t-1};
          extcolor{lightgreen}{\mu_       heta}(x_t, t), \sigma_t^2 \mathbf{I} ig) \
          extcolor{lightgreen}{\mu_       heta}(x_t, t)
    &= rac{1}{\sqrt{lpha_t}} \Big(x_t -
      rac{eta_t}{\sqrt{1-arlpha_t}}   extcolor{lightgreen}{\epsilon_  heta}(x_t, t) \Big)
  \end{align}
docstr_tok: ["#","#","#","#","Sample","from","$","extcolor","{","lightgreen","}","{","p_","heta","}","(","x_","{","t-1","}","|x_t",")","$","\begin","{","align","}","extcolor","{","lightgreen","}","{","p_","heta","}","(","x_","{","t-1","}","|","x_t",")","&","=","\\mathcal","{","N","}","\big","(","x_","{","t-1","}",";","extcolor","{","lightgreen","}","{","\\mu_","heta","}","(","x_t",",","t",")",",","\\sigma_t^2","\\mathbf","{","I","}","\big",")","\\","extcolor","{","lightgreen","}","{","\\mu_","heta","}","(","x_t",",","t",")","&","=","rac","{","1","}","{","\\sqrt","{","\u0007lpha_t","}","}","\\Big","(","x_t","-","rac","{","\beta_t","}","{","\\sqrt","{","1-\bar\u0007lpha_t","}","}","extcolor","{","lightgreen","}","{","\\epsilon_","heta","}","(","x_t",",","t",")","\\Big",")","\\end","{","align","}"]
code: def p_sample(self, xt: torch.Tensor, t: torch.Tensor):
      """
          #### Sample from $\textcolor{lightgreen}{p_\theta}(x_{t-1}|x_t)$
  
          \begin{align}
          \textcolor{lightgreen}{p_\theta}(x_{t-1} | x_t) &= \mathcal{N}\big(x_{t-1};
          \textcolor{lightgreen}{\mu_\theta}(x_t, t), \sigma_t^2 \mathbf{I} \big) \\
          \textcolor{lightgreen}{\mu_\theta}(x_t, t)
            &= \frac{1}{\sqrt{\alpha_t}} \Big(x_t -
              \frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\textcolor{lightgreen}{\epsilon_\theta}(x_t, t) \Big)
          \end{align}
          """
  
      # $\textcolor{lightgreen}{\epsilon_\theta}(x_t, t)$
      eps_theta = self.eps_model(xt, t)
      # [gather](utils.html) $\bar\alpha_t$
      alpha_bar = gather(self.alpha_bar, t)
      # $\alpha_t$
      alpha = gather(self.alpha, t)
      # $\frac{\beta}{\sqrt{1-\bar\alpha_t}}$
      eps_coef = (1 - alpha) / (1 - alpha_bar) ** 0.5
      # $$\frac{1}{\sqrt{\alpha_t}} \Big(x_t -
      #      \frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\textcolor{lightgreen}{\epsilon_\theta}(x_t, t) \Big)$$
      mean = 1 / (alpha**0.5) * (xt - eps_coef * eps_theta)
      # $\sigma^2$
      var = gather(self.sigma2, t)
  
      # $\epsilon \sim \mathcal{N}(\mathbf{0}, \mathbf{I})$
      eps = torch.randn(xt.shape, device=xt.device)
      # Sample
      return mean + (var**0.5) * eps
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/diffusion/ddpm/__init__.py
imports: from typing import Tuple, Optional
  import torch
  import torch.nn.functional
  import torch.utils.data
  from torch import nn
  from labml_nn.diffusion.ddpm.utils import gather
label_desc: Use image node_id 4 for calling the DenoiseDiffusion obj's underlying member method code with example usage: obj.p_sample(xt, t) and returns: unknown