Skip to content
DeepForgeby svx
All problems

Policy Gradient Loss

Problem

Compute the vanilla policy-gradient loss for a batch.

Return the mean of -log_probs[i] * returns[i]. An empty batch gives 0.0.

Hint

Minimizing this loss is gradient ascent on expected return.

Test cases

case 1
in: [[-0.5,-1],[2,3]]
expected: 2
case 2
in: [[0],[5]]
expected: 0
case 3
in: [[-2],[-1]]
expected: -2
case 4
in: [[],[]]
expected: 0
Starter codePython
def policy_gradient_loss(log_probs, returns):
    # Your code here
    pass

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