Python| TensorFlow nn.tanh()
Tensorflow 是谷歌开发的开源机器学习库。它的应用之一是开发深度神经网络。
模块tensorflow.nn为许多基本的神经网络操作提供支持。
许多激活函数之一是双曲正切函数(也称为 tanh),其定义为 .
双曲正切函数在 (-1, 1) 范围内输出,因此将强烈的负输入映射到负值。与 sigmoid函数不同,只有接近零的值被映射到接近零的输出,这在一定程度上解决了“梯度消失”问题。双曲正切函数在每一点都是可微的,它的导数为 .由于表达式涉及 tanh函数,因此可以重用其值以使反向传播更快。
尽管与 sigmoid函数相比,网络“卡住”的可能性较低,但双曲正切函数仍然存在“梯度消失”的问题。整流线性单元 (ReLU) 可以用来克服这个问题。
函数tf.nn.tanh() [别名 tf.tanh] 为 Tensorflow 中的双曲正切函数提供支持。
Syntax: tf.nn.tanh(x, name=None) or tf.tanh(x, name=None)
Parameters:
x: A tensor of any of the following types: float16, float32, double, complex64, or complex128.
name (optional): The name for the operation.
Return : A tensor with the same type as that of x.
代码#1:
Python3
# Importing the Tensorflow library
import tensorflow as tf
# A constant vector of size 6
a = tf.constant([1.0, -0.5, 3.4, -2.1, 0.0, -6.5], dtype = tf.float32)
# Applying the tanh function and
# storing the result in 'b'
b = tf.nn.tanh(a, name ='tanh')
# Initiating a Tensorflow session
with tf.Session() as sess:
print('Input type:', a)
print('Input:', sess.run(a))
print('Return type:', b)
print('Output:', sess.run(b))
Python3
# Importing the Tensorflow library
import tensorflow as tf
# Importing the NumPy library
import numpy as np
# Importing the matplotlib.pyplot function
import matplotlib.pyplot as plt
# A vector of size 15 with values from -5 to 5
a = np.linspace(-5, 5, 15)
# Applying the tanh function and
# storing the result in 'b'
b = tf.nn.tanh(a, name ='tanh')
# Initiating a Tensorflow session
with tf.Session() as sess:
print('Input:', a)
print('Output:', sess.run(b))
plt.plot(a, sess.run(b), color = 'red', marker = "o")
plt.title("tensorflow.nn.tanh")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
输出:
Input type: Tensor("Const_2:0", shape=(6, ), dtype=float32)
Input: [ 1. -0.5 3.4000001 -2.0999999 0. -6.5 ]
Return type: Tensor("tanh_2:0", shape=(6, ), dtype=float32)
Output: [ 0.76159418 -0.46211717 0.9977749 -0.97045201 0. -0.99999547]
代码 #2:可视化
Python3
# Importing the Tensorflow library
import tensorflow as tf
# Importing the NumPy library
import numpy as np
# Importing the matplotlib.pyplot function
import matplotlib.pyplot as plt
# A vector of size 15 with values from -5 to 5
a = np.linspace(-5, 5, 15)
# Applying the tanh function and
# storing the result in 'b'
b = tf.nn.tanh(a, name ='tanh')
# Initiating a Tensorflow session
with tf.Session() as sess:
print('Input:', a)
print('Output:', sess.run(b))
plt.plot(a, sess.run(b), color = 'red', marker = "o")
plt.title("tensorflow.nn.tanh")
plt.xlabel("X")
plt.ylabel("Y")
plt.show()
输出:
Input: [-5. -4.28571429 -3.57142857 -2.85714286 -2.14285714 -1.42857143
-0.71428571 0. 0.71428571 1.42857143 2.14285714 2.85714286
3.57142857 4.28571429 5. ]
Output: [-0.9999092 -0.99962119 -0.99842027 -0.99342468 -0.97284617 -0.89137347
-0.61335726 0. 0.61335726 0.89137347 0.97284617 0.99342468
0.99842027 0.99962119 0.9999092 ]