Skip to content
DeepForgeby svx
All problems
MediumCalculusca-342

Forward KL Gradient in q

Problem

The forward KL divergence KL(p || q) = sum_i p_i log(p_i / q_i) has derivative with respect to q_i equal to -p_i / q_i.

Given the discrete distributions p and q, return the gradient vector with respect to q.

Hint

log(p/q) is linear in log q, so only the -log q term differentiates.

Test cases

case 1
in: [[0.5,0.5],[0.25,0.75]]
expected: [-2,-0.6666666666666666]
case 2
in: [[0.2,0.8],[0.5,0.5]]
expected: [-0.4,-1.6]
case 3
in: [[1,0],[0.4,0.6]]
expected: [-2.5,0]
Starter codePython
def forward_kl_gradient_q(p, q):
    # Your code here
    pass

3 test cases · run your code against them in the editor