经常使用的激活函数

1.阶跃函数函数

,值域{0,1}spa

1 def step_function(x):
2     return np.array(x>0,dtype=np.int)

2.sigmoid函数3d

,值域(0,1)code

1 def sigmoid(x):
2     return 1/(1+np.exp(-x))

3.relu函数blog

,值域[0,+∞)内存

1 def relu(x):
2     return np.maximum(0,x)

4.leaky relu函数io

,值域Rfunction

1 def leaky_relu(x):
2     value = [i if i  >= 0 else 0.01*i for i in x]
3     return np.array(value)

5.tanh函数class

,值域(-1,1)bfc

1 def tanh(x):
2     e_a = np.exp(-2*x)
3     y=(1-e_a)/(1+e_a)
4     return y

 

6.softmax函数

,值域[0,1]

 1 def softmax(x):
 2     e_a = np.exp(x)
 3     sum_e_a = np.sum(e_a)
 4     y=e_a/sum_e_a
 5     return y
 6 #或者,考虑可能出现内存溢出问题,减去一个常数
 7 def softmax(x):
 8     c=np.max(x)
 9     e_a = np.exp(x-c)
10     sum_e_a = np.sum(e_a)
11     y=e_a/sum_e_a
12     return y

7.画图程序

1 x=np.arange(-5,5,0.1)
2 y=softmax(x)#可替换为其它函数
3 plt.plot(x, y)
4 plt.ylim(-1.1, 1.1)
5 plt.show()
相关文章
相关标签/搜索