24小时热门版块排行榜    

查看: 389  |  回复: 0

haomeng12

新虫 (小有名气)

[求助] 遗传算法BP预测中调试程序的问题

求助各位高手,下面程序中,数据不太清楚,能否有人告知'data.txt'中的数据几行几列?我自己输上了45行14列,程序运行后,老是出现“Index exceeds matrix dimensions”
程序如下:
clear all
close all
p=textread('data.txt');
p1=p';
t=p1;
[pn,minp,maxp,tn,mint,maxt] = premnmx(p1,t);
p_train=[pn(1,:);pn(2,:);pn(3,:);pn(4,:);pn(5,:);pn(6,:);pn(7,:);pn(8,:);pn(9,:);pn(10,:);pn(11,:);pn(12,:);pn(13,:);...
    pn(14,:);pn(15,:);pn(16,:);pn(17,:);pn(18,:);pn(19,:);pn(20,:);pn(21,:);pn(22,:);pn(23,:);pn(24,:);pn(25,:);pn(26,:);pn(27,:);...
    pn(28,:);pn(29,:);pn(30,:);pn(31,:);pn(32,:);pn(33,:);pn(34,:);pn(35,:);pn(36,:);pn(37,:);pn(38,:);pn(39,:)]'
t_train=[pn(2,:);pn(3,:);pn(4,:);pn(5,:);pn(6,:);pn(7,:);pn(8,:);pn(9,:);pn(10,:);pn(11,:);pn(12,:);pn(13,:);pn(14,:);pn(15,:);pn(16,:);...
    pn(17,:);pn(18,:);pn(19,:);pn(20,:);pn(21,:);pn(22,:);pn(23,:);pn(24,:);pn(25,:);pn(26,:);pn(27,:);pn(28,:);pn(29,:);pn(30,:);...
    pn(31,:);pn(32,:);pn(33,:);pn(34,:);pn(35,:);pn(36,:);pn(37,:);pn(38,:);pn(39,:);;pn(40,:)]';
save p_train
save t_train
threshold=minmax(p_train);
%创建网络
net=newff(threshold,[12,25,12],{'tansig','tansig','purelin'},'trainlm');
%下面使用遗传算法对网络进行优化
P=p_train;
T=t_train;
R=size(P,1);
S2=size(T,1);
S1=25;%隐含层节点数
S=R*S1+S1*S2+S1+S2;%遗传算法编码长度
aa=ones(S,1)*[-1,1];
popu=50;%种群规模
initPpp=initializega(popu,aa,'gabpEval');%初始化种群
gen=100;%遗传代数
%下面调用gaot工具箱,其中目标函数定义为gabpEval
[x,endPop,bPop,trace]=ga(aa,'gabpEval',[],initPpp,[1e-6 1 1],'maxGenTerm',gen,...
   'normGeomSelect',[0.09],['arithXover'],[2],'nonUnifMutation',[2 gen 3]);
%绘收敛曲线图
figure(1)
plot(trace(:,1),1./trace(:,3),'r-');
hold on
plot(trace(:,1),1./trace(:,2),'b-');
xlabel('Generation');
ylabel('Sum-Squared Error');
figure(2)
plot(trace(:,1),trace(:,3),'r-');
hold on
plot(trace(:,1),trace(:,2),'b-');
xlabel('Generation');
ylabel('Fittness');
%下面将初步得到的权值矩阵赋给尚未开始训练的BP网络
[W1,B1,W2,B2,P,T,A1,A2,SE,val]=gadecod(x);
net.LW{2,1}=W1;
net.LW{3,2}=W2;
net.b{2,1}=B1;
net.b{3,1}=B2;
XX=P;
YY=T;
%设置训练参数
net.trainParam.show=10;
net.trainParam.lr=0.05;
net.trainParam.epochs=7000;
net.trainParam.goal=0.01;
%训练网络
net=train(net,p_train,t_train);
p_test=[pn(40,:)]';   
T=sim(net,p_test);  
a = postmnmx(T',mint(41,1),maxt(41,1));
number=round(a);
error=p1(41,:)-number;
time=[1 2 3 4 5 6 7 8 9 10 11 12];
plot(time,number,'ro--');
hold on
plot(time,p1(41,:),'gd-')
hold on
plot(time,error)
legend('red is yuce','green is actual','blue is error');
title('time(month)')
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 haomeng12 的主题更新
信息提示
请填处理意见