⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gawnn.m

📁 nnToolKit 神经网络工具包是基于 MATLAB 神经网络工具箱自行开发的一组神经网络算法函数库
💻 M
字号:
function retstr = gawnn()
retstr = -1
clc
clear
tic, %开始计时
% 首先进行遗传算法
[P,T,R,S1,S2,S,Q]=wnninit;
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');
% 下面进行 wnn 算法
figure(2)
% 将遗传算法的结果分解为wnn网络所对应的权值、阈值
[W1, shift_b1, W2, scale_a1, P, T, A1, A2, SE, val]=gadecod(x);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55555

tic;
%初始化网络                       
IN=R;       
HN=S1;      
ON=S2;
N=300;
derros=zeros(1,(N+2));
studyspace=zeros(1,(N+2));
studyspace(1,2)=0.002;
amlf=1.001;
betat=0.999;
kkk=1.04;
mc=0.01;


v(:,:,2)=W1;
w=zeros(1,HN,N+2);
%W2(:,:,1)=rand(1,S1);
w(:,:,2)=W2;
a=zeros(1,HN,N+2);
a(:,:,2)=shift_b1';
b=zeros(1,HN,N+2);
b(:,:,2)=scale_a1';
%cshab;
mse=zeros(1,N+2);
ee=zeros(1,N+2);

%数据进行训练
for n=3:(N+2)
  wdel=zeros(1,HN);
  vdel=zeros(HN,IN); 
  adel=zeros(1,HN);
  bdel=zeros(1,HN);
  wincrease=zeros(1,HN);
  vincrease=zeros(HN,IN);
  aincrease=zeros(1,HN);
  bincrease=zeros(1,HN);
  m=1;
  while m<Q+1
    in=P(:,m);
    r=zeros(1,HN); 
    z=zeros(1,HN);
    dz=zeros(1,HN);
    s=0;
    for j=1:HN
      for i=1:IN
        r(1,j)=r(1,j)+v(j,i,n-1)*in(i,1);
      end
      b1=b(1,j,n-1);
      a1=a(1,j,n-1);
      z(1,j)=hfun(r(1,j),b1,a1);
      dz(1,j)=dhfun(r(1,j),b1,a1);
      s=s+w(1,j,n-1)*z(1,j); 
    end
    y(m)=s;
    EEE=sqrt(abs(y(m)-T(m)));
    e=(T(m)-y(m));
    for j=1:HN
      wdel(1,j)=wdel(1,j)+e*z(1,j) *1.5;    %%%%  修改梯度学习效率系数取得好的效果
      for i=1:IN
        vdel(j,i)=vdel(j,i)+  1.5* e*w(1,j,n-1)*dz(1,j)*in(i,1)/a(1,j,n-1);
      end
      adel(1,j)=adel(1,j)+e *w(1,j,n-1)*dz(1,j)*((r(1,j)-b(1,j,n-1))/a(1,j,n-1))/a(1,j,n-1); 
      bdel(1,j)=bdel(1,j)+e *w(1,j,n-1)*dz(1,j)/a(1,j,n-1);
    end
    derros(1,n)=derros(1,n)+abs( T(m)*log(y(m))+(1-T(m) )*log(1-y(m)) ) ;
    mse(1,n)=mse(1,n)+(y(m)-T(m)).^2;
    MSE(1,n)=sqrt(mse(1,n));
    m=m+1;
  end
  %plot(n,EEE); 
  ee(1,n)=EEE;
  for j=1:HN
    wdel(1,j)=-wdel(1,j);
    for i=1:IN
      vdel(j,i)=-vdel(j,i);
    end
  end
  derros(1,n)=-derros(1,n);

  % 学习速率的调整
  if derros(n)<derros(n-1)
    studyspace(1,n)=amlf*studyspace(1,n-1); 
  end
  if derros(n)>=kkk*derros(n-1)
    studyspace(1,n)=betat*studyspace(1,n-1);
  end
  for j=1:HN
    wincrease(1,j)=-studyspace(1,n)*wdel(1,j)+mc*(w(1,j,n-1)-w(1,j,n-2));
    w(1,j,n)=w(1,j,n-1)+wincrease(1,j);
    for i=1:IN
      vincrease(j,i)=-studyspace(1,n)*vdel(j,i)+mc*(v(j,i,n-1)-v(j,i,n-2));
      v(j,i,n)=v(j,i,n-1)+vincrease(j,i);
    end
    aincrease(1,j)=-studyspace(1,n)*adel(1,j)+mc*(a(1,j,n-1)-a(1,j,n-2));
    a(1,j,n)=a(1,j,n-1)+aincrease(1,j); 
    bincrease(1,j)=-studyspace(1,n)*bdel(1,j)+mc*(b(1,j,n-1)-b(1,j,n-2));
    b(1,j,n)=b(1,j,n-1)+bincrease(1,j);
  end
end
%网络误差曲线
toc
plot(MSE(4:N+2)/Q);   
title('遗传算法优化后的误差')

figure(3)

plot(T,'r')
hold on 
plot(y,'g*');title('遗传算法优化后Wnn的输出')

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -