TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
TorchedUp
LearnBetaProblemsSystem DesignSoonPremium
←

267. Backprop: Tanh (PyTorch)

Easy

Implement tanh as a torch.autograd.Function. Forward: y = tanh(x). Backward: dy/dx = 1 - y^2.

The rule: you may NOT call torch.tanh, x.tanh(), or nn.Tanh. Build the forward from .exp() and basic arithmetic.

Implement TanhFunction and the driver dispatches on 'forward' | 'grad_x' | 'gradcheck'. Save the output y for the backward.

Math

tanh(x)=ex+e−xex−e−x​,tanh′(x)=1−tanh2(x)

Related problems

  • Backprop: TanheasyNumPy

Asked at

NumPy

import numpy as np

 

def tanh_run(...):

    pass

🔒

Premium problem

Free accounts include problems #1–20. Upgrade to unlock the editor, hidden test cases, and reference solutions for every problem.

Upgrade to PremiumBack to problems

Already premium?