Skip to content
DeepForgeby svx
All problems
MediumOptimizationop-162

Importance Sampling Gradient

Problem

Estimate a target expectation by importance sampling.

Return (1 / n) * sum over i of (target_probs[i] / sample_probs[i]) * grads[i], skipping samples whose sampling probability is zero. This is the standard importance-weighted gradient estimator.

Hint

Samples drawn from a skewed proposal are reweighted by the probability ratio.

Test cases

case 1
in: [[1,2],[0.5,0.5],[0.5,0.5]]
expected: 1.5
case 2
in: [[1,2],[0.25,0.75],[0.5,0.5]]
expected: 1.75
case 3
in: [[4],[1],[0]]
expected: 0
Starter codePython
def importance_sampling_gradient(grads, target_probs, sample_probs):
    # Your code here
    pass

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