Python實(shí)現(xiàn)感知器模型、兩層神經(jīng)網(wǎng)絡(luò)
本文實(shí)例為大家分享了Python實(shí)現(xiàn)感知器模型、兩層神經(jīng)網(wǎng)絡(luò),供大家參考,具體內(nèi)容如下
python 3.4 因?yàn)槭褂昧?numpy
這里我們首先實(shí)現(xiàn)一個(gè)感知器模型來實(shí)現(xiàn)下面的對應(yīng)關(guān)系
[[0,0,1], ——- 0
[0,1,1], ——- 1
[1,0,1], ——- 0
[1,1,1]] ——- 1
從上面的數(shù)據(jù)可以看出:輸入是三通道,輸出是單通道。
這里的激活函數(shù)我們使用 sigmoid 函數(shù) f(x)=1/(1+exp(-x))
其導(dǎo)數(shù)推導(dǎo)如下所示:
L0=W*X;
z=f(L0);
error=y-z;
delta =error * f'(L0) * X;
W=W+delta;
python 代碼如下:
import numpy as np #sigmoid function def nonlin(x, deriv = False): if(deriv==True): return x*(1-x) return 1/(1+np.exp(-x)) # input dataset X=np.array([[0,0,1], [0,1,1], [1,0,1], [1,1,1]]) # output dataset y=np.array([[0,1,0,1]]).T #seed( ) 用于指定隨機(jī)數(shù)生成時(shí)所用算法開始的整數(shù)值, #如果使用相同的seed( )值,則每次生成的隨即數(shù)都相同, #如果不設(shè)置這個(gè)值,則系統(tǒng)根據(jù)時(shí)間來自己選擇這個(gè)值, #此時(shí)每次生成的隨機(jī)數(shù)因時(shí)間差異而不同。 np.random.seed(1) # init weight value with mean 0 syn0 = 2*np.random.random((3,1))-1 for iter in range(1000): # forward propagation L0=X L1=nonlin(np.dot(L0,syn0)) # error L1_error=y-L1 L1_delta = L1_error*nonlin(L1,True) # updata weight syn0+=np.dot(L0.T,L1_delta) print("Output After Training:") print(L1)
從輸出結(jié)果可以看出基本實(shí)現(xiàn)了對應(yīng)關(guān)系。
下面再用兩層網(wǎng)絡(luò)來實(shí)現(xiàn)上面的任務(wù),這里加了一個(gè)隱層,隱層包含4個(gè)神經(jīng)元。
import numpy as np def nonlin(x, deriv = False): if(deriv == True): return x*(1-x) else: return 1/(1+np.exp(-x)) #input dataset X = np.array([[0,0,1], [0,1,1], [1,0,1], [1,1,1]]) #output dataset y = np.array([[0,1,1,0]]).T #the first-hidden layer weight value syn0 = 2*np.random.random((3,4)) - 1 #the hidden-output layer weight value syn1 = 2*np.random.random((4,1)) - 1 for j in range(60000): l0 = X #the first layer,and the input layer l1 = nonlin(np.dot(l0,syn0)) #the second layer,and the hidden layer l2 = nonlin(np.dot(l1,syn1)) #the third layer,and the output layer l2_error = y-l2 #the hidden-output layer error if(j%10000) == 0: print "Error:"+str(np.mean(l2_error)) l2_delta = l2_error*nonlin(l2,deriv = True) l1_error = l2_delta.dot(syn1.T) #the first-hidden layer error l1_delta = l1_error*nonlin(l1,deriv = True) syn1 += l1.T.dot(l2_delta) syn0 += l0.T.dot(l1_delta) print "outout after Training:" print l2
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python基于socket實(shí)現(xiàn)TCP/IP客戶和服務(wù)器通信
本主要介紹了Python socket網(wǎng)絡(luò)編程TCP/IP服務(wù)器與客戶端通信的相關(guān)資料,這里對Scoket 進(jìn)行詳解并創(chuàng)建TCP服務(wù)器及TCP 客戶端實(shí)例代碼,需要的朋友可以參考下2021-06-06python OpenCV學(xué)習(xí)筆記實(shí)現(xiàn)二維直方圖
本篇文章主要介紹了python OpenCV學(xué)習(xí)筆記實(shí)現(xiàn)二維直方圖,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02python爬蟲把url鏈接編碼成gbk2312格式過程解析
這篇文章主要介紹了python爬蟲把url鏈接編碼成gbk2312格式過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參
這篇文章主要為大家介紹了python深度學(xué)習(xí)標(biāo)準(zhǔn)庫使用argparse調(diào)參實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Python3.8如何解決No module named 'numpy&apos
這篇文章主要介紹了Python3.8如何解決No module named 'numpy'報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Python數(shù)據(jù)擬合與廣義線性回歸算法學(xué)習(xí)
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)擬合與廣義線性回歸算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12