📜  Python| TensorFlow nn.sigmoid()

📅  最后修改于: 2022-05-13 01:55:14.544000             🧑  作者: Mango

Python| TensorFlow nn.sigmoid()

Tensorflow 是谷歌开发的开源机器学习库。它的应用之一是开发深度神经网络。
模块tensorflow.nn为许多基本的神经网络操作提供支持。
许多激活函数之一是 sigmoid函数,其定义为f(x) = 1 / (1 + e^{-x})  .
Sigmoid函数在 (0, 1) 范围内输出,它非常适合我们需要找到数据属于特定类别的概率的二元分类问题。 sigmoid函数在每一点都是可微的,它的导数是f'(x) = f(x) * (1 - f(x))  .由于表达式涉及 sigmoid函数,因此可以重用其值以使反向传播更快。
Sigmoid函数存在“梯度消失”的问题,因为它在两端变平,导致在反向传播期间权重的变化非常小。这会使神经网络拒绝学习并陷入困境。由于这个原因,sigmoid函数的使用正在被其他非线性函数所取代,例如 Rectified Linear Unit (ReLU)。
函数tf.nn.sigmoid() [别名 tf.sigmoid] 为 Tensorflow 中的 sigmoid函数提供支持。

代码#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 sigmoid function and
# storing the result in 'b'
b = tf.nn.sigmoid(a, name ='sigmoid')
 
# 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 sigmoid function and
# storing the result in 'b'
b = tf.nn.sigmoid(a, name ='sigmoid')
 
# 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.sigmoid")
    plt.xlabel("X")
    plt.ylabel("Y")
 
    plt.show()


输出:

Input type: Tensor("Const_1:0", shape=(6, ), dtype=float32)
Input: [ 1.        -0.5        3.4000001 -2.0999999  0.        -6.5      ]
Return type: Tensor("sigmoid:0", shape=(6, ), dtype=float32)
Output: [ 0.7310586   0.37754068  0.96770459  0.10909683  0.5         0.00150118]

代码 #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 sigmoid function and
# storing the result in 'b'
b = tf.nn.sigmoid(a, name ='sigmoid')
 
# 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.sigmoid")
    plt.xlabel("X")
    plt.ylabel("Y")
 
    plt.show()

输出:

Input: 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.00669285  0.01357692  0.02734679  0.05431327  0.10500059  0.19332137
  0.32865255  0.5         0.67134745  0.80667863  0.89499941  0.94568673
  0.97265321  0.98642308  0.99330715]