import numpy as np
from matplotlib import pyplot as plt
def function(x, y, desc):
plt.plot(x, y, color='r')
plt.text(-5, 0.9, r'$y={}(x)$'.format(desc), fontsize=13)
# 设置坐标轴格式
currentAxis = plt.gca()
currentAxis.xaxis.set_label_text('x', fontsize=15)
currentAxis.yaxis.set_label_text('y', fontsize=15)
# 画出X轴和Y轴
plt.axhline(0, color='black', linewidth=0.1) # 画出Y轴
plt.axvline(0, color='black', linewidth=0.1) # 画出X轴
plt.show()
if __name__ == "__main__":
# x是1维数组,数组大小是从-10. 到10.的实数,每隔0.1取一个点
x = np.arange(-10, 10, 0.1)
# function(x, 1 / (1 + np.exp(-x)), desc='sigmoid')
# function(x, (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x)), desc='tanh')
# function(x, np.clip(x, a_min=0., a_max=None), desc='relu')
function(x, np.where(x > 0, x, 0.1 * x), desc='LeakyRelu')
常见激活函数:
-
sigmoid、tanh在当输入较大时,斜率很小,不适合做激活函数,收敛太慢。 (这就要求输入做zero-centered预处理)
-
tanh因为更靠近0,比sigmoid要高效。
-
sigmoid适合用在二分类的输出层,因为输出在(0,1),就代表了概率
- 又叫线性整流单元,具备非线性,并且斜率很不错,可以加速收敛
- 具有一个非常好的特性:在输入值小于0时,它会输出0,那么神经元就不会被激活。相当于只有部分神经元工作,这就带来很好的稀疏性,对整体计算效率有提升。
- 为了解决relu在输入为负值时,没有斜率,可以引入a*X
- 实际应用中大部分输入时大于0的,故这个用的也不多
- relu只能在隐藏层使用
softmax
sigmoid为二分类,softmax则为多分类,输出每一类的概率,其和为1.
偏导数
这里顺便说一下偏导数计算,其中:
sigmoid: a * ( 1- a)
tanh:1-a^2
relu:
- 0:当z < 0
- 1:当z > 0
leaky_relu:
- 0.1:当z < 0
- 1:当z > 0
softmax:参考这篇文章