# Mysteriously defined KL-divergence term [duplicate]

I am trying to re-create a variational autoencoder. The loss function has two terms: reconstruction loss and KL-divergence term. KL-divergence is defined as $$D_{KL}(P||Q) = -\sum_{x\in X}{P(X)\log\bigg(\frac{Q(X)}{P(X)}}\bigg)$$

While in the the code here it says

kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma), axis=-1)

The formula for KL-divergence looks completely different from what is in this line of code. Can somebody familiar with variational autoencoders help?

It'll help to first rewrite the code in MathJax, viz. $$D_{KL}^\text{other}(P\Vert Q)=-\frac12\left(1+\ln\sigma^2-\bar{z}^2-\sigma^2\right).$$(Unfortunately, whoever wrote the code gave the false impression in the naming that z_log_sigma denotes $$\ln\sigma$$ rather than $$\ln\sigma^2$$.) With $$\bar{z}=\mu_1-\mu_2$$ this formula becomes$$D_{KL}^\text{other}(P\Vert Q)=-\frac12-\ln\sigma+\frac12(\mu_1-\mu_2)^2+\frac12\sigma^2.$$This is the $$\sigma_1=\sigma,\,\sigma_2=1$$ special case of the two-Gaussians KL divergence in @flawr's link.