(
self,
*,
betas,
model_mean_type,
model_var_type,
loss_type,
rescale_timesteps=False,
)
| 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. |
| 377 | self.posterior_log_variance_clipped = np.log( |
| 378 | np.append(self.posterior_variance[1], self.posterior_variance[1:])) |
| 379 | self.posterior_mean_coef1 = (betas * |
| 380 | np.sqrt(self.alphas_cumprod_prev) / |
| 381 | (1.0 - self.alphas_cumprod)) |
| 382 | self.posterior_mean_coef2 = ((1.0 - self.alphas_cumprod_prev) * |
| 383 | np.sqrt(alphas) / |
| 384 | (1.0 - self.alphas_cumprod)) |
| 385 | |
| 386 | def q_mean_variance(self, x_start, t): |
| 387 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected