How to use numpy.tanh() and numpy.arctanh() functions (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

numpy is a fundamental package for scientific computing in Python. It provides a high-performance multidimensional array object and tools for working with these arrays. Among numpy’s numerous mathematical functions, tanh() and arctanh() play a pivotal role in various scientific and engineering computations, especially in machine learning, deep learning, and other computational fields.

This tutorial will guide you through using the numpy.tanh() and numpy.arctanh() functions with practical examples. We will start from the basics and gradually move to more advanced applications, highlighting how these functions can be effectively utilized in different scenarios.

Introduction to tanh() and arctanh()

The hyperbolic tangent function, or tanh(), is a mathematical function that maps any real number to the range (-1, 1). It is extensively used in neural networks as an activation function because it helps in mitigating the vanishing gradient problem. Conversely, the arctanh(), or inverse hyperbolic tangent function, unwraps the value back to its original magnitude within (-∞, ∞), excluding -1 and 1 where it’s undefined.

Example 1: Basic Usage of tanh()

import numpy as np

x = np.arange(-5, 6)
result = np.tanh(x)
print('tanh results:', result)

Output:

tanh results: [-0.9999092  -0.9993293  -0.99229794 -0.93110961 -0.76159416  0.          0.76159416  0.93110961  0.99229794  0.9993293   0.9999092 ]

This basic example illustrates how to apply the tanh() function across an array of values, showcasing its ability to compress real numbers to a specific range.

Example 2: Visualizing tanh() and arctanh() Functions

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 100)
tanh_x = np.tanh(x)
arctanh_x = np.arctanh(x/1.01)  # Scale down x to avoid undefined values

plt.figure(figsize=(12, 6))
plt.plot(x, tanh_x, label='tanh(x)')
plt.plot(x, arctanh_x, label='arctanh(x)', linestyle='--')
plt.legend()
plt.show()

The output is a plot showing the curves of the tanh() and arctanh() functions, demonstrating their inverse relationship over a defined range of values. This example uses matplotlib for visualization, highlighting how these functions behave graphically.

Example 3: Advanced Usage with Multidimensional Arrays

import numpy as np

# Creating a multidimensional array
matrix = np.array([[2, -1], [0.5, 3]])
# Applying tanh()
matrix_tanh = np.tanh(matrix)
# Applying arctanh()
matrix_arctanh = np.arctanh(matrix_tanh)

print('Original matrix:\n', matrix)
print('After tanh:\n', matrix_tanh)
print('After arctanh:\n', matrix_arctanh)

Output:

Original matrix:
  [[ 2.  -1. ]
  [ 0.5  3. ]]
 After tanh:
  [[ 0.96402758 -0.76159416]
  [ 0.46211716  0.99505475]]
 After arctanh:
  [[ 2.  -1. ]
  [ 0.5  3. ]]

This advanced example demonstrates using tanh() and arctanh() with multidimensional arrays, showing how these functions can be applied to matrices, providing the same results when the inverse function is applied, thus affirming their mathematical properties.

Example 4: Application in Machine Learning

import numpy as np

# Example neural network layer output
layer_output = np.random.randn(5)  # 5 neurons in a layer
# Activation using tanh
activated_output = np.tanh(layer_output)

# Assuming we need to backpropagate here
# Compute the gradient of tanh
gradient = 1 - np.square(activated_output)

print('Layer output:\n', layer_output)
print('Activated output using tanh:\n', activated_output)
print('Gradient for backpropagation:\n', gradient)

Output:

Layer output:
  [-0.15469125  1.20428692 -1.49419387 -1.06067161  0.49850562]
 Activated output using tanh:
  [-0.15327365  0.83336232 -0.8985     -0.78023985  0.46211716]
 Gradient for backpropagation:
  [0.97650151 0.3054159  0.19285105 0.3909877  0.78658676]

This example explores tanh() utilization in a neural network’s hidden layer, illustrating how the function not only activates neurons but also facilitates calculating gradients for backpropagation effectively due to its mathematical properties.

Conclusion

Through this tutorial, we’ve explored various aspects and applications of tanh() and arctanh() within numpy, highlighting their importance in scientific computing and machine learning. By understanding and implementing these functions effectively, developers can harness the power of numpy to build more efficient and scalable computational models.