文档详情

用遗传算法优化BP神经网络程序.doc

发布:2017-01-31约3.76千字共8页下载文档
文本预览下载声明
红色程序是用遗传算法直接训练BP权值 tic, % 开始计时 [P,T,R,S1,S2,S]=nninit; % BP网络初始化 % 随机生成W1,B1,W2,B2 [W1,B1]=rands(S1,R); [W2,B2]=rands(S2,S1); % BP网络训练的参数设置 disp_fqre=100; max_epoch=3000;err_goal=0.002;lr=0.01; TP=[disp_fqre max_epoch err_goal lr]; % 开始训练 [W1,B1,W2,B2,te,tr]=trainbp(W1,B1,tansig,W2,B2,purelin,P,T,TP); % 仿真结果 TT=simuff(P,W1,B1,tansig,W2,B2,purelin) toc % 结束计时 % 在MATLAB6.5下编程 tic, % 开始计时 [P,T,R,S1,S2,S]=nninit; % BP网络初始化 % 生成BP网络 net = newff(minmax(P),[S1 S2],{logsig logsig},trainlm); % BP网络训练的参数设置 net.trainParam.epochs = 3000; net.trainParam.goal = 0.002; net.trainParam.lr = 0.01; % 开始训练 net = train(net,P,T); % 仿真结果 TT=sim(net,P) toc % 结束计时 % 遗传算法的适应值计算 function [sol, val] = gabpEval(sol,options) % val - the fittness of this individual % sol - the individual, returned to allow for Lamarckian evolution % options - [current_generation] [P,T,R,S1,S2,S]=nninit; for i=1:S, x(i)=sol(i); end; [W1, B1, W2, B2, P, T, A1, A2, SE, val]=gadecod(x); tic, %开始计时 % 首先进行遗传算法 [P,T,R,S1,S2,S]=nninit; aa=ones(S,1)*[-1 1]; popu=30; initPpp=initializega(popu,aa,gabpEval); gen=80; % 遗传代数 % 遗传计算 [x endPop bPop trace]=ga(aa,gabpEval,[],initPpp,[1e-6 1 1],maxGenTerm,gen,... normGeomSelect,[0.09],[arithXover],[2],nonUnifMutation,[2 gen 3]); % x The best found % Lets take a look at the performance of the ga during the run subplot(2,1,1) plot(trace(:,1),1./trace(:,3),r-) hold on plot(trace(:,1),1./trace(:,2),b-) xlabel(Generation); ylabel(Sum-Squared Error); subplot(2,1,2) plot(trace(:,1),trace(:,3),r-) hold on plot(trace(:,1),trace(:,2),b-) xlabel(Generation); ylabel(Fittness); % 下面进行BP算法 figure(2) % 将遗传算法的结果分解为BP网络所对应的权值、阈值 [W1 B1 W2 B2 P T A1 A2 SE val]=gadecod(x); % BP网络训练的参数设置 disp_fqre=100; max_epoch=3000;err_goal=0.002;lr=0.01; TP=[disp_fqre max_epoch err_goal lr]; [W1,B1,W2,B2,te,tr]=trainbp(W1,B1,tansig,W2,B2,purelin,P,T,TP); % 仿真结果 TT=simuff(P,W1,B1,tansig,W2,B2,purelin) toc % 结束计时 % 将遗传算法的编
显示全部
相似文档