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=True,
denoised_fn=None,
cond_fn=None,
model_kwargs=None,
)
| 394 | return out |
| 395 | |
| 396 | def p_sample( |
| 397 | self, |
| 398 | model, |
| 399 | x, |
| 400 | t, |
| 401 | clip_denoised=True, |
| 402 | denoised_fn=None, |
| 403 | cond_fn=None, |
| 404 | model_kwargs=None, |
| 405 | ): |
| 406 | """ |
| 407 | Sample x_{t-1} from the model at the given timestep. |
| 408 | |
| 409 | :param model: the model to sample from. |
| 410 | :param x: the current tensor at x_{t-1}. |
| 411 | :param t: the value of t, starting at 0 for the first diffusion step. |
| 412 | :param clip_denoised: if True, clip the x_start prediction to [-1, 1]. |
| 413 | :param denoised_fn: if not None, a function which applies to the |
| 414 | x_start prediction before it is used to sample. |
| 415 | :param cond_fn: if not None, this is a gradient function that acts |
| 416 | similarly to the model. |
| 417 | :param model_kwargs: if not None, a dict of extra keyword arguments to |
| 418 | pass to the model. This can be used for conditioning. |
| 419 | :return: a dict containing the following keys: |
| 420 | - 'sample': a random sample from the model. |
| 421 | - 'pred_xstart': a prediction of x_0. |
| 422 | """ |
| 423 | out = self.p_mean_variance( |
| 424 | model, |
| 425 | x, |
| 426 | t, |
| 427 | clip_denoised=clip_denoised, |
| 428 | denoised_fn=denoised_fn, |
| 429 | model_kwargs=model_kwargs, |
| 430 | ) |
| 431 | noise = th.randn_like(x) |
| 432 | nonzero_mask = ( |
| 433 | (t != 0).float().view(-1, *([1] * (len(x.shape) - 1))) |
| 434 | ) # no noise when t == 0 |
| 435 | if cond_fn is not None: |
| 436 | out["mean"] = self.condition_mean( |
| 437 | cond_fn, out, x, t, model_kwargs=model_kwargs |
| 438 | ) |
| 439 | sample = out["mean"] + nonzero_mask * th.exp(0.5 * out["log_variance"]) * noise |
| 440 | return {"sample": sample, "pred_xstart": out["pred_xstart"]} |
| 441 | |
| 442 | def p_sample_loop( |
| 443 | self, |
no test coverage detected