Evaluate scalar tanh and manually propagate upstream_gradient through the activation.
y=tanh(x) ∂x∂L=g(1−y2)Here x is the scalar input, y is its tanh output, L is the final loss, and g is upstream_gradient, the derivative of L with respect to y.
Return a tuple of two zero-dimensional PyTorch tensors: y first, then the gradient with respect to x. Both must preserve the inputs’ floating-point dtype and device.
Input: x = 0.5, upstream_gradient = 2.0
Output: (0.462117, 1.572895)
Explanation: The upstream gradient scales the local tanh derivative by two.
Input: x = 0.0, upstream_gradient = -3.0
Output: (0.0, -3.0)
Input: x = 8.0, upstream_gradient = 1.25
Output: (1.0, 0.000001)
torch.tanh preserves a tensor’s shape and dtype.
Tensor.square is useful for the local derivative.
Accepts: number
Accepts: number
Evaluate scalar tanh and manually propagate upstream_gradient through the activation.
y=tanh(x) ∂x∂L=g(1−y2)Here x is the scalar input, y is its tanh output, L is the final loss, and g is upstream_gradient, the derivative of L with respect to y.
Return a tuple of two zero-dimensional PyTorch tensors: y first, then the gradient with respect to x. Both must preserve the inputs’ floating-point dtype and device.
Input: x = 0.5, upstream_gradient = 2.0
Output: (0.462117, 1.572895)
Explanation: The upstream gradient scales the local tanh derivative by two.
Input: x = 0.0, upstream_gradient = -3.0
Output: (0.0, -3.0)
Input: x = 8.0, upstream_gradient = 1.25
Output: (1.0, 0.000001)
torch.tanh preserves a tensor’s shape and dtype.
Tensor.square is useful for the local derivative.
Accepts: number
Accepts: number