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)過(guò)激活函數(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)元沒(méi)法解決,只能解決單一線性關(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)文章!
- python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)
- python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)
- python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)(三)
- python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)(二)
- python機(jī)器學(xué)習(xí)之神經(jīng)網(wǎng)絡(luò)(一)
- Python機(jī)器學(xué)習(xí)應(yīng)用之基于LightGBM的分類預(yù)測(cè)篇解讀
- Python機(jī)器學(xué)習(xí)應(yīng)用之基于天氣數(shù)據(jù)集的XGBoost分類篇解讀
- Python機(jī)器學(xué)習(xí)應(yīng)用之決策樹分類實(shí)例詳解
- Python機(jī)器學(xué)習(xí)應(yīng)用之基于BP神經(jīng)網(wǎng)絡(luò)的預(yù)測(cè)篇詳解
相關(guān)文章
基于Pytorch實(shí)現(xiàn)的聲音分類實(shí)例代碼
聲音分類是音頻深度學(xué)習(xí)中應(yīng)用最廣泛的方法之一,下面這篇文章主要給大家介紹了如何基于Pytorch實(shí)現(xiàn)聲音分類的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
全面剖析Python的Django框架中的項(xiàng)目部署技巧
這篇文章主要全面剖析了Python的Django框架的部署技巧,包括Fabric等自動(dòng)化部署和建立單元測(cè)試等方面,強(qiáng)烈推薦!需要的朋友可以參考下2015-04-04
django初始化數(shù)據(jù)庫(kù)的實(shí)例
今天小編就為大家分享一篇django初始化數(shù)據(jù)庫(kù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python的版本
這篇文章主要給大家介紹了關(guān)于ubuntu?20.04系統(tǒng)下如何切換gcc/g++/python版本的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ubuntu具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-12-12
解決Python字典查找報(bào)Keyerror的問(wèn)題
這篇文章主要介紹了解決Python字典查找報(bào)Keyerror的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python中numpy.empty()函數(shù)實(shí)例講解
在本篇文章里小編給大家分享的是一篇關(guān)于python中numpy.empty()函數(shù)實(shí)例講解內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02
python生成單位陣或?qū)顷嚨娜N方式小結(jié)
這篇文章主要介紹了python生成單位陣或?qū)顷嚨娜N方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Python設(shè)置Word全局樣式和文本樣式的示例代碼
這篇文章主要介紹了如何利用Python對(duì)Word內(nèi)容進(jìn)行各種樣式的設(shè)置,讓其能夠看起來(lái)更加的美觀。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-05-05

