def _kld(self, z, q_param, p_param=None): """ Computes the KL-divergence of some element z. KL(q||p) = -∫ q(z) log [ p(z) / q(z) ] = -E[log p(z) - log q(z)] :param z: sample from q-distribuion :param q_param: (mu, log_var) of the q-distribution :param p_param: (mu, log_var) of the p-distribution :return: KL(q||p) """ (mu, log_var) = q_param if self.flow is not None: f_z, log_det_z = self.flow(z) qz = log_gaussian(z, mu, log_var) - sum(log_det_z) z = f_z else: qz = log_gaussian(z, mu, log_var) if p_param is None: pz = log_standard_gaussian(z) else: (mu, log_var) = p_param pz = log_gaussian(z, mu, log_var) kl = qz - pz return kl
def _kld(self, z, q_param, p_param=None): """ Compute KL-divergence of some element z. Inputs: z : sample from the q distribution q_param : (mu, log_var) of the q distribution. p_param : (mu, log_var) of the p distribution. Returns: KL-divergence of q||p """ # Define q distribution (mu, log_var) = q_param qz = log_gaussian(z, mu, log_var) # Define p distribution if p_param is None: pz = log_standard_gaussian(z) else: (mu, log_var) = p_param pz = log_gaussian(z, mu, log_var) kl = qz - pz return kl
def log_gauss(x, mu, log_var): return -log_gaussian(x, mu, log_var)