資源簡介
手寫函數構建神經網絡結構和模擬神經網絡向前向后傳播和迭代過程
def sigmoid(z):
return 1.0/(1 np.exp(-z))
def bp(w,b,l):
# 1.向前傳播計算預測值和誤差
h1=sigmoid(w[1]*l[1] w[2]*l[2] b[1])
h2=sigmoid(w[3]*l[1] w[4]*l[2] b[1])
h3=sigmoid(w[5]*l[1] w[6]*l[2] b[1])
o1=sigmoid(w[7]*h1 w[9]*h2 w[11]*h3 b[2])
o2=sigmoid(w[8]*h1 w[10]*h2 w[12]*h3 b[2])
e=np.square(o1-0.01)/2 np.square(o2-0.99)/2
#2.向后傳播誤差,跟新權值、偏置
#t1=-(target-outo1)*outo1*(1-outo1)
t1=-(0.01-o1)*o1*(1-o1)
#t2=-(target-outo2)*outo2*(1-outo2)
t2=-(0.99-o2)*o2*(1-o2)
w[7]=w[7]-0.25*(t1*h1)
w[9]=w[9]-0.25*(t1*h2)
w[11]=w[11]-0.25*(t1*h3)
w[8]=w[8]-0.25*(t2*h1)
w[10]=w[10]-0.25*(t2*h2)
w[12]=w[12]-0.25*(t2*h3)
w[1]=w[1]-0.25*(t1*w[7] t2*w[8])*h1*(1-h1)*l[1]
w[2]=w[2]-0.25*(t1*w[7] t2*w[8])*h1*(1-h1)*l[2]
w[3]=w[3]-0.25*(t1*w[9] t2*w[10])*h2*(1-h2)*l[1]
w[4]=w[4]-0.25*(t1*w[9] t2*w[10])*h2*(1-h2)*l[2]
w[5]=w[5]-0.25*(t1*w[11] t2*w[12])*h3*(1-h3)*l[1]
w[6]=w[6]-0.25*(t1*w[11] t2*w[12])*h3*(1-h3)*l[2]
return o1,o2,w,e
def sigmoid(z):
return 1.0/(1 np.exp(-z))
def bp(w,b,l):
# 1.向前傳播計算預測值和誤差
h1=sigmoid(w[1]*l[1] w[2]*l[2] b[1])
h2=sigmoid(w[3]*l[1] w[4]*l[2] b[1])
h3=sigmoid(w[5]*l[1] w[6]*l[2] b[1])
o1=sigmoid(w[7]*h1 w[9]*h2 w[11]*h3 b[2])
o2=sigmoid(w[8]*h1 w[10]*h2 w[12]*h3 b[2])
e=np.square(o1-0.01)/2 np.square(o2-0.99)/2
#2.向后傳播誤差,跟新權值、偏置
#t1=-(target-outo1)*outo1*(1-outo1)
t1=-(0.01-o1)*o1*(1-o1)
#t2=-(target-outo2)*outo2*(1-outo2)
t2=-(0.99-o2)*o2*(1-o2)
w[7]=w[7]-0.25*(t1*h1)
w[9]=w[9]-0.25*(t1*h2)
w[11]=w[11]-0.25*(t1*h3)
w[8]=w[8]-0.25*(t2*h1)
w[10]=w[10]-0.25*(t2*h2)
w[12]=w[12]-0.25*(t2*h3)
w[1]=w[1]-0.25*(t1*w[7] t2*w[8])*h1*(1-h1)*l[1]
w[2]=w[2]-0.25*(t1*w[7] t2*w[8])*h1*(1-h1)*l[2]
w[3]=w[3]-0.25*(t1*w[9] t2*w[10])*h2*(1-h2)*l[1]
w[4]=w[4]-0.25*(t1*w[9] t2*w[10])*h2*(1-h2)*l[2]
w[5]=w[5]-0.25*(t1*w[11] t2*w[12])*h3*(1-h3)*l[1]
w[6]=w[6]-0.25*(t1*w[11] t2*w[12])*h3*(1-h3)*l[2]
return o1,o2,w,e
代碼片段和文件信息
#!/usr/bin/env?python
#?coding:?utf-8
#?In[1]:
import?numpy?as?np
w=[00.10.150.20.250.30.350.40.450.50.550.60.65]
b=[00.350.65]
l=[0510]
def?sigmoid(z):
????return?1.0/(1+np.exp(-z))
def?bp(wbl):
????#?1.向前傳播計算預測值和誤差
????h1=sigmoid(w[1]*l[1]+w[2]*l[2]+b[1])
????h2=sigmoid(w[3]*l[1]+w[4]*l[2]+b[1])
????h3=sigmoid(w[5]*l[1]+w[6]*l[2]+b[1])
????
????
????o1=sigmoid(w[7]*h1+w[9]*h2+w[11]*h3+b[2])
????o2=sigmoid(w[8]*h1+w[10]*h2+w[12]*h3+b[2])
????
????e=np.square(o1-0.01)/2+np.square(o2-0.99)/2
????
????
????#2.向后傳播誤差,跟新權值、偏置
????#t1=-(target-outo1)*outo1*(1-outo1)
????t1=-(0.01-o1)*o1*(1-o1)
????#t2=-(target-outo2)*outo2*(1-outo2)
??
評論
共有 條評論