最优隐神经元数目 算法 matlab,请问Matlab如何用遗传算法优化神经网络隐含层神经元个数...

close all

clear all

clc

global p

global t

global nvar

global ncase

global s1

global s2

global cl

disp('开始运行基于LM正则优化的GABP神经网络...单击任意键开始')

pause

load data.mat;

p=temperament;

t=attitude;

[p,minp,maxp,t,mint,maxt]=premnmx(p,t);

disp('1.初始化神经网络...单击任意键开始')

pause

s1=input('请输入隐含层神经细胞数目:');

s2=input('请输入输出层神经细胞数目:');

[nvar ncase]=size(p);

cl=nvar*s1+s1*s2+s1+s2;

popu=100;

varb=ones(cl,1)*[-1 1];

initPop=initializega(popu,varb,'gabpEval');

gen=200;

[x,endPop,bPop,trace]=ga(varb,'gabpEval',[],initPop,[1e-6 1 0],'maxGenTerm',gen,...

'normGeomSelect',[0.09],['arithXover'],[2],'nonUnifMutation',[2 gen 3]);

[iw,b1,lw,b2,val]=gadecode(x);

net=newff(minmax(p),[s1,s2],{'tansig','purelin'},'trainlm');

net.IW{1,1}=iw;

net.b{1}=b1;

net.LW{2,1}=lw;

net.b{2}=b2;

net.trainParam.show=input('请输入训练显示间隔:');

net.trainParam.epochs=input('请输入训练迭代参数:');

net.trainParam.goal=input('请输入训练目标参数:');

net.trainParam.lr=input('请输入训练速度参数:');

net.trainParam.mc=input('请输入动量参数:');

disp('2.训练神经网络...单击任意键开始')

pause

[net,tr]=train(net,p,t);

disp('3.仿真神经网络...单击任意键开始')

pause

st=sim(net,p);

fprintf('均方误差mse:%.4g \n',mse(t-st));

r=corrcoef(t,st);

r2=r(1,2)^2;

fprintf('决定系数β:%.4g \n',r2);

disp('4.自变量对因变量贡献值分析...单击任意键开始')

pause

p=[ones(1,ncase);p];

w=[net.b{1} net.iw{1}];

v=net.lw{2};

h=w*p;

H=2./(1+exp(-2*h))-1;

y=net.lw{2}*H+net.b{2};

C=zeros(s1,nvar);

c=zeros(1,s1);

for j=1:nvar

for i=1:s1

pm1=cov(h(i,:),p(j+1,:),1);

pm1=pm1(1,2);

pm2=var(h(i,:),1);

C(i,j)=pm1*w(i,j+1)/pm2;

end

end

for i=1:s1

pm3=cov(H(i,:),y,1);

pm3=pm3(1,2);

pm4=var(y,1);

c(i)=pm3*v(i)/pm4;

end

pm5=cov(y,t,1);

pm5=pm5(1,2);

pm6=var(t,1);

cp=pm5/pm6;

z=c*C*cp;

你可能感兴趣的:(最优隐神经元数目,算法,matlab)