我的第一个神经网络

啊啊啊O(≧口≦)O!!!我的第一个神经网络竟然是算出来的。

学习了简单的神经网络模型,今天出于兴趣,自己搭个神经网络的巨简单的模型,不不不,是算出来的。

这篇代码是根据我的上一篇博客点我吧根据推导公式写的,小白技能有限,大佬不要嘲笑啊。

欢迎大家访问我的博客Sky’s blog

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

#激活函数及导数
def sigmoid(x):
     return 1 / (1 + np.exp(-x))
def d_sigmoid(x):
    return x * (1 - x)
    
def tanh(x):
    return np.tanh(x)
def d_tanh(x):
    return 1.0 - np.tanh(x) * np.tanh(x)

##参数设置
alph = 0.5
esp = 0.01
step = 5000

#前向传播
def qianxiangchuanbo(init,weight,b):
    #输入层——>隐含层
    neth=[]#神经元输入加权和
    outh=[]#神经元输出
    #隐含层--->输出层
    neto=[]#输出神经元
    outo=[]#神经元输出
    neth.append(weight[0]*init[0]+weight[1]*init[1]+b[0])
    neth.append(weight[2]*init[0]+weight[3]*init[1]+b[0])
    outh.append(sigmoid(neth[0]))
    outh.append(sigmoid(neth[1]))
    neto.append(weight[4]*outh[0]+weight[5]*outh[1]+b[1])
    neto.append(weight[6]*outh[0]+weight[7]*outh[1]+b[1])
    outo.append(sigmoid(neto[0]))
    outo.append(sigmoid(neto[1]))
    return neth,outh,neto,outo
def fanxiangchuanbo(out,outo,outh):
    
    new_weight=[]
    q=[]
    #输如层——>隐藏层
    a1=(-(init[0]-outo[0])*outo[0]*(1-outo[0]))
    a2=(-(init[1]-outo[1])*outo[1]*(1-outo[1]))
    q.append(a1)
    q.append(a2)
    w1 = q[0]*weight[4]+q[1]*weight[6]*outh[0]*(1-outh[0])*init[0]
    w2 = q[0]*weight[4]+q[1]*weight[6]*outh[0]*(1-outh[0])*init[1]
    w3 = q[0]*weight[5]+q[1]*weight[7]*outh[1]*(1-outh[1])*init[0]
    w4 = q[0]*weight[5]+q[1]*weight[7]*outh[1]*(1-outh[1])*init[1]
    new_weight.append(w1)
    new_weight.append(w2)
    new_weight.append(w3)
    new_weight.append(w4)
    #输出层——>隐藏层
    w5 = -(out[0]-outo[0])*outo[0]*(1-outo[0])*outh[0]
    w6 = -(out[0]-outo[0])*outo[0]*(1-outo[0])*outh[1]
    w7 = -(out[1]-outo[1])*outo[1]*(1-outo[1])*outh[0]
    w8 = -(out[1]-outo[1])*outo[1]*(1-outo[1])*outh[1]
    new_weight.append(w5)
    new_weight.append(w6)
    new_weight.append(w7)
    new_weight.append(w8)
    return new_weight
#输入集
init = [0.05,0.10]
#真实输出集
out = [0.01,0.99]
#权重
weight = [0.15,0.20,0.25,0.30,0.40,0.45,0.50,0.55]
#偏置项
b=[0.35,0.60]

count=0
result = []
while True:
    count=count+1
    
    neth,outh,neto,outo = qianxiangchuanbo(init,weight,b)
    e=(abs(out[0]-outo[0])+abs(out[1]-outo[1]))
    result.append(e)
    gd_weight = fanxiangchuanbo(out,outo,outh)
    for i in range(len(weight)):
        weight[i]=weight[i]-alph*gd_weight[i]

   # if e<esp:
    #    break
    
    if count > step:
        break
for k in range(len(result)):
  plt.scatter(k,result[k])
plt.show()
print(weight)
print(out)
print(outo)

最后的运行结果如图
在这里插入图片描述
由于此代码只运行了5000次,可以看出与实际的差距还是很大的,如果感兴趣,你可以试试增大迭代次数,或者控制精度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲸可落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值