Skip to content
DeepForgeby svx
All problems
EasyDeep Learningdl-013

Sigmoid Backward

Problem

Backpropagate through a sigmoid. For each pre-activation z[i], compute s = sigmoid(z[i]) and return dout[i] * s * (1 - s).

The signature is sigmoid_grad(dout, z). Use a numerically stable sigmoid.

Hint

The sigmoid derivative is s * (1 - s); multiply by the upstream gradient.

Test cases

case 1
in: [[1],[0]]
expected: [0.25]
case 2
in: [[1,2],[0,0]]
expected: [0.25,0.5]
case 3
in: [[2],[1]]
expected: [0.3932238665]
case 4
in: [[1],[-1]]
expected: [0.1966119332]
case 5
in: [[1,1],[-1000,1000]]
expected: [0,0]
Starter codePython
import math
def sigmoid_grad(dout, z):
    # Your code here
    pass

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