All problems
KL Early-Stop Check
Problem
Decide whether training should stop early because the policy moved too far, measured by KL(p || q).
Compute KL(p || q) = sum of p * log(p / q), skipping zero probabilities, and return True when it exceeds target_kl.
Hint
The check is strict: a KL exactly equal to target_kl does not trigger a stop.
Test cases
case 1
in: [[0.5,0.5],[0.5,0.5],0.01]
expected: false
case 2
in: [[0.9,0.1],[0.5,0.5],0.1]
expected: true
case 3
in: [[1,0],[0.5,0.5],0.5]
expected: true
case 4
in: [[0.5,0.5],[0.5,0.5],0]
expected: false
Starter codePython
import math
def kl_early_stop(p, q, target_kl):
# Your code here
pass4 test cases · run your code against them in the editor