MCPcopy Index your code
hub / github.com/CompVis/diff2flow / discretized_gaussian_log_likelihood

Function discretized_gaussian_log_likelihood

diff2flow/ddpm.py:533–559  ·  view source on GitHub ↗

Compute the log-likelihood of a Gaussian distribution discretizing to a given image. :param x: the target images. It is assumed that this was uint8 values, rescaled to the range [-1, 1]. :param means: the Gaussian mean Tensor. :param log_scales: the Gaussian log s

(x, *, means, log_scales)

Source from the content-addressed store, hash-verified

531
532
533def discretized_gaussian_log_likelihood(x, *, means, log_scales):
534 """
535 Compute the log-likelihood of a Gaussian distribution discretizing to a
536 given image.
537
538 :param x: the target images. It is assumed that this was uint8 values,
539 rescaled to the range [-1, 1].
540 :param means: the Gaussian mean Tensor.
541 :param log_scales: the Gaussian log stddev Tensor.
542 :return: a tensor like x of log probabilities (in nats).
543 """
544 assert x.shape == means.shape #== log_scales.shape # TODO: is this valid?
545 centered_x = x - means
546 inv_stdv = torch.exp(-log_scales)
547 plus_in = inv_stdv * (centered_x + 1.0 / 255.0)
548 cdf_plus = approx_standard_normal_cdf(plus_in)
549 min_in = inv_stdv * (centered_x - 1.0 / 255.0)
550 cdf_min = approx_standard_normal_cdf(min_in)
551 log_cdf_plus = torch.log(cdf_plus.clamp(min=1e-12))
552 log_one_minus_cdf_min = torch.log((1.0 - cdf_min).clamp(min=1e-12))
553 cdf_delta = cdf_plus - cdf_min
554 log_probs = torch.where(
555 x < -0.999, log_cdf_plus,
556 torch.where(x > 0.999, log_one_minus_cdf_min, torch.log(cdf_delta.clamp(min=1e-12))),
557 )
558 assert log_probs.shape == x.shape
559 return log_probs

Callers 1

_vb_terms_bpdMethod · 0.70

Calls 1

Tested by

no test coverage detected