Skip to content
DeepForgeby svx
All problems
MediumCalculusca-343

Reverse KL Gradient in q

Problem

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

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

Hint

Apply the product rule to q log(q/p).

Test cases

case 1
in: [[0.5,0.5],[0.25,0.75]]
expected: [1.6931471805599454,0.5945348918918356]
case 2
in: [[0.2,0.8],[0.5,0.5]]
expected: [0.083709268125845,1.4700036292457357]
case 3
in: [[0.25,0.75],[0.25,0.75]]
expected: [1,1]
Starter codePython
def reverse_kl_gradient_q(q, p):
    # Your code here
    pass

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