๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #68

Tanh Function

The tanh (hyperbolic tangent) function is sigmoid's zero-centered cousin โ€” it squashes inputs into (-1, 1) instead of (0, 1), fixing one of sigmoid's two drawbacks while still suffering from the other.

Formula

\[ \tanh(z) = \frac{e^z - e^{-z}}{e^z+e^{-z}} = 2\sigma(2z) - 1 \]

The second form makes the relationship to sigmoid explicit: tanh is just a rescaled, shifted sigmoid.

Derivative

\[ \tanh'(z) = 1-\tanh^2(z) \]
PropertyValue
Range\((-1, 1)\)
\(\tanh(0)\)0
Maximum derivative1, at \(z=0\)

Graph

Same S-shape as sigmoid, but centered at zero and stretched to range from -1 to 1.

Why Zero-Centering Matters

Because tanh's output can be negative, gradients flowing back through it aren't systematically biased in one direction the way sigmoid's always-positive output can bias them (see the note on sigmoid's second drawback). This typically makes optimization converge somewhat faster and more smoothly for hidden layers, which is why tanh was historically preferred over sigmoid specifically for hidden layers, back when saturating activations were the norm.

The Shared Flaw: Still Saturates

Tanh has the exact same vanishing gradient problem as sigmoid โ€” for large \(|z|\), the curve flattens and \(\tanh'(z)\to0\). Being zero-centered fixes one issue but not the fundamental saturation problem, which is why tanh, like sigmoid, has also largely been replaced by ReLU-family activations for hidden layers in deep networks.

Numerical Example

\[ \tanh(0) = 0, \qquad \tanh(1) \approx 0.762, \qquad \tanh(-1) \approx -0.762 \]

Code

import numpy as np
import torch

print(np.tanh([-1, 0, 1]))   # [-0.762  0.     0.762]

z = torch.tensor([-1.0, 0.0, 1.0])
print(torch.tanh(z))

Where It's Still Used Today

Tanh appears inside LSTM and GRU cells (alongside sigmoid gates) for producing candidate values within a bounded, zero-centered range โ€” covered fully in the LSTM & GRU category. It's rarely the default choice for feedforward or convolutional hidden layers today.

Common Mistakes

  • Assuming zero-centering alone solves vanishing gradients โ€” it addresses a separate, secondary issue; the saturation-driven gradient shrinkage is still present and often just as severe as sigmoid's.

Interview Relevance

Q: "Why might tanh converge faster than sigmoid for the same network, even though both can suffer vanishing gradients?" Tanh's zero-centered output (range -1 to 1) avoids systematically biasing gradient directions the way sigmoid's always-positive output (0 to 1) can, which tends to make gradient descent's path to a good solution more direct and efficient โ€” though both still saturate for large-magnitude inputs.

Practice Question

Using the identity \(\tanh(z) = 2\sigma(2z)-1\), verify that \(\tanh(0)=0\) follows directly from \(\sigma(0)=0.5\).

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Tanh Function โ€“ FAQs

Quick answers about learning Tanh Function in Deep Learning.

This free note from CodingNow 2.0 explains Tanh Function in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Tanh Function, is 100% free with no signup required.
With focused practice, most students grasp Tanh Function in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now