Premium problem94. KL Divergence Between Distributions

Medium Locked

p and q hold valid probability distributions along the last dimension. Return sum(p * log(p/q)) along that dimension, so a (B, C) input yields (B,).

KL is not symmetric: it measures the cost of using q when the truth is p.

Input

p =
tensor([[0.5000, 0.5000],
        [0.9000, 0.1000]])
q =
tensor([[0.2500, 0.7500],
        [0.5000, 0.5000]])

Output

tensor([0.1438, 0.3681])

Premium problem

This one's part of Premium. Unlock the full PyTorch track plus every other premium problem on the site.

Implement solve(...)