MCPcopy Create free account
hub / github.com/CompVis/diff2flow / calc_bpd_loop

Method calc_bpd_loop

diff2flow/ddpm.py:444–493  ·  view source on GitHub ↗

Compute the entire variational lower-bound, measured in bits-per-dim, as well as other related quantities. Returns: A dict containing the following keys: - total_bpd: the total variational lower-bound, per batch element. - prior_

(self, model, x_start, clip_denoised=False, model_kwargs=None)

Source from the content-addressed store, hash-verified

442 return {"output": output, "pred_xstart": out["pred_xstart"]}
443
444 def calc_bpd_loop(self, model, x_start, clip_denoised=False, model_kwargs=None):
445 """
446 Compute the entire variational lower-bound, measured in bits-per-dim,
447 as well as other related quantities.
448 Returns:
449 A dict containing the following keys:
450 - total_bpd: the total variational lower-bound, per batch element.
451 - prior_bpd: the prior term in the lower-bound.
452 - vb: an [N x T] tensor of terms in the lower-bound.
453 - xstart_mse: an [N x T] tensor of x_0 MSEs for each timestep.
454 - mse: an [N x T] tensor of epsilon MSEs for each timestep.
455 """
456 device = x_start.device
457 batch_size = x_start.shape[0]
458
459 vb = []
460 xstart_mse = []
461 mse = []
462 for t in list(range(self.num_timesteps))[::-1]:
463 t_batch = torch.tensor([t] * batch_size, device=device)
464 noise = torch.randn_like(x_start)
465 x_t = self.q_sample(x_start=x_start, t=t_batch, noise=noise)
466 # Calculate VLB term at the current timestep
467 with torch.no_grad():
468 out = self._vb_terms_bpd(
469 model,
470 x_start=x_start,
471 x_t=x_t,
472 t=t_batch,
473 clip_denoised=clip_denoised,
474 model_kwargs=model_kwargs,
475 )
476 vb.append(out["output"])
477 xstart_mse.append(mean_flat((out["pred_xstart"] - x_start) ** 2))
478 eps = self.predict_eps_from_xstart(x_t, t_batch, out["pred_xstart"])
479 mse.append(mean_flat((eps - noise) ** 2))
480
481 vb = torch.stack(vb, dim=1)
482 xstart_mse = torch.stack(xstart_mse, dim=1)
483 mse = torch.stack(mse, dim=1)
484
485 prior_bpd = self.prior_bpd(x_start)
486 total_bpd = vb.sum(dim=1) + prior_bpd
487 return {
488 "total_bpd": total_bpd,
489 "prior_bpd": prior_bpd,
490 "vb": vb,
491 "xstart_mse": xstart_mse,
492 "mse": mse,
493 }
494
495
496def extract_into_tensor(a, t, x_shape):

Callers

nothing calls this directly

Calls 5

q_sampleMethod · 0.95
_vb_terms_bpdMethod · 0.95
prior_bpdMethod · 0.95
mean_flatFunction · 0.70

Tested by

no test coverage detected