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

📄 bp2.m

📁 用调用函数法实现bp网络
💻 M
字号:
%批处理1个隐含层—取误差最小的隐层单元数目
clear Re Pr
q1=4:13;%q1为常向量,表示隐含层神经元的个数
Ir=0.012; max_epoch=5000;
EE=zeros(length(q1),max_epoch);
res=zeros(size(q1));%res将要存储误差向量,这里先置零 
for aa=1:length(q1)
clear oi ok wwji wwkj b1 b2 deltak deltao yy result cout1 count2 E
  ceshiyangben=c19;err_goal=0.02;
  tic  
  for i=1:48                           %定义输出目标向量
  target(i,:)=[0 0 0 0 0 0 0 0 0 0 1];
  end
  for i=49:96
  target(i,:)=[0 0 0 0 0 0 0 0 0 1 0];
  end
  for i=97:144
  target(i,:)=[0 0 0 0 0 0 0 0 1 0 0];
  end
  for i=145:192
  target(i,:)=[0 0 0 0 0 0 0 1 0 0 0];
  end
  for i=193:240
  target(i,:)=[0 0 0 0 0 0 1 0 0 0 0];
  end
  for i=241:288
  target(i,:)=[0 0 0 0 0 1 0 0 0 0 0];
  end
  for i=289:336
  target(i,:)=[0 0 0 0 1 0 0 0 0 0 0];
  end
  for i=337:384
  target(i,:)=[0 0 0 1 0 0 0 0 0 0 0];
  end
  for i=385:432
  target(i,:)=[0 0 1 0 0 0 0 0 0 0 0];
  end
  for i=433:480
  target(i,:)=[0 1 0 0 0 0 0 0 0 0 0];
  end
  for i=481:528
  target(i,:)=[1 0 0 0 0 0 0 0 0 0 0];
  end
  E=[];T=target';%mubiaoxiangliang    %11 528
  [m,n]=size(ceshiyangben);[s2,s1]=size(T); 
  for i=2:n;
   for j=1:m
   ceshiyangben(j,i)=ceshiyangben(j,i)/max(ceshiyangben(:,i));
   end
  end %样本归一化
   q=q1(aa);
   Wij=0.2*rands(q,10); Wki=0.2*rands(11,q); %
   b1=0.1*rands(q,1);b2=0.1*rands(11,1);%—随机给定隐含层、输出层偏值 
   wwji=[b1 Wij];
   wwkj=[b2 Wki];
   P=ceshiyangben';
   count1=zeros(1,11);
   count2=zeros(1,11);
  for kk=1:max_epoch
     
     netj=wwji*P;%训练样本
     oj=logsig(netj);%13 528
     oj1=[-ones(528,1) oj']; %528 14
     netk=wwkj*oj1';      %11 528
     oo=logsig(netk);%11-528
     errr=T-oo;   
     sse=mse(errr);
     deltao=errr.*oo.*(ones(11,528)-oo);%11-528
     deltak=(Wki'*deltao).*oj.*(ones(528,(q))-oj')';%13-528

     
     wwji=wwji+Ir*(deltak*P');%权值调整
     wwkj=wwkj+Ir*(deltao*oj1);  
   
     E=[E,sse];
  end
  res(aa)=norm(errr)
  EE(aa,:)=E;
  %样本测试    
     netjj=wwji*P;%输入样本
     ojj=logsig(netjj);%13 528
     ojj1=[-ones(528,1) oj']; % 528 14
     netkk=wwkj*ojj1';     %11 528
     ooo=logsig(netkk);%11-528
     for j=1:s2    % 1:11
        for i=1:528
       yy(j,i)=round(oo(j,i));
        end
     end
 for h=1:528         %分类器判对数目叠加
   if yy(:,h)==T(:,h)
      if h<=48
      result(1,h)=1;count1(1,1)=count1(1,1)+1;
      elseif h<=96&h>48
      result(1,h)=2;count1(1,2)=count1(1,2)+1;
      elseif h<=144&h>96
      result(1,h)=3;count1(1,3)=count1(1,3)+1;
      elseif h<=192&h>144
      result(1,h)=4;count1(1,4)=count1(1,4)+1;
      elseif h<=240&h>192
      result(1,h)=5;count1(1,5)=count1(1,5)+1;
      elseif h<=288&h>240
      result(1,h)=6;count1(1,6)=count1(1,6)+1;
      elseif h<=336&h>288
      result(1,h)=7;count1(1,7)=count1(1,7)+1;
      elseif h<=384&h>336
      result(1,h)=8;count1(1,8)=count1(1,8)+1;
      elseif h<=432&h>384
      result(1,h)=9;count1(1,9)=count1(1,9)+1;
      elseif h<=480&h>432
      result(1,h)=10;count1(1,10)=count1(1,10)+1;
      elseif h<=528&h>480
      result(1,h)=11;count1(1,11)=count1(1,11)+1;
     end
    else
    result(1,h)=0;   
   end
  end
    Rresult(aa,:)=result;

  for hh=1:528     %判断结果的数目
    if yy(:,hh)==[0 0 0 0 0 0 0 0 0 0 1]'
        count2(1,1)=count2(1,1)+1;
    elseif  yy(:,hh)==[0 0 0 0 0 0 0 0 0 1 0]' 
        count2(1,2)=count2(1,2)+1;
    elseif  yy(:,hh)==[0 0 0 0 0 0 0 0 1 0 0]' 
       count2(1,3)=count2(1,3)+1; 
    elseif  yy(:,hh)==[0 0 0 0 0 0 0 1 0 0 0]' 
       count2(1,4)=count2(1,4)+1; 
    elseif  yy(:,hh)==[0 0 0 0 0 0 1 0 0 0 0]' 
       count2(1,5)=count2(1,5)+1; 
    elseif  yy(:,hh)==[0 0 0 0 0 1 0 0 0 0 0]' 
       count2(1,6)=count2(1,6)+1; 
    elseif  yy(:,hh)==[0 0 0 0 1 0 0 0 0 0 0]' 
       count2(1,7)=count2(1,7)+1; 
    elseif  yy(:,hh)==[0 0 0 1 0 0 0 0 0 0 0]' 
       count2(1,8)=count2(1,8)+1; 
    elseif  yy(:,hh)==[0 0 1 0 0 0 0 0 0 0 0]' 
       count2(1,9)=count2(1,9)+1; 
     elseif  yy(:,hh)==[0 1 0 0 0 0 0 0 0 0 0]' 
       count2(1,10)=count2(1,10)+1; 
     elseif  yy(:,hh)==[1 0 0 0 0 0 0 0 0 0 0]' 
       count2(1,11)=count2(1,11)+1;  
    end
  end
  Re(aa,:)=count1./[48 48 48 48 48 48 48 48 48 48 48];
  Pr(aa,:)=count1./count2;
end
  for dd=1:length(q1)
    plot(EE(dd,:))
    hold on
  end
 number=find(res==min(res));
if(length(number)>1) q=number(1)
else q=number
end%选取最优神经元数,number为使得误差最小的隐层神经元个
q
time=toc
Re(q-3,:)
Pr(q-3,:)
figure
plot(Rresult(q-3,:))




  
  
      

⌨️ 快捷键说明

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