Sample x_{t-1} from the model at the given timestep. :param model: the model to sample from. :param x: the current tensor at x_{t-1}. :param t: the value of t, starting at 0 for the first diffusion step. :param clip_denoised: if True, clip the x_start predic
(
self,
model,
x,
t,
clip_denoised=False,
denoised_fn=None,
cond_fn=None,
model_kwargs=None,
)
| 453 | return out |
| 454 | |
| 455 | def p_sample( |
| 456 | self, |
| 457 | model, |
| 458 | x, |
| 459 | t, |
| 460 | clip_denoised=False, |
| 461 | denoised_fn=None, |
| 462 | cond_fn=None, |
| 463 | model_kwargs=None, |
| 464 | ): |
| 465 | """ |
| 466 | Sample x_{t-1} from the model at the given timestep. |
| 467 | |
| 468 | :param model: the model to sample from. |
| 469 | :param x: the current tensor at x_{t-1}. |
| 470 | :param t: the value of t, starting at 0 for the first diffusion step. |
| 471 | :param clip_denoised: if True, clip the x_start prediction to [-1, 1]. |
| 472 | :param denoised_fn: if not None, a function which applies to the |
| 473 | x_start prediction before it is used to sample. |
| 474 | :param cond_fn: if not None, this is a gradient function that acts |
| 475 | similarly to the model. |
| 476 | :param model_kwargs: if not None, a dict of extra keyword arguments to |
| 477 | pass to the model. This can be used for conditioning. |
| 478 | :return: a dict containing the following keys: |
| 479 | - 'sample': a random sample from the model. |
| 480 | - 'pred_xstart': a prediction of x_0. |
| 481 | """ |
| 482 | out = self.p_mean_variance( |
| 483 | model, |
| 484 | x, |
| 485 | t, |
| 486 | clip_denoised=clip_denoised, |
| 487 | denoised_fn=denoised_fn, |
| 488 | model_kwargs=model_kwargs, |
| 489 | ) |
| 490 | noise = th.randn_like(x) |
| 491 | nonzero_mask = ( |
| 492 | (t != 0).float().view(-1, *([1] * (len(x.shape) - 1))) |
| 493 | ) # no noise when t == 0 |
| 494 | if cond_fn is not None: |
| 495 | out["mean"] = self.condition_mean(cond_fn, out, x, t, model_kwargs=model_kwargs) |
| 496 | sample = out["mean"] + nonzero_mask * th.exp(0.5 * out["log_variance"]) * noise |
| 497 | return {"sample": sample, "pred_xstart": out["pred_xstart"]} |
| 498 | |
| 499 | def p_sample_loop( |
| 500 | self, |
no test coverage detected