import torch


def kl_loss(mu, logvar):
    """
        KL-Divergence = 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
        :param mu: the mean from the latent vector
        :param logvar: log variance from the latent vector
    """
    KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
    return KLD