sigmoid激活函数定义为:
该值域为:(0,1)
求该函数的导数推理过程:
主要是依据复合函数:
求解过程:
先对右边 求导:
再返回来求解:
又因为
所以最后导数形式为:
一般作用:
- 取值范围在0~1之间。
- 对于一个事件发生情况,50%是其结果的分水岭,选择函数应该在0.5中心对称。
可以对这两个函数画出图像:
代码实现:
import math
import matplotlib.pyplot as plt
import numpy as np
# 以类实现
class Sigmoid:
def __init__(self, w):
self.weight_input = w
self.output = 0.0
# 原函数
def obj(self):
self.output = 1.0 / (1.0 + np.exp(- self.weight_input))
return self.output
# 导数
def der(self):
if self.output == 0.0:
self.output = 1.0 / (1.0 + np.exp(- self.weight_input))
return self.output * (1 - self.output)
# 返回间隔均匀的100个样本,计算间隔为[start, stop]。
x = np.linspace(-10, 10, 100)
y_obj = Sigmoid(x).obj()
y_der = Sigmoid(x).der()
# 画图,原函数
plt.figure(figsize=(12, 12))
plt.subplot(2,2,1)
plt.plot(x, y_obj, color='red', label='primitive function')
plt.ylim((-0.2, 1.2))
plt.legend()
plt.xlabel(r'$f(x) = \frac{1}{1+e^{-x}}$', fontsize=18, color='red')
# 导数
plt.subplot(2,2,2)
plt.plot(x, y_der, color='blue', label='derived function')
plt.ylim((-0.1, 0.5))
plt.legend()
plt.xlabel('f(x)\' = f(x)*(1-f(x))', fontsize=18, color='blue')
plt.show()