声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1448|回复: 5

[分享]3个径向基网络的matlab源程序

[复制链接]
发表于 2006-3-21 10:24 | 显示全部楼层 |阅读模式

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

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

x
<P>一维输入,一维输出,逼近效果很好!<BR><BR><BR><BR>1.基于聚类的RBF 网设计算法<BR>SamNum = 100; % 总样本数<BR>TestSamNum = 101; % 测试样本数<BR>InDim = 1; % 样本输入维数<BR>ClusterNum = 10; % 隐节点数,即聚类样本数<BR>Overlap = 1.0; % 隐节点重叠系数</P>
<P>% 根据目标函数获得样本输入输出<BR>rand('state',sum(100*clock))<BR>NoiseVar = 0.1;<BR>Noise = NoiseVar*randn(1,SamNum);<BR>SamIn = 8*rand(1,SamNum)-4;<BR>SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);<BR>SamOut = SamOutNoNoise + Noise;</P>
<P>TestSamIn = -4:0.08:4;<BR>TestSamOut = 1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);</P>
<P>figure<BR>hold on<BR>grid<BR>plot(SamIn,SamOut,'k+')<BR>plot(TestSamIn,TestSamOut,'k--')<BR>xlabel('Input x');<BR>ylabel('Output y');</P>
<P>Centers = SamIn(:,1:ClusterNum);</P>
<P>NumberInClusters = zeros(ClusterNum,1); % 各类中的样本数,初始化为零<BR>IndexInClusters = zeros(ClusterNum,SamNum); % 各类所含样本的索引号<BR>while 1,<BR>NumberInClusters = zeros(ClusterNum,1); % 各类中的样本数,初始化为零<BR>IndexInClusters = zeros(ClusterNum,SamNum); % 各类所含样本的索引号</P>
<P>% 按最小距离原则对所有样本进行分类<BR>for i = 1:SamNum<BR>AllDistance = dist(Centers',SamIn(:,i));<BR>[MinDist,Pos] = min(AllDistance);<BR>NumberInClusters(Pos) = NumberInClusters(Pos) + 1;<BR>IndexInClusters(Pos,NumberInClusters(Pos)) = i;<BR>end<BR>% 保存旧的聚类中心<BR>OldCenters = Centers;</P>
<P>for i = 1:ClusterNum<BR>Index = IndexInClusters(i,1:NumberInClusters(i));<BR>Centers(:,i) = mean(SamIn(:,Index)')';<BR>end<BR>% 判断新旧聚类中心是否一致,是则结束聚类<BR>EqualNum = sum(sum(Centers==OldCenters));<BR>if EqualNum == InDim*ClusterNum,<BR>break,<BR>end<BR>end</P>
<P>% 计算各隐节点的扩展常数(宽度)<BR>AllDistances = dist(Centers',Centers); % 计算隐节点数据中心间的距离(矩阵)<BR>Maximum = max(max(AllDistances)); % 找出其中最大的一个距离<BR>for i = 1:ClusterNum % 将对角线上的0 替换为较大的值<BR>AllDistances(i,i) = Maximum+1;<BR>end<BR>Spreads = Overlap*min(AllDistances)'; % 以隐节点间的最小距离作为扩展常数</P>
<P>% 计算各隐节点的输出权值<BR>Distance = dist(Centers',SamIn); % 计算各样本输入离各数据中心的距离<BR>SpreadsMat = repmat(Spreads,1,SamNum);<BR>HiddenUnitOut = radbas(Distance./SpreadsMat); % 计算隐节点输出阵<BR>HiddenUnitOutEx = [HiddenUnitOut' ones(SamNum,1)]'; % 考虑偏移<BR>W2Ex = SamOut*pinv(HiddenUnitOutEx); % 求广义输出权值<BR>W2 = W2Ex(:,1:ClusterNum); % 输出权值<BR>B2 = W2Ex(:,ClusterNum+1); % 偏移</P>
<P>% 测试<BR>TestDistance = dist(Centers',TestSamIn);<BR>TestSpreadsMat = repmat(Spreads,1,TestSamNum);<BR>TestHiddenUnitOut = radbas(TestDistance./TestSpreadsMat);<BR>TestNNOut = W2*TestHiddenUnitOut+B2;<BR>plot(TestSamIn,TestNNOut,'k-')<BR>W2<BR>B2</P>
回复
分享到:

使用道具 举报

 楼主| 发表于 2006-3-21 10:24 | 显示全部楼层

接上

2.基于梯度法的RBF 网设计算法<BR><BR>SamNum = 100; % 训练样本数<BR>TargetSamNum = 101; % 测试样本数<BR>InDim = 1; % 样本输入维数<BR>UnitNum = 10; % 隐节点数<BR>MaxEpoch = 5000; % 最大训练次数<BR>E0 = 0.9; % 目标误差<BR>% 根据目标函数获得样本输入输出<BR>rand('state',sum(100*clock))<BR>NoiseVar = 0.1;<BR>Noise = NoiseVar*randn(1,SamNum);<BR>SamIn = 8*rand(1,SamNum)-4;<BR>SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);<BR>SamOut = SamOutNoNoise + Noise;<BR>TargetIn = -4:0.08:4;<BR>TargetOut = 1.1*(1-TargetIn+2*TargetIn.^2).*exp(-TargetIn.^2/2);<BR>figure<BR>hold on<BR>grid<BR>plot(SamIn,SamOut,'k+')<BR>plot(TargetIn,TargetOut,'k--')<BR>xlabel('Input x');<BR>ylabel('Output y');<BR>Center = 8*rand(InDim,UnitNum)-4;<BR>SP = 0.2*rand(1,UnitNum)+0.1;<BR>W = 0.2*rand(1,UnitNum)-0.1;<BR>lrCent = 0.001; % 隐节点数据中心学习系数<BR>lrSP = 0.001; % 隐节点扩展常数学习系数<BR>lrW = 0.001; % 隐节点输出权值学习系数<BR>ErrHistory = []; % 用于记录每次参数调整后的训练误差<BR>for epoch = 1:MaxEpoch<BR>AllDist = dist(Center',SamIn);<BR>SPMat = repmat(SP',1,SamNum);<BR>UnitOut = radbas(AllDist./SPMat);<BR>NetOut = W*UnitOut;<BR>Error = SamOut-NetOut;<BR>%停止学习判断<BR>SSE = sumsqr(Error)<BR>% 记录每次权值调整后的训练误差<BR>ErrHistory = [ErrHistory SSE];<BR>if SSE&lt;E0, break, end<BR>for i = 1:UnitNum<BR>CentGrad = (SamIn-repmat(Center(:,i),1,SamNum))...<BR>*(Error.*UnitOut(i,:)*W(i)/(SP(i)^2))';<BR>SPGrad = AllDist(i,:).^2*(Error.*UnitOut(i,:)*W(i)/(SP(i)^3))';<BR>WGrad = Error*UnitOut(i,:)';<BR>Center(:,i) = Center(:,i) + lrCent*CentGrad;<BR>SP(i) = SP(i) + lrSP*SPGrad;<BR>W(i) = W(i) + lrW*WGrad;<BR>end<BR>end<BR>% 测试<BR>TestDistance = dist(Center',TargetIn);<BR>TestSpreadsMat = repmat(SP',1,TargetSamNum);<BR>TestHiddenUnitOut = radbas(TestDistance./TestSpreadsMat);<BR>TestNNOut = W*TestHiddenUnitOut;<BR>plot(TargetIn,TestNNOut,'k-')<BR>% 绘制学习误差曲线<BR>figure<BR>hold on<BR>grid<BR>[xx,Num] = size(ErrHistory);<BR>plot(1:Num,ErrHistory,'k-');
 楼主| 发表于 2006-3-21 10:25 | 显示全部楼层

接上

3.基于OLS 的RBF 网设计算法<BR>SamNum = 100; % 训练样本数<BR>TestSamNum = 101; % 测试样本数<BR>SP = 0.6; % 隐节点扩展常数<BR>ErrorLimit = 0.9; % 目标误差<BR>% 根据目标函数获得样本输入输出<BR>rand('state',sum(100*clock))<BR>NoiseVar = 0.1;<BR>Noise = NoiseVar*randn(1,SamNum);<BR>SamIn = 8*rand(1,SamNum)-4;<BR>SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);<BR>SamOut = SamOutNoNoise + Noise;<BR>TestSamIn = -4:0.08:4;<BR>TestSamOut = 1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);<BR>figure<BR>hold on<BR>grid<BR>plot(SamIn,SamOut,'k+')<BR>plot(TestSamIn,TestSamOut,'k--')<BR>xlabel('Input x');<BR>ylabel('Output y');<BR>[InDim,MaxUnitNum] = size(SamIn); % 样本输入维数和最大允许隐节点数<BR>% 计算隐节点输出阵<BR>Distance = dist(SamIn',SamIn);<BR>HiddenUnitOut = radbas(Distance/SP);<BR>PosSelected = [];<BR>VectorsSelected = [];<BR>HiddenUnitOutSelected = [];<BR>ErrHistory = []; % 用于记录每次增加隐节点后的训练误差<BR>VectorsSelectFrom = HiddenUnitOut;<BR>dd = sum((SamOut.*SamOut)')';<BR>for k = 1 : MaxUnitNum<BR>% 计算各隐节点输出矢量与目标输出矢量的夹角平方值<BR>PP = sum(VectorsSelectFrom.*VectorsSelectFrom)';<BR>Denominator = dd * PP';<BR>[xxx,SelectedNum] = size(PosSelected);<BR>if SelectedNum&gt;0,<BR>[lin,xxx] = size(Denominator);<BR>Denominator(:,PosSelected) = ones(lin,1);<BR>end<BR>Angle = ((SamOut*VectorsSelectFrom) .^ 2) ./ Denominator;<BR>% 选择具有最大投影的矢量,得到相应的数据中心<BR>[value,pos] = max(Angle);<BR>PosSelected = [PosSelected pos];<BR>% 计算RBF 网训练误差<BR>HiddenUnitOutSelected = [HiddenUnitOutSelected; HiddenUnitOut(pos,:)];<BR>HiddenUnitOutEx = [HiddenUnitOutSelected; ones(1,SamNum)];<BR>W2Ex = SamOut*pinv(HiddenUnitOutEx); % 用广义逆求广义输出权值<BR>W2 = W2Ex(:,1:k); % 得到输出权值<BR>B2 = W2Ex(:,k+1); % 得到偏移<BR>NNOut = W2*HiddenUnitOutSelected+B2; % 计算RBF 网输出<BR>SSE = sumsqr(SamOut-NNOut)<BR>% 记录每次增加隐节点后的训练误差<BR>ErrHistory = [ErrHistory SSE];<BR>if SSE &lt; ErrorLimit, break, end<BR>% 作Gram-Schmidt 正交化<BR>NewVector = VectorsSelectFrom(:,pos);<BR>ProjectionLen = NewVector' * VectorsSelectFrom / (NewVector'*NewVector);<BR>VectorsSelectFrom = VectorsSelectFrom - NewVector * ProjectionLen;<BR>end<BR>UnitCenters = SamIn(PosSelected);%%%%%%%%%%%<BR>% 测试<BR>TestDistance = dist(UnitCenters',TestSamIn);%%%%%%%%<BR>TestHiddenUnitOut = radbas(TestDistance/SP);<BR>TestNNOut = W2*TestHiddenUnitOut+B2;<BR>plot(TestSamIn,TestNNOut,'k-')<BR>k<BR>UnitCenters<BR>W2<BR>B2
发表于 2006-3-21 14:36 | 显示全部楼层

回复:(mahaiyan)[分享]3个径向基网络的matlab源程序...

不错,赞一个
发表于 2006-3-29 10:14 | 显示全部楼层
<P>问一下,有没有用于图象识别的神经网络的程序啊?<BR>你的这个是用来做甚摸的?控制吗?</P>
发表于 2006-5-18 18:15 | 显示全部楼层

xiexie

xiexie[em04]
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-11-20 11:44 , Processed in 0.076385 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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