Skip to content
DeepForgeby svx
All problems
EasyCalculusca-281

Sigmoid BCE Gradient with Logits

Problem

For binary cross-entropy with a sigmoid output, the gradient with respect to the logit z is sigmoid(z) - y, where y is the target in {0, 1}.

Given the logits and targets, return the per-example gradient list.

Hint

The BCE and sigmoid derivatives cancel into p - y.

Test cases

case 1
in: [[0,0],[0,1]]
expected: [0.5,-0.5]
case 2
in: [[1,-1],[1,0]]
expected: [-0.2689414213699951,0.2689414213699951]
case 3
in: [[2,2,2],[1,1,0]]
expected: [-0.11920292202211769,-0.11920292202211769,0.8807970779778823]
Starter codePython
def sigmoid_bce_gradient(logits, targets):
    # Your code here
    pass

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