📜  Python – tensorflow.math.tanh()(1)

📅  最后修改于: 2023-12-03 15:34:06.988000             🧑  作者: Mango

Python – tensorflow.math.tanh()

The tensorflow.math.tanh() function is a mathematical function in the TensorFlow library that returns the hyperbolic tangent of its input tensor. The hyperbolic tangent is a mathematical function commonly used in neural network models.

Syntax

The syntax for using the tensorflow.math.tanh() function is as follows:

tf.math.tanh(x, name=None)
Parameters

The function takes two parameters:

  • x: The input tensor.

  • name: An optional name for the operation.

Return Value

The function returns a tensor with the same shape as the input tensor, containing the hyperbolic tangent of each element in the input tensor.

Example

Here is an example of how to use the tensorflow.math.tanh() function:

import tensorflow as tf

# Create a tensor with some values
x = tf.constant([[-1.0, 2.0], [3.0, -4.0]])

# Compute the hyperbolic tangent of the tensor
tanh_x = tf.math.tanh(x)

# Print the result
print(tanh_x)

Output:

tf.Tensor(
[[-0.7615942  0.9640276]
 [ 0.9950547 -0.9993292]], shape=(2, 2), dtype=float32)

As you can see, the tensorflow.math.tanh() function computes the hyperbolic tangent of each element in the input tensor, and returns a new tensor with the same shape. In this example, the input tensor x has shape (2, 2), and the output tensor tanh_x has the same shape.

Conclusion

The tensorflow.math.tanh() function is a useful mathematical function in the TensorFlow library that can be used to compute the hyperbolic tangent of a tensor. It is commonly used in neural network models, such as activation functions.