Sigmoid激活函数-Python实现
在本教程中,我们将学习 sigmoid 激活函数。 sigmoid 函数总是返回 0 和 1 之间的输出。
学完本教程你就会知道:
-
什么是激活函数?
-
如何在python中实现sigmoid函数?
-
如何在python中绘制sigmoid函数?
-
我们在哪里使用sigmoid函数?
-
sigmoid激活函数会导致哪些问题?
-
sigmoid 激活的更好替代方案。
什么是激活函数?
激活函数是控制神经网络输出的数学函数。激活函数有助于确定神经元是否被激发。
一些流行的激活功能是:
-
二进制步
-
线性
-
乙状结肠
-
谭
-
ReLU
-
泄漏 ReLU
-
Softmax
激活负责将非线性添加到神经网络模型的输出中。如果没有激活函数,神经网络只是一个线性回归。
计算神经网络输出的数学方程为:

激活函数
在本教程中,我们将重点介绍 sigmoid 激活函数。 该函数来自数学中的 sigmoid 函数。
让我们从讨论函数的公式开始。
sigmoid激活函数的公式
在数学上,您可以将 sigmoid 激活函数表示为:

公式
你可以看到分母总是大于 1,因此输出总是在 0 和 1 之间。
Python实现Sigmoid激活函数
在本节中,我们将学习如何在 Python 中实现 sigmoid 激活函数。
我们可以将python中的函数定义为:
import numpy as np
def sig(x):
return 1/(1 + np.exp(-x))
让我们尝试在一些输入上运行该函数。
import numpy as np
def sig(x):
return 1/(1 + np.exp(-x))
x = 1.0
print('Applying Sigmoid Activation on (%.1f) gives %.1f' % (x, sig(x)))
x = -10.0
print('Applying Sigmoid Activation on (%.1f) gives %.1f' % (x, sig(x)))
x = 0.0
print('Applying Sigmoid Activation on (%.1f) gives %.1f' % (x, sig(x)))
x = 15.0
print('Applying Sigmoid Activation on (%.1f) gives %.1f' % (x, sig(x)))
x = -2.0
print('Applying Sigmoid Activation on (%.1f) gives %.1f' % (x, sig(x)))
输出 :
Applying Sigmoid Activation on (1.0) gives 0.7
Applying Sigmoid Activation on (-10.0) gives 0.0
Applying Sigmoid Activation on (0.0) gives 0.5
Applying Sigmoid Activation on (15.0) gives 1.0
Applying Sigmoid Activation on (-2.0) gives 0.1
使用 Python 绘制 Sigmoid 激活
为了绘制 sigmoid 激活,我们将使用Numpy 库:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-10, 10, 50)
p = sig(x)
plt.xlabel("x")
plt.ylabel("Sigmoid(x)")
plt.plot(x, p)
plt.show()
输出 :

乙状结肠
我们可以看到输出在 0 和 1 之间。
sigmoid 函数通常用于预测概率,因为概率总是在 0 和 1 之间。
sigmoid 函数的缺点之一是朝向末端区域Y 值对 X 值变化的响应非常少。
这会导致一个称为梯度消失问题的问题。
梯度消失会减慢学习过程,因此是不可取的。
让我们讨论一些克服这个问题的替代方案。
ReLu激活函数
解决这个梯度消失问题的更好的替代方法是ReLu 激活函数。
如果输入为负,ReLu 激活函数返回 0,否则按原样返回输入。
在数学上它表示为:

恢复
您可以按如下方式在 Python 中实现它:
def relu(x):
return max(0.0, x)
让我们看看它在某些输入上是如何工作的。
def relu(x):
return max(0.0, x)
x = 1.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -10.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 0.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = 15.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
x = -20.0
print('Applying Relu on (%.1f) gives %.1f' % (x, relu(x)))
输出:
Applying Relu on (1.0) gives 1.0
Applying Relu on (-10.0) gives 0.0
Applying Relu on (0.0) gives 0.0
Applying Relu on (15.0) gives 15.0
Applying Relu on (-20.0) gives 0.0
ReLu 的问题在于负输入的梯度为零。
这再次导致负输入的梯度消失(零梯度)问题。
为了解决这个问题,我们有另一种替代方法,称为 Leaky ReLu 激活函数。
Leaky ReLu 激活函数
泄漏的 ReLu 通过将 x 的极小线性分量赋予负输入来解决负值的零梯度问题。
数学上我们可以定义为:
f(x)= 0.01x, x<0
= x, x>=0
您可以使用以下方法在 Python 中实现它:
def leaky_relu(x):
if x>0 :
return x
else :
return 0.01*x
x = 1.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -10.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 0.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = 15.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
x = -20.0
print('Applying Leaky Relu on (%.1f) gives %.1f' % (x, leaky_relu(x)))
输出 :
Applying Leaky Relu on (1.0) gives 1.0
Applying Leaky Relu on (-10.0) gives -0.1
Applying Leaky Relu on (0.0) gives 0.0
Applying Leaky Relu on (15.0) gives 15.0
Applying Leaky Relu on (-20.0) gives -0.2
结论
本教程是关于 Sigmoid 激活函数的。我们学习了如何在 python 中实现和绘制函数。
更多推荐

所有评论(0)