声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2339|回复: 1

[经典算法] BP算法——带有详细的说明

[复制链接]
发表于 2011-11-2 21:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[input_n,sample]=size(P);[output_n]=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;                                       %误差计算

[weight1, bias1 ,weight2, bias2]=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 [weight1 bias1 weight2 bias2]=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













回复
分享到:

使用道具 举报

发表于 2011-11-4 21:23 | 显示全部楼层
谢了。。。。。。。。。。。。。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-12-23 10:17 , Processed in 0.087872 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表