资源描述:
《bp神经网络MATLAB程序.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、%清空环境变量clcclear%读取数据num=xlsread('C:UsersAdministratorDesktop模型训练matlab正样本标记.xls',1,'C3:AN52');num1=xlsread('C:UsersAdministratorDesktop模型训练matlab负样本标记.xls',1,'C3:AN52');训练数据输入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];训练数据输出a1=o
3、nes(50,1)*1;a2=ones(50,1)*0;output=[a1;a2];savedata1.matbsavedata2.mataloaddata1loaddata2%节点个数inputnum=100;hiddennum=50;outputnum=100;%训练数据和预测数据input_train=Input;output_train=output;input_test=a(:,91:137);output_test=b(:,91:137);%将训练样本输入输出数据归一化[inputn,inputps]=mapminmax(input_t
4、rain);[outputn,outputps]=mapminmax(output_train);[pn,minp,maxp,tn,mint,maxt]=premnmx(input,output);%将数据归一化NodeNum1=20;%隐层第一层节点数NodeNum2=40;%隐层第二层节点数TypeNum=1;%输出维数TF1='tansig';TF2='tansig';TF3='tansig';net=newff(minmax(pn),[NodeNum1,NodeNum2,TypeNum],{TF1TF2TF3},'traingdx');%网络
5、创建traingdmnet.trainParam.show=50;net.trainParam.epochs=50000;%训练次数设置net.trainParam.goal=1e-5;%训练所要达到的精度net.trainParam.lr=0.01;%学习速率net=train(net,pn,tn);p2n=tramnmx(ptest,minp,maxp);%测试数据的归一化an=sim(net,p2n);[a]=postmnmx(an,mint,maxt)%数据的反归一化,即最终想得到的预测结果plot(1:length(t),t,'o',1:l
6、ength(t)+1,a,'+');title('o表示预测值---*表示实际值')gridonm=length(a);%向量a的长度t1=[t,a(m)];error=t1-a;%误差向量figureplot(1:length(error),error,'-.')title('误差变化图')gridon%构建BP神经网络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神经网络训练net=train(net,inputn,outputn);%测试样本归一化inputn_test=mapminmax('apply',input_test,inputps);%BP神经网络预测an=sim(net,inputn_test);%%网络得到数据反归一化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