MCPcopy Create free account
hub / github.com/MotrixLab/FineMoGen / GaussianDiffusion

Class GaussianDiffusion

mogen/models/utils/gaussian_diffusion.py:319–1160  ·  view source on GitHub ↗

Utilities for training and sampling diffusion models. Ported directly from here, and then adapted over time to further experimentation. https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42 :param betas: a 1-D

Source from the content-addressed store, hash-verified

317
318
319class GaussianDiffusion:
320 """
321 Utilities for training and sampling diffusion models.
322
323 Ported directly from here, and then adapted over time to further experimentation.
324 https://github.com/hojonathanho/diffusion/blob/1e0dceb3b3495bbe19116a5e1b3596cd0706c543/diffusion_tf/diffusion_utils_2.py#L42
325
326 :param betas: a 1-D numpy array of betas for each diffusion timestep,
327 starting at T and going to 1.
328 :param model_mean_type: a ModelMeanType determining what the model outputs.
329 :param model_var_type: a ModelVarType determining how variance is output.
330 :param loss_type: a LossType determining the loss function to use.
331 :param rescale_timesteps: if True, pass floating point timesteps into the
332 model so that they are always scaled like in the
333 original paper (0 to 1000).
334 """
335
336 def __init__(
337 self,
338 *,
339 betas,
340 model_mean_type,
341 model_var_type,
342 loss_type,
343 rescale_timesteps=False,
344 ):
345 self.model_mean_type = model_mean_type
346 self.model_var_type = model_var_type
347 self.loss_type = loss_type
348 self.rescale_timesteps = rescale_timesteps
349
350 # Use float64 for accuracy.
351 betas = np.array(betas, dtype=np.float64)
352 self.betas = betas
353 assert len(betas.shape) == 1, "betas must be 1-D"
354 assert (betas > 0).all() and (betas <= 1).all()
355
356 self.num_timesteps = int(betas.shape[0])
357
358 alphas = 1.0 - betas
359 self.alphas_cumprod = np.cumprod(alphas, axis=0)
360 self.alphas_cumprod_prev = np.append(1.0, self.alphas_cumprod[:-1])
361 self.alphas_cumprod_next = np.append(self.alphas_cumprod[1:], 0.0)
362 assert self.alphas_cumprod_prev.shape == (self.num_timesteps, )
363
364 # calculations for diffusion q(x_t | x_{t-1}) and others
365 self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod)
366 self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod)
367 self.log_one_minus_alphas_cumprod = np.log(1.0 - self.alphas_cumprod)
368 self.sqrt_recip_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod)
369 self.sqrt_recipm1_alphas_cumprod = np.sqrt(1.0 / self.alphas_cumprod -
370 1)
371
372 # calculations for posterior q(x_{t-1} | x_t, x_0)
373 self.posterior_variance = (betas * (1.0 - self.alphas_cumprod_prev) /
374 (1.0 - self.alphas_cumprod))
375 # log calculation clipped because the posterior variance is 0 at the
376 # beginning of the diffusion chain.

Callers 2

__init__Method · 0.85
build_diffusionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected