近年来,神经网络方法在多个领域表现出优于传统算法的特点,其使用神经元的组合来提取数据中的深度特征,在非线性任务中表现出优越的性能。
神经元的非线性,主要是通过其激活函数f(x),将wx+b进行非线性化映射,其过程如下:
y = f(wx+b)
总结并绘制学术论文中可用的激活函数图,绘制的函数图包括: sigmoid函数、ReLU函数、tanh函数和pReLU函数(参数为0.5)。
1. sigmoid函数
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1. / (1 + np.exp(-x))
def plot_sigmoid():
x = np.arange(-10, 10, 0.1)
y = sigmoid(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.plot(x, y)
plt.xlim([-10.05, 10.05])
plt.ylim([-0.02, 1.02])
plt.tight_layout()
plot_sigmoid()
2. tanh函数
import numpy as np
import matplotlib.pyplot as plt
def tanh(x):
return (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
def plot_sigmoid():
x = np.arange(-10, 10, 0.1)
y = tanh(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.plot(x, y)
plt.xlim([-10.05, 10.05])
plt.ylim([-0.02, 1.02])
ax.set_yticks([-1.0, -0.5, 0.5, 1.0])
ax.set_xticks([-10, -5, 5, 10])
plt.tight_layout()
plot_sigmoid()
3. ReLU函数
import numpy as np
import matplotlib.pyplot as plt
def relu(x):
return np.where(x<0, 0, x)
def plot_sigmoid():
x = np.arange(-10, 10, 0.1)
y = relu(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.plot(x, y)
plt.xlim([-10.05, 10.05])
plt.ylim([-0.02, 1.02])
ax.set_yticks([2, 4, 6, 8, 10])
plt.tight_layout()
plot_sigmoid()
4.pReLU函数
import numpy as np
import matplotlib.pyplot as plt
def prelu(x):
return np.where(x<0, x * 0.5, x)
def plot_sigmoid():
x = np.arange(-10, 10, 0.1)
y = prelu(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['left'].set_position(('data', 0))
ax.spines['bottom'].set_position(('data', 0))
ax.plot(x, y)
plt.tight_layout()
plot_sigmoid()