Python編程實現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法示例
本文實例講述了Python編程實現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法。分享給大家供大家參考,具體如下:
python實現(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 "腳本之家測試結(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時,預(yù)測結(jié)果為

迭代次數(shù)為1000時,預(yù)測結(jié)果為:

迭代次數(shù)為10000,預(yù)測結(jié)果為:

迭代次數(shù)為100000,預(yù)測結(jié)果為:

可見迭代次數(shù)越多,預(yù)測結(jié)果越接近理想值,當(dāng)時耗時也越長。
python實現(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 "腳本之家測試結(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
運行結(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)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 神經(jīng)網(wǎng)絡(luò)(BP)算法Python實現(xiàn)及應(yīng)用
- Python實現(xiàn)的三層BP神經(jīng)網(wǎng)絡(luò)算法示例
- python實現(xiàn)BP神經(jīng)網(wǎng)絡(luò)回歸預(yù)測模型
- Python與人工神經(jīng)網(wǎng)絡(luò):使用神經(jīng)網(wǎng)絡(luò)識別手寫圖像介紹
- TensorFlow平臺下Python實現(xiàn)神經(jīng)網(wǎng)絡(luò)
- Python實現(xiàn)的NN神經(jīng)網(wǎng)絡(luò)算法完整示例
- python使用RNN實現(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-10
python使用梯度下降和牛頓法尋找Rosenbrock函數(shù)最小值實例
這篇文章主要介紹了python使用梯度下降和牛頓法尋找Rosenbrock函數(shù)最小值實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧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

