KL of a Diagonal Gaussian to N(0, I)

~12 mincode completion

Implement gaussian_kl_to_standard_normal(mu, var) returning a scalar.

Examples

Standard normal has KL 0

Input
gaussian_kl_to_standard_normal([0, 0], [1, 1])
Output
0

mu=(1,0), var=(1,4)

Input
gaussian_kl_to_standard_normal([1, 0], [1, 4])
Output
1.30685

A single unit with var=e

Input
gaussian_kl_to_standard_normal([0], [2.718281828459045])
Output
0.35914

Hints

Hint 1

is the natural log, which is what this formula wants.

Hint 2

Reach for variance rather than std.

Requirements

  • Return scalar

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

8 employers weight this skill

4 autonomy companies, 1 enterprise vendor, 1 defense company, 1 health and bio company, 1 AI product company. Top match scores 87.

Python
import numpy as np


def gaussian_kl_to_standard_normal(mu, var):
    """
    KL of a diagonal Gaussian q=N(mu, var) to N(0, I).

    Args:
        mu, var: arrays of the same shape, var > 0

    Returns:
        scalar
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.