Skip to content
DeepForgeby svx
All problems
MediumCalculusca-341

Categorical Cross-Entropy Gradient

Problem

For categorical cross-entropy L(p) = -sum_i y_i log(p_i), the gradient with respect to p is -y_i / p_i elementwise.

Given the probability vector and the one-hot target vector, return the gradient list.

Hint

Only the target class contributes a nonzero gradient.

Test cases

case 1
in: [[0.5,0.5],[1,0]]
expected: [-2,0]
case 2
in: [[0.2,0.3,0.5],[0,0,1]]
expected: [0,0,-2]
case 3
in: [[0.25,0.75],[0,1]]
expected: [0,-1.3333333333333333]
Starter codePython
def categorical_cross_entropy_gradient(probs, targets):
    # Your code here
    pass

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