Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Implement the tanh activation function for a layer output, which squashes real-valued inputs into the range ((-1, 1)). This is a common deep-learning basic used to add non-linearity to neural networks.
The activation is defined as:
Implement the function
Rules:
x.x.np.tanh, torch.tanh, jax.numpy.tanh).np.exp) for efficiency.Output:
| Argument | Type |
|---|---|
| x | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Input/output must be NumPy array.
No built-in tanh helpers (np.tanh/torch.tanh).
Avoid overflow; use stable exp-based formula.
Implement tanh element-wise using vectorized NumPy operations.
Avoid torch.tanh/np.tanh; use np.exp and the identity tanh(x) = (e^x - e^{-x})/(e^x + e^{-x}) (or an equivalent form).
For numerical stability, avoid computing both exp(x) and exp(-x) for large |x|. Use a stable rewrite with np.where: e.g. for x>=0: (1-exp(-2x))/(1+exp(-2x)), and for x<0 mirror it with exp(2x).