class_docstr: Utilities for training and sampling diffusion models.
  Original ported from this codebase:
  https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42
  :param betas: a 1-D numpy array of betas for each diffusion timestep,
                starting at T and going to 1.
class_docstr_tok: ["Utilities","for","training","and","sampling","diffusion","models",".","Original","ported","from","this","codebase",":","https",":","\/\/github.com\/hojonathanho\/diffusion\/blob\/1e0dceb3b3495bbe19116a5e1b3596cd0706c543\/diffusion_tf\/diffusion_utils_2.py","#","L42",":","param","betas",":","a","1-D","numpy","array","of","betas","for","each","diffusion","timestep",",","starting","at","T","and","going","to","1","."]
code: def __init__(
      self,
      *,
      betas,
      model_mean_type,
      model_var_type,
      loss_type,
  ):
      super(GaussianDiffusion, self).__init__()
      self.model_mean_type = model_mean_type
      self.model_var_type = model_var_type
      self.loss_type = loss_type
  
      # Use float64 for accuracy.
      betas = np.array(betas, dtype=np.float64)
      assert len(betas.shape) == 1, "betas must be 1-D"
      assert (betas > 0).all() and (betas <= 1).all()
  
      self.num_timesteps = int(betas.shape[0])
  
      alphas = 1.0 - betas
      alphas_cumprod = np.cumprod(alphas, axis=0)
      alphas_cumprod_prev = np.append(1.0, alphas_cumprod[:-1])
      alphas_cumprod_next = np.append(alphas_cumprod[1:], 0.0)
      assert alphas_cumprod_prev.shape == (self.num_timesteps,)
  
      # calculations for diffusion q(x_t | x_{t-1}) and others
      sqrt_alphas_cumprod = np.sqrt(alphas_cumprod)
      sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - alphas_cumprod)
      log_one_minus_alphas_cumprod = np.log(1.0 - alphas_cumprod)
      sqrt_recip_alphas_cumprod = np.sqrt(1.0 / alphas_cumprod)
      sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / alphas_cumprod - 1)
  
      # calculations for posterior q(x_{t-1} | x_t, x_0)
      posterior_variance = (
          betas * (1.0 - alphas_cumprod_prev) / (1.0 - alphas_cumprod)
      )
      # below: log calculation clipped because the posterior variance is 0 at the beginning of the diffusion chain
      posterior_log_variance_clipped = np.log(
          np.append(posterior_variance[1], posterior_variance[1:])
      )
      posterior_mean_coef1 = (
          betas * np.sqrt(alphas_cumprod_prev) / (1.0 - alphas_cumprod)
      )
      posterior_mean_coef2 = (
          (1.0 - alphas_cumprod_prev)
          * np.sqrt(alphas)
          / (1.0 - alphas_cumprod)
      )
  
      self.register_buffer(
          "betas", th.from_numpy(betas), persistent=False
      )
      self.register_buffer(
          "alphas_cumprod",
          th.from_numpy(alphas_cumprod),
          persistent=False,
      )
      self.register_buffer(
          "alphas_cumprod_prev",
          th.from_numpy(alphas_cumprod_prev),
          persistent=False,
      )
      self.register_buffer(
          "alphas_cumprod_next",
          th.from_numpy(alphas_cumprod_next),
          persistent=False,
      )
  
      self.register_buffer(
          "sqrt_alphas_cumprod",
          th.from_numpy(sqrt_alphas_cumprod),
          persistent=False,
      )
      self.register_buffer(
          "sqrt_one_minus_alphas_cumprod",
          th.from_numpy(sqrt_one_minus_alphas_cumprod),
          persistent=False,
      )
      self.register_buffer(
          "log_one_minus_alphas_cumprod",
          th.from_numpy(log_one_minus_alphas_cumprod),
          persistent=False,
      )
      self.register_buffer(
          "sqrt_recip_alphas_cumprod",
          th.from_numpy(sqrt_recip_alphas_cumprod),
          persistent=False,
      )
      self.register_buffer(
          "sqrt_recipm1_alphas_cumprod",
          th.from_numpy(sqrt_recipm1_alphas_cumprod),
          persistent=False,
      )
  
      self.register_buffer(
          "posterior_variance",
          th.from_numpy(posterior_variance),
          persistent=False,
      )
      self.register_buffer(
          "posterior_log_variance_clipped",
          th.from_numpy(posterior_log_variance_clipped),
          persistent=False,
      )
      self.register_buffer(
          "posterior_mean_coef1",
          th.from_numpy(posterior_mean_coef1),
          persistent=False,
      )
      self.register_buffer(
          "posterior_mean_coef2",
          th.from_numpy(posterior_mean_coef2),
          persistent=False,
      )
  
imports: import enum
  import math
  import numpy
  import torch
label_desc: Use image node_id 1 to create a new GaussianDiffusion object from inherited base classes: th.nn with example: obj = GaussianDiffusion()