Skip to content

← Loss Functions step 4 of 8

Medium Primitives

Implement KL Divergence

KL divergence compares two probability distributions in a specified direction. For D_KL(P || Q), weight each log-ratio by P, then sum over outcomes:

D_KL(P || Q) = sum(p[i] * log(p[i] / q[i]))

Use the natural logarithm, so the result is measured in nats. Reversing P and Q usually changes the result; KL divergence is not a symmetric distance.

Your task

Implement kl_divergence(p, q) from elementwise arithmetic, logarithms and a sum. Do not use a built-in KL-divergence helper.

The inputs are one-dimensional floating-point tensors of the same nonempty shape, dtype and device. Each contains strictly positive probabilities that sum to one. Inputs with zero or negative probabilities are outside this task.

This exercise uses a fixed additive epsilon of 1e-10. Apply it exactly as follows, including in the outer multiplier:

p_safe = p + 1e-10
q_safe = q + 1e-10
result = sum(p_safe * log(p_safe / q_safe))

Return a scalar tensor with shape (), preserving the input dtype and leaving p and q unchanged. Do not average across outcomes, swap the inputs, renormalize after adding epsilon, or clamp the final result to zero.

For p=[0.8, 0.2] and q=[0.5, 0.5], the result is approximately 0.192745. The first outcome contributes positively and the second contributes negatively; sum both terms. Equal input distributions give zero.

What does epsilon change?

The exact KL formula is the expression at the top. The required computation instead uses slightly shifted values whose sums exceed one. It approximates KL when epsilon is small relative to the probabilities, but can differ noticeably when a probability is close to epsilon. Use the specified shifted formula for grading, not a different clipping or smoothing rule.

Exact KL is nonnegative for normalized distributions. Floating-point sums can have small rounding errors near zero, so do not use a final clamp to conceal them. This exercise does not define the infinite-divergence case where Q is zero at an outcome with positive P.