单神经元引论
对于如花,大美,小明三个因素是如何影响小强这个因素的。
这里用到的是多元的线性回归,比较基础
from numpy import array,exp,dot,random
其中dot
是点乘
导入关系矩阵:
X= array ( [ [0,0,1],[1,1,1],[1,0,1],[0,1,1]]) y = array( [ [0,1,1,0]]).T ## T means "transposition"
为了满足0到1的可能性,我们采用激活函数
matlab作图
x=[-8:0.001:8] y=1./(1+exp(-x)) plot(x,y) grid on text(-6,0.8,['$\frac{1}{1+e^{-x}}$'],'interpreter','latex','fontsize',25)
然后
for it in range(10000): z=dot(X,weights) output=1/(1+exp(-z))##'dot' play role of "dot product" error=y-output delta=error*output*(1-output) weight<strong style="color:transparent">本文来源gao@daima#com搞(%代@#码网@</strong>s+=dot(X.T,delta)
其中
delta=error*output*(1-output)
是求导的结果和误差相乘,表示梯度
具体数学流程
所以具体流程如下,X
具体化了一下
error
即为每个带权参数经过激活函数映射后到y结果的量化距离
最终代码:(PS:默认lr取1,可修改)
from numpy import array,exp,dot,random """ Created on vscode 10/22/2021 @author Squirre17 """ X=array([[0,0,1],[1,1,1],[1,0,1],[0,1,1]]) y=array([[0,1,1,0]]).T ## T means "transposition" random.seed(1) epochs=10000 weights=2*random.random((3,1))-1## 3 row 1 line, range[-1,1) for it in range(epochs): output=1/(1+exp(-dot(X,weights)))##'dot' play role of "dot product" error=y-output slope=output*(1-output) delta=error*slope weights+=dot(X.T,delta) print(weights) print(1/(1+exp( -dot([[1,0,0]], weights))))