Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-157

VAE KL Term (1D)

Problem

Compute the KL divergence from a diagonal Gaussian posterior to the standard normal prior: -0.5 * sum(1 + log_var - mu^2 - exp(log_var)). The signature takes log_var, not sigma.

The signature is vae_kl_term(mu, log_var). Return a scalar.

Hint

The KL is exactly zero when the posterior equals the standard normal.

Test cases

case 1
in: [[0],[0]]
expected: 0
case 2
in: [[1,0],[0,0]]
expected: 0.5
case 3
in: [[0],[1]]
expected: 0.3591409142
case 4
in: [[2],[0.5]]
expected: 2.0743606354
Starter codePython
import math
def vae_kl_term(mu, log_var):
    # Your code here
    pass

4 test cases · run your code against them in the editor