介绍
本篇文章,介绍几个常用的神经网络激活函数:
- sigmoid 函数
- tanh 函数
- ReLU 函数
sigmoid 函数
sigmoid 函数表达式
函数表达式如下所示:
σ(z)=11+e−z
画出函数图像:
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1.0/(1.0+np.exp(-x))
x = np.linspace(-8,8,200)
y = sigmoid(x)
plt.plot(x,y)
plt.show()
sigmoid 函数导数
对 sigmoid 函数求导,可以得到如下表达式:
σ′(z)=−e−z(1−e−z)2=1(1−e−z)(1−11−e−z)=σ(z)(1−σ(z))
画出其导数图形,如下:
def pre_sigmoid(z):
return sigmoid(z)*(1-sigmoid(z))
y_sigmoid_pre = pre_sigmoid(x)
plt.plot(x, y_sigmoid_pre)
tanh 函数
tanh 函数表达式
函数表达式如下:
tanh(z)=1−e−2z1+e−2z=2σ(2z)−1
画出函数图像,如下所示:
def tanh(x):
return 2*sigmoid(2*x)-1
y1 = tanh(x)
plt.plot(x,y1)
tanh 函数导数
tanh′(z)=4σ′(2z)
def pre_tanh(z):
return 4*pre_sigmoid(2*z)
y_tanh_pre = pre_tanh(x)
plt.plot(x, y_tanh_pre)
ReLU 函数
ReLU 函数表达式
ReLU(z)=max(z,0)
函数图像如下所示:
def relu(z):
return np.maximum(0,z)
y2 = relu(x)
plt.plot(x,y2)
ReLU 函数的导数
注意到,ReLU 函数在 z=0 处不可导,我们可以指定 z=0 处的导数为 0 或者 1。
def pre_relu(z):
result = []
for i in z:
if i >= 0: result.append(1)
else: result.append(0)
return result
y_relu_pre = pre_relu(x)
plt.plot(x, y_relu_pre)
小结
首先,定义对任何函数求导的函数:
def derivative(f, z, eps=0.0001):
return (f(z+eps)-f(z-eps))/(2*eps)
然后,将上述三个激活函数画在一起:
x = np.linspace(-3,3,200)
plt.plot(x, sigmoid(x), color="red")
plt.plot(x, tanh(x), color="green")
plt.plot(x, relu(x), color="blue")
plt.legend(["sigmoid", "tanh", "relu"], loc="best")
plt.grid()
plt.legend()
用来标示图注plt.grid()
用来打开网格线
然后将三个激活函数的导数图像画在一起:
plt.plot(x, derivative(sigmoid, x), color="red")
plt.plot(x, derivative(tanh, x), color="green")
plt.plot(x, derivative(relu, x), color="blue")
plt.legend(["sigmoid", "tanh", "relu"], loc="best")
plt.grid()
我的个人博客
欢迎访问我的个人博客: www.wangs0622.com