Loading problem...
The hyperbolic tangent function (commonly written as tanh) is one of the most fundamental activation functions used in neural networks and deep learning architectures. It serves as a smooth, differentiable non-linearity that maps any real-valued input to an output in the range (-1, 1).
The mathematical definition of the hyperbolic tangent function is:
$$\tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$$
This can also be expressed in terms of the sigmoid function:
$$\tanh(x) = 2 \cdot \sigma(2x) - 1$$
where σ(x) is the sigmoid function.
Key Properties:
Your Task: Write a Python function that computes the hyperbolic tangent activation value for a given real-valued input. Your implementation should:
x = 1.00.7616For x = 1.0, we compute the exponentials:
• e¹ = 2.7183 • e⁻¹ = 0.3679
Applying the tanh formula:
tanh(1) = (e¹ - e⁻¹) / (e¹ + e⁻¹) = (2.7183 - 0.3679) / (2.7183 + 0.3679) = 2.3504 / 3.0862 = 0.7616
The function maps the input 1.0 to approximately 0.7616, demonstrating the compression of values toward the range boundaries.
x = 0.00.0For x = 0.0, both exponentials equal 1:
• e⁰ = 1.0 • e⁻⁰ = 1.0
Applying the tanh formula:
tanh(0) = (e⁰ - e⁻⁰) / (e⁰ + e⁻⁰) = (1.0 - 1.0) / (1.0 + 1.0) = 0 / 2 = 0.0
This demonstrates that the hyperbolic tangent function passes through the origin, a key property that makes it zero-centered.
x = -1.0-0.7616For x = -1.0, we compute:
• e⁻¹ = 0.3679 • e¹ = 2.7183
Applying the tanh formula:
tanh(-1) = (e⁻¹ - e¹) / (e⁻¹ + e¹) = (0.3679 - 2.7183) / (0.3679 + 2.7183) = -2.3504 / 3.0862 = -0.7616
Notice that tanh(-1) = -tanh(1), demonstrating the odd function property. This symmetry is valuable in neural networks as it treats negative and positive inputs symmetrically.
Constraints