RBF源程序
clear,clc;SamNum = 100; % 训练样本数
%TargetSamNum = length(TargetIn); % 测试样本数
InDim = 1; % 样本输入维数
UnitNum = 10; % 隐节点数
MaxEpoch = 2500; % 最大训练次数
E0 = 0.9; %目标误差
% 根据目标函数获得样本输入输出
rand('state',sum(100*clock))
NoiseVar = 0.1;
Noise = NoiseVar*randn(1,SamNum);
SamIn = 8*rand(1,SamNum)-4;
SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);
SamOut = SamOutNoNoise + Noise;
TargetIn = -4:0.08:4;
TargetOut = 1.1*(1-TargetIn+2*TargetIn.^2).*exp(-TargetIn.^2/2);
TargetSamNum = length(TargetIn); % 测试样本数
figure
hold on
grid
plot(SamIn,SamOut,'b+')
plot(TargetIn,TargetOut,'r--')
xlabel('Input x');
ylabel('Output y');
Center = 8*rand(InDim,UnitNum)-4; %Data Center
SP = 0.2*rand(1,UnitNum)+0.1; %Spread Constant
W = 0.2*rand(1,UnitNum)-0.1; %Weigh Matrix
lrCent = 0.001; % 隐节点数据中心学习系数
lrSP = 0.001; % 隐节点扩展常数学习系数
lrW = 0.001; % 隐节点输出权值学习系数
ErrHistory = []; % 用于记录每次参数调整后的训练误差
for epoch = 1:MaxEpoch
AllDist = dist(Center',SamIn);
SPMat = repmat(SP',1,SamNum);
UnitOut = radbas(AllDist./SPMat);
NetOut = W*UnitOut;
Error = SamOut-NetOut;
%停止学习判断
SSE = sumsqr(Error);
% 记录每次权值调整后的训练误差
ErrHistory = ;
if SSE<E0, break, end
for i = 1:UnitNum
CentGrad = (SamIn-repmat(Center(:,i),1,SamNum))...
*(Error.*UnitOut(i,:)*W(i)/(SP(i)^2))';
SPGrad = AllDist(i,:).^2*(Error.*UnitOut(i,:)*W(i)/(SP(i)^3))';
WGrad = Error*UnitOut(i,:)';
Center(:,i) = Center(:,i) + lrCent*CentGrad;
SP(i) = SP(i) + lrSP*SPGrad;
W(i) = W(i) + lrW*WGrad;
end
end
% 测试
TestDistance = dist(Center',TargetIn);
TestSpreadsMat = repmat(SP',1,TargetSamNum);
TestHiddenUnitOut = radbas(TestDistance./TestSpreadsMat);
TestNNOut = W*TestHiddenUnitOut;
plot(TargetIn,TestNNOut,'k-');legend('Sample Data','Desired Curve','RBF NN Curve');
% 绘制学习误差曲线
figure
hold on
grid
= size(ErrHistory);
plot(1:Num,ErrHistory,'k-');
[ 本帖最后由 suffer 于 2006-11-20 08:14 编辑 ] 楼主能详细解释一下么?我是新手有点看不懂哦:@Q
回复 2楼 的帖子
该程序是魏海坤编著的《神经网络结构设计的理论与方法》(国防工业出版社)一书后附的程序,你可以参考这本书,里面有详细的解释。谢谢楼主分享。[ 本帖最后由 civilkf 于 2008-7-16 09:01 编辑 ]
rbf
:handshake :
页:
[1]