python實(shí)現(xiàn)簡單神經(jīng)網(wǎng)絡(luò)算法
python實(shí)現(xiàn)簡單神經(jīng)網(wǎng)絡(luò)算法,供大家參考,具體內(nèi)容如下
python實(shí)現(xiàn)二層神經(jīng)網(wǎng)絡(luò)
包括輸入層和輸出層
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
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
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
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ù)越多,預(yù)測結(jié)果越接近理想值,當(dāng)時(shí)耗時(shí)也越長。
python實(shí)現(xiàn)三層神經(jīng)網(wǎng)絡(luò)
包括輸入層、隱含層和輸出層
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
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
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
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中xml.etree.ElementTree的使用示例
ElementTree是Python標(biāo)準(zhǔn)庫中的一個(gè)模塊,專門用于處理XML文件,它提供了解析、創(chuàng)建、修改和遍歷XML文檔的API,非常適合處理配置文件、數(shù)據(jù)交換格式和Web服務(wù)響應(yīng)等場景,本文就來介紹一下,感興趣的可以了解一下2024-09-09
一起來學(xué)習(xí)一下python的數(shù)據(jù)類型
這篇文章主要為大家詳細(xì)介紹了python的數(shù)據(jù)類型,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下希望能夠給你帶來幫助2022-01-01
python基礎(chǔ)教程之簡單入門說明(變量和控制語言使用方法)
這篇文章主要介紹了開始學(xué)習(xí)python的第一步需要知道的知識(變量和控制語言使用方法),需要的朋友可以參考下2014-03-03
Django 使用easy_thumbnails壓縮上傳的圖片方法
今天小編就為大家分享一篇Django 使用easy_thumbnails壓縮上傳的圖片方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承簡單示例
這篇文章主要介紹了Python面向?qū)ο蟪绦蛟O(shè)計(jì)之類的定義與繼承,結(jié)合完整實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類的定義、調(diào)用、繼承及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-03-03

