Skip to content
DeepForgeby svx
All problems
EasyDeep Learningdl-014

Tanh Backward

Problem

Backpropagate through tanh. For each pre-activation z[i], return dout[i] * (1 - tanh(z[i])^2).

The signature is tanh_grad(dout, z).

Hint

Compute tanh(z) once per element, then use 1 - tanh(z)^2.

Test cases

case 1
in: [[1],[0]]
expected: [1]
case 2
in: [[1],[1]]
expected: [0.4199743416]
case 3
in: [[2,3],[-1,0.5]]
expected: [0.8399486832,2.3593431989]
case 4
in: [[1,1],[1000,-1000]]
expected: [0,0]
Starter codePython
import math
def tanh_grad(dout, z):
    # Your code here
    pass

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