資源描述:
《bp神經(jīng)網(wǎng)絡(luò)MATLAB程序.doc》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、%清空環(huán)境變量clcclear%讀取數(shù)據(jù)num=xlsread('C:UsersAdministratorDesktop模型訓(xùn)練matlab正樣本標記.xls',1,'C3:AN52');num1=xlsread('C:UsersAdministratorDesktop模型訓(xùn)練matlab負樣本標記.xls',1,'C3:AN52');訓(xùn)練數(shù)據(jù)輸入fu_input=[num1(:,1),num1(:,3),num1(:,5),num1(:,7),num1(:,10),num1(:,12),num1(:,14),num1(:,16)
2、,num1(:,18),num1(:,20),num1(:,24),num1(:,26),num1(:,33),num1(:,35),num1(:,37)];zheng_input=[num(:,1),num(:,3),num(:,5),num(:,7),num(:,10),num(:,12),num(:,14),num(:,16),num(:,18),num(:,20),num(:,24),num(:,26),num(:,33),num(:,35),num(:,37)];Input=[fu_input;zheng_input];訓(xùn)練數(shù)據(jù)輸出a1=o
3、nes(50,1)*1;a2=ones(50,1)*0;output=[a1;a2];savedata1.matbsavedata2.mataloaddata1loaddata2%節(jié)點個數(shù)inputnum=100;hiddennum=50;outputnum=100;%訓(xùn)練數(shù)據(jù)和預(yù)測數(shù)據(jù)input_train=Input;output_train=output;input_test=a(:,91:137);output_test=b(:,91:137);%將訓(xùn)練樣本輸入輸出數(shù)據(jù)歸一化[inputn,inputps]=mapminmax(input_t
4、rain);[outputn,outputps]=mapminmax(output_train);[pn,minp,maxp,tn,mint,maxt]=premnmx(input,output);%將數(shù)據(jù)歸一化NodeNum1=20;%隱層第一層節(jié)點數(shù)NodeNum2=40;%隱層第二層節(jié)點數(shù)TypeNum=1;%輸出維數(shù)TF1='tansig';TF2='tansig';TF3='tansig';net=newff(minmax(pn),[NodeNum1,NodeNum2,TypeNum],{TF1TF2TF3},'traingdx');%網(wǎng)絡(luò)
5、創(chuàng)建traingdmnet.trainParam.show=50;net.trainParam.epochs=50000;%訓(xùn)練次數(shù)設(shè)置net.trainParam.goal=1e-5;%訓(xùn)練所要達到的精度net.trainParam.lr=0.01;%學(xué)習(xí)速率net=train(net,pn,tn);p2n=tramnmx(ptest,minp,maxp);%測試數(shù)據(jù)的歸一化an=sim(net,p2n);[a]=postmnmx(an,mint,maxt)%數(shù)據(jù)的反歸一化,即最終想得到的預(yù)測結(jié)果plot(1:length(t),t,'o',1:l
6、ength(t)+1,a,'+');title('o表示預(yù)測值---*表示實際值')gridonm=length(a);%向量a的長度t1=[t,a(m)];error=t1-a;%誤差向量figureplot(1:length(error),error,'-.')title('誤差變化圖')gridon%構(gòu)建BP神經(jīng)網(wǎng)絡(luò)net=newff(inputn,outputn,[20,12],{'tansig','purelin'},'traingdx');net.trainParam.epochs=1000;net.trainParam.lr=0.01;
7、net.trainParam.goal=0.00000001;%BP神經(jīng)網(wǎng)絡(luò)訓(xùn)練net=train(net,inputn,outputn);%測試樣本歸一化inputn_test=mapminmax('apply',input_test,inputps);%BP神經(jīng)網(wǎng)絡(luò)預(yù)測an=sim(net,inputn_test);%%網(wǎng)絡(luò)得到數(shù)據(jù)反歸一化BPoutput=mapminmax('reverse',an,outputps);d=BPoutput>cfigure(1)%plot(d,':og');scatter(1:(137-90),d,'rx');
8、holdon;%plot(output_test,'-*');scatter(1:(137-90),outp