博大广阔 发表于 2011-11-2 21:04

BP算法——带有详细的说明

function aa=BP()

clc
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                   函数输入                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P=-2:0.1:2;
T=1+sin(pi*P/4);

if size(P,2)~=size(T);error('input with ouput of sample not same');end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   BP算法的参数设置                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Lr=0.1;                                             %学习效率
goal=0.001;                                        %期望误差最小值
max_epoch=5;                                        %最大迭代次数
Layer_n=5;                                           %隐含层的神经网络数

%神经网络的调整数:weight=Layer_n * input_n+ output_n * Layer_n
%               bias=Layer_n + output_n
% for a network to be able to generalize ,it should have fewer parameters
% than there are data points in the training set
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
=size(P);=size(T,1);                         %初始化隐含层和输出层的权重系数
weight1=rand(Layer_n,input_n);weight2=rand(output_n,Layer_n);   %为防止等量调整,权重初始值为随机   
bias1=rand(Layer_n,1);bias2=rand(size(output_n,1),1);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%      (二) 神经网络的训练                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


for interation=1:1:max_epoch                           %迭代次数   
    EE=0;
   for K=1:1:size(P,2)
   p=P(:,K);
a1=fun1(weight1*p+bias1);                  %计算隐含层输出
a2=fun2(weight2*a1+bias2);                        %计算输出层输出
E=T(:,K)-a2;                                       %误差计算

=stepdest(E,weight1 ,bias1 ,weight2, bias2,a1,p,Lr);





EE=EE+E.'*E;
   end
   
if EE<goal; break; end
Er(interation,:)=EE; TT(interation,:)=interation;         %误差保存
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%               (三) 神经网络的计算                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for K=1:1:size(P,2)
p=P(:,K);
a1=fun1(weight1*p+bias1);                           %计算隐含层输出
a2=fun2(weight2*a1+bias2);                           %计算输出层输出
T2(:,K)=a2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%               (四)性能对比
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure(1)
plot(TT,Er,'r'); grid on;                        %画误差图像
figure(2)
plot(P,T,'b',P,T2,'r');                        %画给的函数图像和神经网络的图像
end



function =stepdest(E,weight1 ,bias1, weight2, bias2,a1,p,Lr)
%标准梯度算法
Sout=-2*dfun2(weight2*a1+bias2).*E;                %输出层的灵敏度计算
S1=dfun1(weight1*p+bias1).*(weight2.'*Sout);       %第一层灵敏度的计算
   weight2=weight2-Lr*Sout*a1.';                           %更新权重
   bias2=bias2-Lr*Sout;   
weight1=weight1-Lr*S1*p.';                        %权重更新
    bias1=bias1-Lr*S1;
end
























%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                  (五) 神经元的传递函数   
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function F=fun1(X)
%Hyperbolic Tangent sigmoid
%F=(exp(X)-exp(-X))./(exp(X)+exp(-X));
%logsid
F=1./(1+exp(-X));
end

function F=dfun1(X)
%Tangent sigmoid 导数
%F=(4*exp(X).*exp(-X))./(exp(X)+exp(-X)).^2;
%dlogsig
F=exp(X)./(exp(X) + 1).^2;
end


function F=fun2(X)
F=X;
end

function F=dfun2(X)
F=ones(size(X));
end













S11021023 发表于 2011-11-4 21:23

谢了。。。。。。。。。。。。。
页: [1]
查看完整版本: BP算法——带有详细的说明