Skip to content
DeepForgeby svx
All problems
EasyCalculusca-243

Tanh Derivative Backprop

Problem

The hyperbolic tangent has derivative 1 - tanh(x)^2.

Given an upstream gradient grad and a scalar pre-activation x, return grad * (1 - tanh(x)^2).

Hint

The tanh derivative is largest at the origin and decays toward zero at saturation.

Test cases

case 1
in: [1,0]
expected: 1
case 2
in: [2,1]
expected: 0.8399486832280523
case 3
in: [-1,0.5]
expected: -0.7864477329659274
case 4
in: [0,2]
expected: 0
Starter codePython
def tanh_backward(grad, x):
    # Your code here
    pass

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