class_docstr: ## Denoise Diffusion
class_docstr_tok: ["#","#","Denoise","Diffusion"]
docstr: #### Sample from $q(x_t|x_0)$
  
  egin{align}
  q(x_t|x_0) &= \mathcal{N} \Big(x_t; \sqrt{arlpha_t} x_0, (1-arlpha_t) \mathbf{I} \Big)
  \end{align}
docstr_tok: ["#","#","#","#","Sample","from","$","q","(","x_t|x_0",")","$","\begin","{","align","}","q","(","x_t|x_0",")","&","=","\\mathcal","{","N","}","\\Big","(","x_t",";","\\sqrt","{","\bar\u0007lpha_t","}","x_0",",","(","1-\bar\u0007lpha_t",")","\\mathbf","{","I","}","\\Big",")","\\end","{","align","}"]
code: def q_sample(
      self,
      x0: torch.Tensor,
      t: torch.Tensor,
      eps: Optional[torch.Tensor] = None,
  ):
      """
      #### Sample from $q(x_t|x_0)$
  
      \begin{align}
      q(x_t|x_0) &= \mathcal{N} \Big(x_t; \sqrt{\bar\alpha_t} x_0, (1-\bar\alpha_t) \mathbf{I} \Big)
      \end{align}
      """
  
      # $\epsilon \sim \mathcal{N}(\mathbf{0}, \mathbf{I})$
      if eps is None:
          eps = torch.randn_like(x0)
  
      # get $q(x_t|x_0)$
      mean, var = self.q_xt_x0(x0, t)
      # Sample from $q(x_t|x_0)$
      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 3 for calling the DenoiseDiffusion obj's underlying member method code with example usage: obj.q_sample(x0, t, eps) and returns: unknown