神经网络中用到的sigmoid函数,即S函数在python里可以直接调用,但没想到耗费我很长时间。
最开始,我安装了SciPy v1.0.1;
但是尝试各种调用方式
from scipy import special
from scipy.special import expit
都显示找不到expit模块。百度和Google都未果。
后来卸载了SciPy重装了SciPy v1.0.0 就成功了……果然不要贪图新版本……
>>> from scipy.special import expit, logit
>>> expit([-np.inf, -1.5, 0, 1.5, np.inf])
array([ 0. , 0.18242552, 0.5 , 0.81757448, 1. ])
logit
is the inverse of expit
:
>>> logit(expit([-2.5, 0, 3.1, 5.0]))
array([-2.5, 0. , 3.1, 5. ])
Plot expit(x) for x in [-6, 6]:
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(-6, 6, 121)
>>> y = expit(x)
>>> plt.plot(x, y)
>>> plt.grid()
>>> plt.xlim(-6, 6)
>>> plt.xlabel('x')
>>> plt.title('expit(x)')
>>> plt.show()
