python機器學習實現(xiàn)神經(jīng)網(wǎng)絡示例解析
單神經(jīng)元引論

對于如花,大美,小明三個因素是如何影響小強這個因素的。

這里用到的是多元的線性回歸,比較基礎
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的可能性,我們采用激活函數(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)
是求導的結果和誤差相乘,表示梯度

具體數(shù)學流程
所以具體流程如下,X具體化了一下

error即為每個帶權參數(shù)經(jīng)過激活函數(shù)映射后到y(tǒng)結果的量化距離

最終代碼:(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))))
參考

多神經(jīng)元

這個意思就是兩個美女XOR
單神經(jīng)元沒法解決,只能解決單一線性關系


代碼如下,可自行調整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])
其中關于l1_error=l1_delta.dot(w1.T),就是第三層的誤差反向加權傳播給第二層

以上就是python機器學習實現(xiàn)神經(jīng)網(wǎng)絡示例解析的詳細內容,更多關于python機器學習實現(xiàn)神經(jīng)網(wǎng)絡的資料請關注腳本之家其它相關文章!
相關文章
ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python的版本
這篇文章主要給大家介紹了關于ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python版本的相關資料,文中通過代碼介紹的非常詳細,對大家學習或者使用ubuntu具有一定的參考借鑒價值,需要的朋友可以參考下2023-12-12
python中numpy.empty()函數(shù)實例講解
在本篇文章里小編給大家分享的是一篇關于python中numpy.empty()函數(shù)實例講解內容,對此有興趣的朋友們可以學習下。2021-02-02

