python機(jī)器學(xué)習(xí)實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)示例解析
單神經(jīng)元引論
對(duì)于如花,大美,小明三個(gè)因素是如何影響小強(qiáng)這個(gè)因素的。
這里用到的是多元的線性回歸,比較基礎(chǔ)
from numpy import array,exp,dot,random
其中dot
是點(diǎn)乘
導(dǎo)入關(guān)系矩陣:
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的可能性,我們采用激活函數(shù)
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) weights+=dot(X.T,delta)
其中
delta=error*output*(1-output)
是求導(dǎo)的結(jié)果和誤差相乘,表示梯度
具體數(shù)學(xué)流程
所以具體流程如下,X
具體化了一下
error
即為每個(gè)帶權(quán)參數(shù)經(jīng)過激活函數(shù)映射后到y(tǒng)結(jié)果的量化距離
最終代碼:(PS:默認(rèn)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))))
參考
多神經(jīng)元
這個(gè)意思就是兩個(gè)美女XOR
單神經(jīng)元沒法解決,只能解決單一線性關(guān)系
代碼如下,可自行調(diào)整epoches
和lr
from numpy import array,exp,dot,random """ Created on vscode 10/22/2021 @author Squirre17 """ X=array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]) y=array([[0,1,1,0]]).T # T means "transposition" random.seed(1) epochs=100000 w0=2*random.random((3,4))-1 # input layer neure w1=2*random.random((4,1))-1 # hidden layer neure lr=1 def fp(input): l1=1/(1+exp(-dot(input,w0))) # 4×4 l2=1/(1+exp(-dot(l1,w1))) # 4×1 return l1,l2 def bp(l1,l2,y): l2_error=y-l2 l2_slope=l2*(1-l2) l1_delta=l2_error*l2_slope*lr # 4×1 l1_error=l1_delta.dot(w1.T) l1_slope=l1*(1-l1) l0_delta=l1_error*l1_slope*lr return l0_delta,l1_delta for it in range(epochs): l0=X l1,l2=fp(l0) l0_delta,l1_delta=bp(l1,l2,y) w1+=dot(l1.T,l1_delta) # 4×4 4×1 # adjust w1 according to loss w0+=dot(l0.T,l0_delta) print(fp([[1,0,0]])[1])
其中關(guān)于l1_error=l1_delta.dot(w1.T)
,就是第三層的誤差反向加權(quán)傳播給第二層
以上就是python機(jī)器學(xué)習(xí)實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)示例解析的詳細(xì)內(nèi)容,更多關(guān)于python機(jī)器學(xué)習(xí)實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。