• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python机器学习实现神经网络示例解析

python 搞代码 4年前 (2022-01-09) 25次浏览 已收录 0个评论
文章目录[隐藏]

单神经元引论

对于如花,大美,小明三个因素是如何影响小强这个因素的。

这里用到的是多元的线性回归,比较基础

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))))

参考

多神经元


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python机器学习实现神经网络示例解析

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址