PIXELBANKv9.1.0
Menu

Compute the KL divergence between two discrete probability distributions.

DKL(P∥Q)=∑iP(i)log⁡P(i)Q(i)D_{KL}(P \| Q) = \sum_i P(i) \log \frac{P(i)}{Q(i)}

This is used in RLHF to penalize the policy from deviating too far from the reference model.

Input:

  • Line 1: space-separated floats (distribution P)
  • Line 2: space-separated floats (distribution Q)

Output: KL divergence, rounded to 4 decimal places.

Example:

Input:
0.5 0.5
0.5 0.5
Output:
0.0
Reasoning:
  • The input distributions P and Q are read as space-separated floats: P = [0.5, 0.5] and Q = [0.5, 0.5].
  • We calculate the KL divergence using the formula: DKL(P∥Q)=∑iP(i)log⁡P(i)Q(i)D_{KL}(P \| Q) = \sum_i P(i) \log \frac{P(i)}{Q(i)}, which simplifies to 0.5log⁡0.50.5+0.5log⁡0.50.50.5 \log \frac{0.5}{0.5} + 0.5 \log \frac{0.5}{0.5}.
  • Since log⁡0.50.5=log⁡1=0\log \frac{0.5}{0.5} = \log 1 = 0, the expression reduces to 0.5⋅0+0.5⋅0=00.5 \cdot 0 + 0.5 \cdot 0 = 0.
  • The final output is the calculated KL divergence, rounded to 4 decimal places: 0.00.0.

Constraints:

  • P and Q are valid probability distributions (sum to 1, all positive)
  • Same length
  • Round to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
KL Divergence Penalty - Easy | PixelBank