How to use tanh activation in machine learning | tf.keras

Tanh activation function limits a real valued number to the range [-1, 1]. Its a non linear activation function with fixed output range.

  1. using tanh activation function on input x produces output with function ((exp(x) - exp(-x))/(exp(x) + exp(-x)))

tf.keras.activations module of tf.keras api provides built-in activation to use, refer following code to use tanh activation function on tensors.


import tensorflow as tf

input = tf.random.normal([2,3])
output = tf.keras.activations.tanh(input)

print("Input")
print(input)

print("Output after applying tanh activation")
print(output)

Example output:


Input
tf.Tensor(
[[ 1.5365825  1.2561369  1.50169  ]
 [-1.320414  -1.9326932 -1.5359558]], shape=(2, 3), dtype=float32)
Output after applying tanh activation
tf.Tensor(
[[ 0.9115443   0.84999555  0.9054532 ]
 [-0.86688685 -0.9589505  -0.9114382 ]], shape=(2, 3), dtype=float32)


Follow US on Twitter: