bp神經(jīng)網(wǎng)絡(luò)詳細(xì)步驟C#實(shí)現(xiàn)_第1頁
bp神經(jīng)網(wǎng)絡(luò)詳細(xì)步驟C#實(shí)現(xiàn)_第2頁
bp神經(jīng)網(wǎng)絡(luò)詳細(xì)步驟C#實(shí)現(xiàn)_第3頁
bp神經(jīng)網(wǎng)絡(luò)詳細(xì)步驟C#實(shí)現(xiàn)_第4頁
bp神經(jīng)網(wǎng)絡(luò)詳細(xì)步驟C#實(shí)現(xiàn)_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem;usingSystem.IO;usingSystem.Text;namespaceBpANNet{〃/<summary>〃/BpNet的摘要說明。:〃/</summary>publicclassBpNet{publicintinNum;//輸入節(jié)點(diǎn)數(shù)inthideNum;//隱層節(jié)點(diǎn)數(shù)publicintoutNum;//輸出層節(jié)點(diǎn)數(shù)publicintsampleNum;//樣本總數(shù)RandomR;double口x;//輸入節(jié)點(diǎn)的輸入數(shù)據(jù)double口x1;//隱層節(jié)點(diǎn)的輸出double口x2;//輸出節(jié)點(diǎn)的輸出double口o1;//隱層的輸入double口o2;//輸出層的輸入publicdouble[,]w;//權(quán)值矢I陣w,這是輸入層與隱藏層之間的權(quán)值矩陣publicdouble[,]v;//權(quán)值矢I陣V,這是隱藏層與輸出層之間的權(quán)值矩陣publicdouble[,]dw;//權(quán)值矢I陣wpublicdouble[,]dv;//權(quán)值矢I陣Vpublicdoublerate;//學(xué)習(xí)率publicdouble口b1;//隱層閾值矩陣publicdouble口b2;//輸出層閾值矩陣publicdouble口db1;//隱層閾值矩陣publicdouble口db2;//輸出層閾值矩陣double口pp;//隱藏層的誤差double[]qq;//輸出層的誤差double口yd;//輸出層的教師數(shù)據(jù),所謂教師數(shù)據(jù)就是實(shí)際數(shù)據(jù)而已!publicdoublee;//均方誤差doublein_rate;//歸一化比例系數(shù)//用于確定隱藏層的神經(jīng)細(xì)胞數(shù)publicintcomputeHideNum(intm,intn){doubles=Math.Sqrt(0.43*m*n+0.12*n*n+2.54*m+0.77*n+0.35)+0.51;intss=Convert.ToInt32(s);return((s-(double)ss)>0.5)?ss+1:ss;)publicBpNet(double[,]p,double[,]t){//構(gòu)造函數(shù)邏輯R=newRandom();this.inNum=p.GetLength(1);this.outNum=t.GetLength(1);this.hideNum=computeHideNum(inNum,outNum);//this.hideNum=18;this.sampleNum=p.GetLength(0);Console.WriteLine("輸入節(jié)點(diǎn)數(shù)目:"+inNum);Console.WriteLine("隱層節(jié)點(diǎn)數(shù)目:"+hideNum);Console.WriteLine("輸出層節(jié)點(diǎn)數(shù)目:"+outNum);Console.ReadLine();//將這些矩陣規(guī)定好矩陣大小x=newdouble[inNum];x1=newdouble[hideNum];x2=newdouble[outNum];o1=newdouble[hideNum];o2=newdouble[outNum];w=newdouble[inNum,hideNum];//權(quán)值矢I陣w,這是輸入層與隱藏層之間的權(quán)值矩陣v=newdouble[hideNum,outNum];dw=newdouble[inNum,hideNum];dv=newdouble[hideNum,outNum];//閾值b1=newdouble[hideNum];b2=newdouble[outNum];db1=newdouble[hideNum];db2=newdouble[outNum];//誤差pp=newdouble[hideNum];//隱藏層的誤差qq=newdouble[outNum];//輸出層的誤差yd=newdouble[outNum];//輸出層的教師數(shù)據(jù)//初始化wfor(inti=0;i<inNum;i++){for(intj=0;j<hideNum;j++){//NextDouble返回一個(gè)介于0.0和1.0之間的隨機(jī)數(shù)。w[i,j]=(R.NextDouble()*2-1.0)/2;}}//初始化vfor(inti=0;i<hideNum;i++){for(intj=0;j<outNum;j++){v[i,j]=(R.NextDouble()*2-1.0)/2;}}rate=0.8;e=0.0;in_rate=1.0;}//訓(xùn)練函數(shù)publicvoidtrain(double[,]p,double[,]t){e=0.0;//★求p,t中的最大值doublepMax=0.0;//sampleNum為樣本總數(shù)for(intisamp=0;isamp<sampleNum;isamp++)(//inNum是輸入層的節(jié)點(diǎn)數(shù)(即神經(jīng)細(xì)胞數(shù))for(inti=0;i<inNum;i++)(if(Math.Abs(p[isamp,i])>pMax)(pMax=Math.Abs(p[isamp,i]);}}for(intj=0;j<outNum;j++)(if(Math.Abs(t[isamp,j])>pMax)(pMax=Math.Abs(t[isamp,j]);}}in_rate=pMax;}//endisampfor(intisamp=0;isamp<sampleNum;isamp++)(//★數(shù)據(jù)歸一化for(inti=0;i<inNum;i++)(x[i]=p[isamp,i]/in_rate;}for(inti=0;i<outNum;i++)(yd[i]=t[isamp,i]/in_rate;}//計(jì)算隱層的輸入和輸出for(intj=0;j<hideNum;j++)(o1[j]=0.0;for(inti=0;i<inNum;i++)(o1[j]+=w[i,j]*x[i];//“權(quán)值”*“輸入”的那個(gè)累加的過程)//這個(gè)b1[j]就是隱藏層的閾值,閾值就是一個(gè)輸入為“-1”的累加值x1[j]=1.0/(1.0+Math.Exp(-o1[j]-b1[j]));)//計(jì)算輸出層的輸入和輸出for(intk=0;k<outNum;k++)(o2[k]=0.0;for(intj=0;j<hideNum;j++)(o2[k]+=v[j,k]*x1[j];)x2[k]=1.0/(1.0+Math.Exp(-o2[k]-b2[k]));)//計(jì)算輸出層誤差和均方差for(intk=0;k<outNum;k++)(//yd[k]是輸出層的教師數(shù)據(jù),所謂教師數(shù)據(jù)就是實(shí)際應(yīng)該輸出的數(shù)據(jù)而已qq[k]=(yd[k]-x2[k])*x2[k]*(1.0-x2[k]);e+=(yd[k]-x2[k])*(yd[k]-x2[k]);//更新V,V矩陣是隱藏層與輸出層之間的權(quán)值for(intj=0;j<hideNum;j++)(v[j,k]+=rate*qq[k]*x1[j];))//計(jì)算隱層誤差for(intj=0;j<hideNum;j++)(//PP矩陣是隱藏層的誤差pp[j]=o.0;//算法參考我的視頻截圖for(intk=0;k<outNum;k++)(pp[j]+=qq[k]*v[j,k];}pp[j]=pp[j]*x1[j]*(1-x1[j]);//更新Wfor(inti=0;i<inNum;i++)(w[i,j]+=rate*pp[j]*x[i];}}//更新b2,輸出層的閾值for(intk=0;k<outNum;k++){b2[k]+=rate*qq[k];}//更新b1,隱藏層的閾值for(intj=0;j<hideNum;j++){]b1[j]+=rate*pp[j];}}//endisampe=Math.Sqrt(e);//均方差//adjustWV(w,dw);//adjustWV(v,dv);}//endtrainpublicvoidadjustWV(double[,]w,double[,]dw){for(inti=0;i<w.GetLength(0);i++)for(intj=0;j<w.GetLength(1);j++){w[i,j]+=dw[i,j];}}}publicvoidadjustWV(double口w,double口dw){for(inti=0;i<w.Length;i++){w[i]+=dw[i];}}//數(shù)據(jù)仿真函數(shù)publicdouble口sim(double[]psim)(for(inti=0;i<inNum;i++)x[i]=psim[i]/in_rate;//in_rate為歸一化系數(shù)for(intj=0;j<hideNum;j++)(o1[j]=0.0;二for(inti=0;i<inNum;i++)o1[j]=o1[j]+w[i,j]*x[i];x1[j]=1.0/(1.0+Math.Exp(-o1[j]-b1[j]));}for(intk=0;k<outNum;k++){o2[k]=0.0;for(intj=0;j<hideNum;j++)o2[k]=o2[k]+v[j,k]*x1[j];x2[k]=1.0/(1.0+Math.Exp(-o2[k]-b2[k]));x2[k]=in_rate*x2[k];returnx2;}//endsim//保存矩陣w,vpublicvoidsaveMatrix(double[,]w,stringfilename){StreamWritersw=File.CreateText(filename);for(inti=0;i<w.GetLength(0);i++){for(intj=0;j<w.GetLength(1);j++){sw.Write(w[i,j]+"");}sw.WriteLine();}sw.Close();}〃保存矩陣b1,b2publicvoidsaveMatrix(double[]b,stringfilename){StreamWritersw=File.CreateText(filename);for(inti=0;i<b.Length;i++){sw.Write(b[i]+"");}sw.Close();}〃讀取矩陣W,VpublicvoidreadMatrixW(double[,]w,stringfilename){一StreamReadersr;trysr=newStreamReader(filename,Encoding.GetEncoding("gb2312"));Stringline;inti=0;while((line=sr.ReadLine())!=null){string[]s1=line.Trim().Split('');for(intj=0;j<s1.Length;j++)w[i,j]=Convert.ToDouble(s1[j]);}i++;}sr.Close();}catch(Exceptione){//Lettheuserknowwhatwentwrong.Console.WriteLine("Thefilecouldnotberead:");Console.WriteLine(e.Message);}}〃讀取矩陣b1,b2publicvoidreadMatrixB(double[]b,stringfilename)(StreamReadersr;try{Zlsr=newStreamReader(filename,Encoding.GetEncoding("gb2312"));Stringline;inti=0;while((line=sr.ReadLine())!=null)b[i]=Convert.ToDouble(line);i++;}sr.Close();}catch(Exceptione)(//Lettheuserknowwhatwentwrong.Console.WriteLine("Thefilecouldnotberead:");Console.WriteLine(e.Message);I}}}//endbpnet}//endnamespace//主調(diào)用程序namespaceBpANNet(///<summary>///Class1的摘要說明。///</summary>classClass1///<summary>///應(yīng)用程序的主入口點(diǎn)。///</summary>[STAThread]staticvoidMain(string[]args)〃0.1399,0.1467,0.1567,0.1595,0.1588,0.1622,0.1611,0.1615,0.1685,0.1789,0.1790//double[,]p1=newdouble[,]{{0.05,0.02},{0.09,0.11},{0.12,0.20},{0.15,0.22},{0.20,0.25},{0.75,0.75},{0.80,0.83},{0.82,0.80},{0.90,0.89},{0.95,0.89},{0.09,0.04},{0.1,0.1},{0.14,0.21},{0.18,0.24},{0.22,0.28},{0.77,0.78},{0.79,0.81},{0.84,0.82},{0.94,0.93},{0.98,0.99}};//double[,]t1=newdouble[,]{{1,0},{1,0},{1,0},{1,0},{1,0},{0,1},{0,1},{0,1},{0,1},{0,1},{1,0},{1,0},{1,0},{1,0},{1,0},{0,1},{0,1},{0,1},{0,1},{0,1}};//p1是輸入的信息,一共5組,輸入層為六個(gè)節(jié)點(diǎn),p1[5][6]double[,]p1=newdouble[,]{{0.1399,0.1467,0.1567,0.1595,0.1588,0.1622},{0.1467,0.1567,0.1595,0.1588,0.1622,0.1611},{0.1567,0.1595,0.1588,0.1622,0.1611,0.1615},{0.1595,0.1588,0.1622,0.1611,0.1615,0.1685},{0.1588,0.1622,0.1611,0.1615,0.1685,0.1789}};//t1是輸出信息,一共6組,t1[6][1]double[,]t1=newdouble[,]{{0.1622},{0.1611},{0.1615},{0.1685},{0.1789},{0.1790}};BpNetbp=newBpNet(p1,t1);intstudy=0;~|do|{study++;bp.train(p1,t1);//bp.rate=0.95-(0.95-0.3)*study/50000;//Console.Write("第"+study+"次學(xué)習(xí):");//Console.WriteLine("均方差為"+bp.e);}while(bp.e>0.001&&study<50000);Console.Write("第"+study+"次學(xué)習(xí):");Console.WriteLine("均方差為"+bp.e);bp.saveMatrix(bp.w,"w.txt");bp.saveMa

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論