使用python實(shí)現(xiàn)一段bp神經(jīng)網(wǎng)絡(luò)后,由于數(shù)據(jù)量增多(76條),出現(xiàn)nan報(bào)錯(cuò)
代碼如下:
# coding:utf-8
import numpy as np
import matplotlib.pyplot as plt
def logsig(x):
return 1.0/(1+np.exp(-x))
#76條歸一化數(shù)據(jù)
#輸出層
# 訂單數(shù)
ordernum=[12668619, 13103780, 13257004, 14638190, 14726263, 12600723, 12949334, 13341628, 13169524, 13301632, 15020992, 15606709, 13446703, 13371539, 13262251, 13162602, 13168550, 13598886, 12370827, 11962626, 11717528, 11869467, 12318038, 13303466, 14288269, 12600621, 12957388, 13200367, 13271152, 13051823, 15049197, 16090773, 13568785, 13152151, 12927545, 12827040, 13172809, 14904629, 15239996, 12646447, 12834756, 13102306, 13447091, 13632212, 15747318, 16151994, 13377774, 13633451, 13317559, 13592490, 14065724, 15377027, 15486313, 13024443, 13557400, 13234309, 13538881, 14339158, 15770610, 16017075, 13932597, 14057435, 14136860, 14621550, 15745729, 17343336, 17072830, 14217876, 14457732, 14380455, 14505519, 14957927, 16587364, 16489540, 13943044, 14326370]
# 輸入層 3 個(gè)變量
#是否節(jié)假日(0代表工作日;1代表節(jié)假日)
holiday=[0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0]
#整體在線商家數(shù)
shopnum= [0, 0.0171, 0.0372, 0.0415, 0.0432, 0.0618, 0.0794, 0.0971, 0.1153, 0.1313, 0.1359, 0.1377, 0.1537, 0.1685, 0.1799, 0.1882, 0.1949, 0.1989, 0.2021, 0.2016, 0.1977, 0.1931, 0.1933, 0.1945, 0.1901, 0.2004, 0.2148, 0.2323, 0.2508, 0.2686, 0.2724, 0.2725, 0.2854, 0.3036, 0.3230, 0.3428, 0.3610, 0.3637, 0.3642, 0.3770, 0.3920, 0.4077, 0.4199, 0.4304, 0.4301, 0.4299, 0.4389, 0.4428, 0.4576, 0.4702, 0.4837, 0.4837, 0.4850, 0.5022, 0.5165, 0.5273, 0.5427, 0.5560, 0.5566, 0.5566, 0.5724, 0.5871, 0.6020, 0.6167, 0.6307, 0.6319, 0.6332, 0.6489, 0.6601, 0.6670, 0.6713, 0.6759, 0.6673, 0.6655, 0.6733, 0.6720]
#整體補(bǔ)貼率
subsidyrate=[0.5333, 0.7238, 0.5905, 0.5619, 0.5762, 0.4619, 0.4143, 0.4571, 0.4667, 0.2762, 0.3238, 0.3381, 0.2095, 0.1952, 0.2333, 0.3571, 0.3714, 0.3524, 0.0667, 0.0143, 0.0000, 0.1000, 0.0095, 0.1048, 0.1619, 0.0714, 0.0619, 0.0857, 0.2238, 0.1143, 0.2000, 0.2143, 0.0857, 0.1810, 0.2286, 0.4333, 0.3571, 0.3810, 0.3905, 0.3571, 0.4000, 0.5000, 0.9095, 0.9238, 0.9190, 0.9476, 1.0000, 0.9381, 0.7905, 0.9619, 0.8095, 0.6286, 0.5714, 0.5143, 0.4952, 0.4619, 0.5857, 0.5190, 0.6714, 0.5952, 0.5571, 0.6333, 0.6714, 0.7190, 0.6476, 0.6952, 0.5286, 0.4190, 0.4000, 0.4190, 0.5810, 0.5476, 0.5714, 0.5524, 0.4762, 0.4714]
# 輸入值
samplein = np.mat([holiday,subsidyrate,shopnum]) #3*76
sampleinminmax = np.array([samplein.min(axis=1).T.tolist()[0],samplein.max(axis=1).T.tolist()[0]]).transpose()#3*2,對(duì)應(yīng)最大值最小值
# 輸出值
sampleout = np.mat([ordernum])#1*76
sampleoutminmax = np.array([sampleout.min(axis=1).T.tolist()[0],sampleout.max(axis=1).T.tolist()[0]]).transpose()#2*2,對(duì)應(yīng)最大值最小值
#3*76
sampleinnorm = (2*(np.array(samplein.T)-sampleinminmax.transpose()[0])/(sampleinminmax.transpose()[1]-sampleinminmax.transpose()[0])-1).transpose()
#1*76
sampleoutnorm = (2*(np.array(sampleout.T).astype(float)-sampleoutminmax.transpose()[0])/(sampleoutminmax.transpose()[1]-sampleoutminmax.transpose()[0])-1).transpose()
#給輸出樣本添加噪音
noise = 0.03*np.random.rand(sampleoutnorm.shape[0],sampleoutnorm.shape[1])
sampleoutnorm += noise
# 訓(xùn)練次數(shù)
maxepochs = 1500
# 學(xué)習(xí)速率
learnrate = 0.035
# 誤差率
errorfinal = 0.65*10**(-3)
#errorfinal = 0.05
# 數(shù)據(jù)量
samnum = 76
# 輸入數(shù)
indim = 3
# 輸出數(shù)
outdim = 1
hiddenunitnum = 8 # 2~10
# 隨機(jī)產(chǎn)生權(quán)值和偏置
w1 = 0.5*np.random.rand(hiddenunitnum,indim)-0.1
b1 = 0.5*np.random.rand(hiddenunitnum,1)-0.1
w2 = 0.5*np.random.rand(outdim,hiddenunitnum)-0.1
b2 = 0.5*np.random.rand(outdim,1)-0.1
errhistory = []
for i in range(maxepochs):
hiddenout = logsig((np.dot(w1,sampleinnorm).transpose()+b1.transpose())).transpose()
hiddenout = np.nan_to_num(hiddenout)
networkout = (np.dot(w2,np.nan_to_num(hiddenout)).transpose()+b2.transpose()).transpose()
err = sampleoutnorm - np.nan_to_num(networkout)
sse = sum(sum(err**2))
errhistory.append(sse)
if sse < errorfinal:
break
delta2 = err
delta1 = np.dot(w2.transpose(),delta2)*hiddenout*(1-hiddenout)
dw2 = np.dot(delta2,hiddenout.transpose())
db2 = np.dot(delta2,np.ones((samnum,1)))
dw1 = np.dot(delta1,sampleinnorm.transpose())
dw1 = np.nan_to_num(dw1)
db1 = np.dot(delta1,np.ones((samnum,1)))
# db1 = np.nan_to_num(db1)
# w2 += learnrate*dw2
# b2 += learnrate*db2
w2 = np.nan_to_num(w2+learnrate * dw2)
b2 = np.nan_to_num(b2+learnrate*db2)
w1 += learnrate*dw1
b1 += learnrate * db1
# b1 = np.nan_to_num(b1+learnrate*db1)
# print 'db1=', db1
# print 'db1=', db1
# print 'err=',err,'w1=',w1,'w2=',w2,'b1=',b1,'b2=',b2
# 誤差曲線圖
errhistory10 = np.log10(errhistory)
minerr = min(errhistory10)
plt.plot(errhistory10)
plt.plot(range(0,i+1000,1000),[minerr]*len(range(0,i+1000,1000)))
ax=plt.gca()
ax.set_yticks([-2,-1,0,1,2,minerr])
ax.set_yticklabels([u'$10^{-2}$',u'$10^{-1}$',u'$1$',u'$10^{1}$',u'$10^{2}$',str(('%.4f'%np.power(10,minerr)))])
ax.set_xlabel('iteration')
ax.set_ylabel('error')
ax.set_title('Error History')
plt.savefig('total-errorhistory.png',dpi=700)
plt.close()
# 仿真輸出和實(shí)際輸出對(duì)比圖
hiddenout = logsig((np.dot(w1,sampleinnorm).transpose()+b1.transpose())).transpose()
networkout = (np.dot(w2,hiddenout).transpose()+b2.transpose()).transpose()
diff = sampleoutminmax[:,1]-sampleoutminmax[:,0]
networkout2 = (networkout+1)/2
networkout2[0] = networkout2[0]*diff[0]+sampleoutminmax[0][0]
#networkout2[1] = networkout2[1]*diff[1]+sampleoutminmax[1][0]
sampleout = np.array(sampleout)
print 'networkout',networkout
fig,axes = plt.subplots(nrows=2,ncols=1,figsize=(12,10))
line1, =axes[0].plot(networkout2[0],'k',marker = u'$\circ$')
line2, = axes[0].plot(sampleout[0],'r',markeredgecolor='b',marker = u'$\star$',markersize=9)
print "chazhi=",(networkout2[0]-sampleout[0])
print"準(zhǔn)確率=",(sampleout[0]/networkout2[0])
print"誤差率=",((networkout2[0]-sampleout[0])/networkout2[0])
axes[0].legend((line1,line2),('simulation output','real output'),loc = 'upper left')
yticks = [10000000,12000000,14000000,16000000,18000000]
ytickslabel = [u'$10$',u'$12$',u'$14$',u'$16$',u'$18$']
axes[0].set_yticks(yticks)
axes[0].set_yticklabels(ytickslabel)
axes[0].set_ylabel('ordernum')
xticks = range(0,30,1)
xtickslabel = range(0,30,1)
axes[0].set_xticks(xticks)
axes[0].set_xticklabels(xtickslabel)
axes[0].set_xlabel(u'date')
axes[0].set_title('ordernum')
fig.savefig('total-simulation.png',dpi=500,bbox_inches='tight')
plt.close()
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。