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
Related problems
Asked at
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.
Already premium?