class
Tanh
extends
ModuleTanh()Hyperbolic tangent activation function.
Applies element-wise:
Maps all real inputs to the open interval . Zero-centred, unlike sigmoid, which makes it the preferred gate/output activation in many recurrent architectures (LSTM, GRU).
Notes
- Input: — any shape.
- Output: — same shape as input, values in .
Like sigmoid, tanh saturates for large inputs. Its zero-centred output reduces the bias shift problem in successive layers, but the vanishing gradient issue still applies for very deep networks.
Examples
>>> import lucid
>>> import lucid.nn as nn
>>> m = nn.Tanh()
>>> x = lucid.tensor([-2.0, -1.0, 0.0, 1.0, 2.0])
>>> m(x)
tensor([-0.9640, -0.7616, 0. , 0.7616, 0.9640])
>>> # Hidden state output in a simple recurrent cell
>>> h = lucid.randn(32, 128)
>>> h_next = m(h)
>>> h_next.shape
(32, 128)Methods (1)
fn
forward
→Tensorforward(x: Tensor)Apply the activation function element-wise.
Parameters
inputTensorInput tensor of arbitrary shape.
Returns
TensorOutput tensor of the same shape as input.