MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / FlaxDiagonalGaussianDistribution

Class FlaxDiagonalGaussianDistribution

src/diffusers/models/vae_flax.py:689–723  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

687
688
689class FlaxDiagonalGaussianDistribution(object):
690 def __init__(self, parameters, deterministic=False):
691 # Last axis to account for channels-last
692 self.mean, self.logvar = jnp.split(parameters, 2, axis=-1)
693 self.logvar = jnp.clip(self.logvar, -30.0, 20.0)
694 self.deterministic = deterministic
695 self.std = jnp.exp(0.5 * self.logvar)
696 self.var = jnp.exp(self.logvar)
697 if self.deterministic:
698 self.var = self.std = jnp.zeros_like(self.mean)
699
700 def sample(self, key):
701 return self.mean + self.std * jax.random.normal(key, self.mean.shape)
702
703 def kl(self, other=None):
704 if self.deterministic:
705 return jnp.array([0.0])
706
707 if other is None:
708 return 0.5 * jnp.sum(self.mean**2 + self.var - 1.0 - self.logvar, axis=[1, 2, 3])
709
710 return 0.5 * jnp.sum(
711 jnp.square(self.mean - other.mean) / other.var + self.var / other.var - 1.0 - self.logvar + other.logvar,
712 axis=[1, 2, 3],
713 )
714
715 def nll(self, sample, axis=[1, 2, 3]):
716 if self.deterministic:
717 return jnp.array([0.0])
718
719 logtwopi = jnp.log(2.0 * jnp.pi)
720 return 0.5 * jnp.sum(logtwopi + self.logvar + jnp.square(sample - self.mean) / self.var, axis=axis)
721
722 def mode(self):
723 return self.mean
724
725
726@flax_register_to_config

Callers 1

encodeMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected