学习率自动寻优的共轭梯度bp神经网络 matlab源码

clear all;

clc;

close all;

 

SamNum=200;%样本数

TestSamNum=100;%测试样本

HiddenUnit1Num=10;%隐层1结点数

InDim=2;%样本输入维数

OutDim=1;%样本输出维数

%根据目标函数获得样本输入输出

SamIn1=8*rand(1,SamNum)-4;%产生样本向量

SamIn2=8*rand(1,SamNum)-4;%产生样本向量

SamIn=[SamIn1;SamIn2];

SamOut=0.25*(2+sin(SamIn1)+cos(SamIn2));

 

TestSamIn1=-4:0.05:4;%测试数据

TestSamIn2=-4:0.05:4;%测试数据

TestSamIn=[TestSamIn1;TestSamIn2];

TestSamOut=0.25*(2+sin(TestSamIn1)+cos(TestSamIn2));%测试数据目标输出

MaxEpochs=2000;%最大训练次数

%lr=0.05;%手动设定时的学习率

E0=0.000001;%目标误差

W1=0.2*rand(HiddenUnit1Num,InDim)-0.1;%随机产生权值

B1=0.2*rand(HiddenUnit1Num,1)-0.1;

W2=0.2*rand(OutDim,HiddenUnit1Num)-0.1;

B2=0.2*rand(OutDim,1)-0.1;

tempg1=zeros(10,2,SamNum,3);%负梯度临时存储位

tempg2=zeros(1,10,SamNum,3);%负梯度临时存储位

tempd1=zeros(10,2,2);%搜索方向临时存储位

tempd2=zeros(1,10,2);%搜索方向临时存储位

tempgb1=zeros(10,1,SamNum,3);%负梯度临时存储位

tempgb2=zeros(1,SamNum,3);%负梯度临时存储位

tempdb1=zeros(10,1,2);%搜索方向临时存储位

tempdb2=zeros(1,2);%搜索方向临时存储位

 

Dw1Ex=zeros(HiddenUnit1Num,InDim,SamNum);

Db1Ex=zeros(HiddenUnit1Num,1,SamNum);

Dw2Ex=zeros(OutDim,HiddenUnit1Num,SamNum);

Db2Ex=zeros(OutDim,SamNum);

%Dw2Ex=zeros(HiddenUnit2Num,HiddenUnit1Num+1);

%Delta2Store=zeros(HiddenUnit2Num,SamNum);

%Delta2Store=zeros(HiddenUnit1Num,SamNum);

for i=1:MaxEpochs

%正向传播时隐层,及网络输出值%

u=W1SamIn+B1ones(1,size(SamIn,2));

Hidden1Out=1./(1+exp(-u));

NetworkOut=W2*Hidden1Out+B2;   %网络输出值

%停止学习判断条件

Error=SamOut-NetworkOut;%是一个1*100的向量

SSE=sum(Error.^2);%所有样本产生的误差之和

if SSE<E0,break,end

%误差曲线

if (mod(i,1)==0)

pause(0.0000001)

plot(i,SSE,‘b.-'),title(‘误差曲线图’);

hold on

grid on

end

&nbsp;

%%

%%计算反向传播误差

&nbsp;

Delta2=Error;%负的Ep对y求偏导= -(y-y^) 这个是1*100的向量,包含每个样本

a=zeros(HiddenUnit1Num,SamNum);

for m=1:HiddenUnit1Num

a(m,:)=Delta2.*Hidden1Out(m,:);%E对W2偏导=Ep对W2偏导数的和=∑(y-y^)*z  求得的是每个样本的导数和∑aEp/aW2

end

Dw2Ex(1,:,:)=a;

Db2Ex=Delta2;

tempg2(:,:,:,1)=-Dw2Ex;%w2导数

tempgd2(:,:,:,1)=-Db2Ex;%b2导数

b=zeros(HiddenUnit1Num,InDim,SamNum);

c=zeros(HiddenUnit1Num,1,SamNum);

for m=1:SamNum %对每一个样本分别计算

Delta1=(W2’Delta2(m)).Hidden1Out(:,m).(1-Hidden1Out(:,m));% Ep对Sk求偏导=aEp/aSk=(aEp/ay)(ay/aSj)(aSj/aZk)(aZk/aSk)=(y-y^)1w2z(1-z),见理论部分文档中公式(1-5)第二行

c(:,:,m)=Delta1;% ,见理论部分文档中公式(1-5)第二行,此处为每个样本的向量,未求和

for n=1:2

b(:,n,m)= Delta1*(SamIn(n,m))';%E对W1偏导=Ep对W1偏导的和=Delta1*X=∑w2*(y-y^)*z*(1-z)*x ,见理论部分文档中公式(1-5)第一行

end

end

Dw1Ex=b;

Db1Ex=c;

&nbsp;

tempg2(:,:,:,1)=-Dw2Ex;%负梯度临时存储位当前值赋值刷新

tempgb2(:,:,1)=-Db2Ex;%负梯度临时存储位当前值赋值刷新

tempg1(:,:,:,1)=-Dw1Ex;%负梯度临时存储位当前值赋值刷新

tempgb1(:,:,:,1)=-Db1Ex;%负梯度临时存储位当前值赋值刷新

%%

%     更新权值

if (i==1) %第一次迭代搜索方向取负梯度方向

tempd1(:,:,1)=-sum(tempg1(:,:,:,1),3);

tempd2(:,:,1)=-sum(tempg2(:,:,:,1),3);

tempdb1(:,:,1)=-sum(tempgb1(:,:,:,1),3);

tempdb2(:,1)=-sum(tempgb2(:,:,1),2);

else

&nbsp;

ga=sum(sum(sum(tempg1(:,:,:,1).^2),2))+sum(sum(sum(tempgb1(:,:,:,1).^2),2))+sum(sum(sum(tempg2(:,:,:,1).^2),2))+sum(sum(tempgb2(:,:,1).^2));

gb=sum(sum(sum(tempg1(:,:,:,2).^2),2))+sum(sum(sum(tempgb1(:,:,:,2).^2),2))+sum(sum(sum(tempg2(:,:,:,2).^2),2))+sum(sum(tempgb2(:,:,2).^2));

beit1=ga/gb;

for n=1:InDim

for m=1:HiddenUnit1Num

tempd1(m,n,1)=-sum(tempg1(m,n,:,1))+beit1*tempd1(m,n,2);%

tempdb1(m,1,1)=-sum(tempgb1(m,1,:,1))+beit1*tempdb1(m,1,2);

tempd2(1,m,1)=-sum(tempg2(1,m,:,1))+beit1*tempd2(1,m,2);

end

end

tempdb2(1,1)=-sum(tempgb2(1,:,1))+beit1*tempdb2(1,2);

end

%%

%学习率自动寻优

dertaElr2=zeros(1,SamNum);

dertaElr=zeros(1,SamNum);

for m=1:SamNum

dertazlr=Hidden1Out(:,m).(1-Hidden1Out(:,m)).(tempd1(:,:,1)*SamIn(:,m)+tempdb1(:,:,1));%隐层输出对lr求导

dertazlr2=(tempd1(:,:,1)SamIn(:,m)+tempdb1(:,:,1)).(dertazlr-2*Hidden1Out(:,m).*dertazlr);%隐层输出对lr求2次导

dertaElr2(m)=(tempd2(:,:,1)Hidden1Out(:,m)+W2dertazlr+tempdb2(:,1))^2-Delta2(m)*sum(tempd2(:,:,1)dertazlr+W2dertazlr2);%E对lr求导在lr=0处的值

dertaElr(m)=-Delta2(m)*(tempd2(:,:,1)Hidden1Out(:,m)+W2dertazlr+tempdb2(:,1));%E对lr求导

end

S=-sum(dertaElr)/sum(dertaElr2);%学习率lr

%更新权值

W1=W1+S*tempd1(:,:,1);%权值W1更新公式

B1=B1+S*tempdb1(:,:,1);

W2=W2+S*tempd2(:,:,1);

B2=B2+S*tempdb2(:,1);

%负梯度做为上一时刻存储到第二位

tempg1(:,:,:,2:end)=tempg1(:,:,:,1:end-1);%%负梯度临时存储位右移刷新

tempg2(:,:,:,2:end)=tempg2(:,:,:,1:end-1);%%负梯度临时存储位右移刷新

tempd1(:,:,2:end)=tempd1(:,:,1:end-1);%%搜索方向临时存储位右移刷新

tempd2(:,:,2:end)=tempd2(:,:,1:end-1);%%搜索方向临时存储位右移刷新

tempgb1(:,:,:,2:end)=tempgb1(:,:,:,1:end-1);%%负梯度临时存储位右移刷新

tempgb2(:,:,2:end)=tempgb2(:,:,1:end-1);%%负梯度临时存储位右移刷新

tempdb1(:,:,2:end)=tempdb1(:,:,1:end-1);%%搜索方向临时存储位右移刷新

tempdb2(:,2:end)=tempdb2(:,1:end-1);%%搜索方向临时存储位右移刷新

end

u=W1TestSamIn+B1ones(1,size(TestSamIn,2));

Hidden1Out=1./(1+exp(-u));

TestNetworkOut=W2Hidden1Out+B2ones(size(TestSamOut));

k=1:SamNum;

figure(2)

plot(k,SamOut,'-r*',k,NetworkOut,'-o');

legend(‘训练样本值’,‘BP拟合值’);

title(‘BP网络训练图’);

xlabel(‘样本个数’);

&nbsp;

figure(3)

plot(TestSamIn1,TestNetworkOut,'-r*',TestSamIn1,TestSamOut,'-o');

legend(‘测试样本值’,‘测试值’);

title(‘BP网络测试图’);

xlabel(‘样本’);

See Also