Python編程實(shí)現(xiàn)的簡(jiǎn)單神經(jīng)網(wǎng)絡(luò)算法示例
本文實(shí)例講述了Python編程實(shí)現(xiàn)的簡(jiǎn)單神經(jīng)網(wǎng)絡(luò)算法。分享給大家供大家參考,具體如下:
python實(shí)現(xiàn)二層神經(jīng)網(wǎng)絡(luò)
包括輸入層和輸出層
# -*- coding:utf-8 -*- #! python2 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,0,1,1]]).T np.random.seed(1) #init weight value syn0 = 2*np.random.random((3,1))-1 print "腳本之家測(cè)試結(jié)果:" for iter in xrange(100000): l0 = x #the first layer,and the input layer l1 = nonlin(np.dot(l0,syn0)) #the second layer,and the output layer l1_error = y-l1 l1_delta = l1_error*nonlin(l1,True) syn0 += np.dot(l0.T, l1_delta) print "outout after Training:" print l1
這里,
l0:輸入層
l1:輸出層
syn0:初始權(quán)值
l1_error:誤差
l1_delta:誤差校正系數(shù)
func nonlin:sigmoid函數(shù)
這里迭代次數(shù)為100時(shí),預(yù)測(cè)結(jié)果為
迭代次數(shù)為1000時(shí),預(yù)測(cè)結(jié)果為:
迭代次數(shù)為10000,預(yù)測(cè)結(jié)果為:
迭代次數(shù)為100000,預(yù)測(cè)結(jié)果為:
可見迭代次數(shù)越多,預(yù)測(cè)結(jié)果越接近理想值,當(dāng)時(shí)耗時(shí)也越長(zhǎng)。
python實(shí)現(xiàn)三層神經(jīng)網(wǎng)絡(luò)
包括輸入層、隱含層和輸出層
# -*- coding:utf-8 -*- #! python2 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 syn0 = 2*np.random.random((3,4)) - 1 #the first-hidden layer weight value syn1 = 2*np.random.random((4,1)) - 1 #the hidden-output layer weight value print "腳本之家測(cè)試結(jié)果:" 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
運(yùn)行結(jié)果:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 神經(jīng)網(wǎng)絡(luò)(BP)算法Python實(shí)現(xiàn)及應(yīng)用
- Python實(shí)現(xiàn)的三層BP神經(jīng)網(wǎng)絡(luò)算法示例
- python實(shí)現(xiàn)BP神經(jīng)網(wǎng)絡(luò)回歸預(yù)測(cè)模型
- Python與人工神經(jīng)網(wǎng)絡(luò):使用神經(jīng)網(wǎng)絡(luò)識(shí)別手寫圖像介紹
- TensorFlow平臺(tái)下Python實(shí)現(xiàn)神經(jīng)網(wǎng)絡(luò)
- Python實(shí)現(xiàn)的NN神經(jīng)網(wǎng)絡(luò)算法完整示例
- python使用RNN實(shí)現(xiàn)文本分類
- python編寫樸素貝葉斯用于文本分類
- Python使用循環(huán)神經(jīng)網(wǎng)絡(luò)解決文本分類問題的方法詳解
相關(guān)文章
Python深度學(xué)習(xí)pytorch卷積神經(jīng)網(wǎng)絡(luò)LeNet
這篇文章主要為大家講解了Python深度學(xué)習(xí)中的pytorch卷積神經(jīng)網(wǎng)絡(luò)LeNet的示例解析,有需要的朋友可以借鑒參考下希望能夠有所幫助2021-10-10python使用梯度下降和牛頓法尋找Rosenbrock函數(shù)最小值實(shí)例
這篇文章主要介紹了python使用梯度下降和牛頓法尋找Rosenbrock函數(shù)最小值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04通過Folium在地圖上展示數(shù)據(jù)Python地理可視化的入門示例詳解
這篇文章主要介紹了通過Folium在地圖上展示數(shù)據(jù)Python地理可視化的入門,在本文中,我們介紹了如何使用Python中的Folium庫進(jìn)行地理可視化,通過Folium,我們可以輕松地創(chuàng)建交互式地圖,并在地圖上展示數(shù)據(jù)、繪制形狀、添加圖例和文本標(biāo)簽等,需要的朋友可以參考下2024-05-05詳解python opencv圖像混合算術(shù)運(yùn)算
這篇文章主要介紹了python opencv圖像混合算術(shù)運(yùn)算的相關(guān)知識(shí),本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09Python import自己的模塊報(bào)錯(cuò)問題及解決
這篇文章主要介紹了Python import自己的模塊報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02淺析Python打包時(shí)包含靜態(tài)文件處理方法
這篇文章主要介紹了Python打包時(shí)包含靜態(tài)文件處理方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Django 實(shí)現(xiàn)jwt認(rèn)證的示例
這篇文章主要介紹了Django 實(shí)現(xiàn)jwt 認(rèn)證的示例,幫助大家更好的理解和學(xué)習(xí)使用django,感興趣的朋友可以了解下2021-04-04