声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2237|回复: 6

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

[复制链接]
发表于 2006-11-22 22:49 | 显示全部楼层 |阅读模式

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

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

x
3个径向基网络的matlab源程序  


一维输入,一维输出,逼近效果很好!

1.基于聚类的RBF 网设计算法
  1. SamNum = 100; % 总样本数
  2. TestSamNum = 101; % 测试样本数
  3. InDim = 1; % 样本输入维数
  4. ClusterNum = 10; % 隐节点数,即聚类样本数
  5. Overlap = 1.0; % 隐节点重叠系数

  6. % 根据目标函数获得样本输入输出
  7. rand('state',sum(100*clock))
  8. NoiseVar = 0.1;
  9. Noise = NoiseVar*randn(1,SamNum);
  10. SamIn = 8*rand(1,SamNum)-4;
  11. SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);
  12. SamOut = SamOutNoNoise + Noise;

  13. TestSamIn = -4:0.08:4;
  14. TestSamOut = 1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);

  15. figure
  16. hold on
  17. grid
  18. plot(SamIn,SamOut,'k+')
  19. plot(TestSamIn,TestSamOut,'k--')
  20. xlabel('Input x');
  21. ylabel('Output y');

  22. Centers = SamIn(:,1:ClusterNum);

  23. NumberInClusters = zeros(ClusterNum,1); % 各类中的样本数,初始化为零
  24. IndexInClusters = zeros(ClusterNum,SamNum); % 各类所含样本的索引号
  25. while 1,
  26. NumberInClusters = zeros(ClusterNum,1); % 各类中的样本数,初始化为零
  27. IndexInClusters = zeros(ClusterNum,SamNum); % 各类所含样本的索引号

  28. % 按最小距离原则对所有样本进行分类
  29. for i = 1:SamNum
  30. AllDistance = dist(Centers',SamIn(:,i));
  31. [MinDist,Pos] = min(AllDistance);
  32. NumberInClusters(Pos) = NumberInClusters(Pos) + 1;
  33. IndexInClusters(Pos,NumberInClusters(Pos)) = i;
  34. end
  35. % 保存旧的聚类中心
  36. OldCenters = Centers;

  37. for i = 1:ClusterNum
  38. Index = IndexInClusters(i,1:NumberInClusters(i));
  39. Centers(:,i) = mean(SamIn(:,Index)')';
  40. end
  41. % 判断新旧聚类中心是否一致,是则结束聚类
  42. EqualNum = sum(sum(Centers==OldCenters));
  43. if EqualNum == InDim*ClusterNum,
  44. break,
  45. end
  46. end

  47. % 计算各隐节点的扩展常数(宽度)
  48. AllDistances = dist(Centers',Centers); % 计算隐节点数据中心间的距离(矩阵)
  49. Maximum = max(max(AllDistances)); % 找出其中最大的一个距离
  50. for i = 1:ClusterNum % 将对角线上的0 替换为较大的值
  51. AllDistances(i,i) = Maximum+1;
  52. end
  53. Spreads = Overlap*min(AllDistances)'; % 以隐节点间的最小距离作为扩展常数

  54. % 计算各隐节点的输出权值
  55. Distance = dist(Centers',SamIn); % 计算各样本输入离各数据中心的距离
  56. SpreadsMat = repmat(Spreads,1,SamNum);
  57. HiddenUnitOut = radbas(Distance./SpreadsMat); % 计算隐节点输出阵
  58. HiddenUnitOutEx = [HiddenUnitOut' ones(SamNum,1)]'; % 考虑偏移
  59. W2Ex = SamOut*pinv(HiddenUnitOutEx); % 求广义输出权值
  60. W2 = W2Ex(:,1:ClusterNum); % 输出权值
  61. B2 = W2Ex(:,ClusterNum+1); % 偏移

  62. % 测试
  63. TestDistance = dist(Centers',TestSamIn);
  64. TestSpreadsMat = repmat(Spreads,1,TestSamNum);
  65. TestHiddenUnitOut = radbas(TestDistance./TestSpreadsMat);
  66. TestNNOut = W2*TestHiddenUnitOut+B2;
  67. plot(TestSamIn,TestNNOut,'k-')
  68. W2
  69. B2
复制代码


2.基于梯度法的RBF 网设计算法

  1. SamNum = 100; % 训练样本数
  2. TargetSamNum = 101; % 测试样本数
  3. InDim = 1; % 样本输入维数
  4. UnitNum = 10; % 隐节点数
  5. MaxEpoch = 5000; % 最大训练次数
  6. E0 = 0.9; % 目标误差
  7. % 根据目标函数获得样本输入输出
  8. rand('state',sum(100*clock))
  9. NoiseVar = 0.1;
  10. Noise = NoiseVar*randn(1,SamNum);
  11. SamIn = 8*rand(1,SamNum)-4;
  12. SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);
  13. SamOut = SamOutNoNoise + Noise;
  14. TargetIn = -4:0.08:4;
  15. TargetOut = 1.1*(1-TargetIn+2*TargetIn.^2).*exp(-TargetIn.^2/2);
  16. figure
  17. hold on
  18. grid
  19. plot(SamIn,SamOut,'k+')
  20. plot(TargetIn,TargetOut,'k--')
  21. xlabel('Input x');
  22. ylabel('Output y');
  23. Center = 8*rand(InDim,UnitNum)-4;
  24. SP = 0.2*rand(1,UnitNum)+0.1;
  25. W = 0.2*rand(1,UnitNum)-0.1;
  26. lrCent = 0.001; % 隐节点数据中心学习系数
  27. lrSP = 0.001; % 隐节点扩展常数学习系数
  28. lrW = 0.001; % 隐节点输出权值学习系数
  29. ErrHistory = []; % 用于记录每次参数调整后的训练误差
  30. for epoch = 1:MaxEpoch
  31. AllDist = dist(Center',SamIn);
  32. SPMat = repmat(SP',1,SamNum);
  33. UnitOut = radbas(AllDist./SPMat);
  34. NetOut = W*UnitOut;
  35. Error = SamOut-NetOut;
  36. %停止学习判断
  37. SSE = sumsqr(Error)
  38. % 记录每次权值调整后的训练误差
  39. ErrHistory = [ErrHistory SSE];
  40. if SSE<E0, break, end
  41. for i = 1:UnitNum
  42. CentGrad = (SamIn-repmat(Center(:,i),1,SamNum))...
  43. *(Error.*UnitOut(i,*W(i)/(SP(i)^2))';
  44. SPGrad = AllDist(i,.^2*(Error.*UnitOut(i,*W(i)/(SP(i)^3))';
  45. WGrad = Error*UnitOut(i,';
  46. Center(:,i) = Center(:,i) + lrCent*CentGrad;
  47. SP(i) = SP(i) + lrSP*SPGrad;
  48. W(i) = W(i) + lrW*WGrad;
  49. end
  50. end
  51. % 测试
  52. TestDistance = dist(Center',TargetIn);
  53. TestSpreadsMat = repmat(SP',1,TargetSamNum);
  54. TestHiddenUnitOut = radbas(TestDistance./TestSpreadsMat);
  55. TestNNOut = W*TestHiddenUnitOut;
  56. plot(TargetIn,TestNNOut,'k-')
  57. % 绘制学习误差曲线
  58. figure
  59. hold on
  60. grid
  61. [xx,Num] = size(ErrHistory);
  62. plot(1:Num,ErrHistory,'k-');
复制代码


3.基于OLS 的RBF 网设计算法
  1. SamNum = 100; % 训练样本数
  2. TestSamNum = 101; % 测试样本数
  3. SP = 0.6; % 隐节点扩展常数
  4. ErrorLimit = 0.9; % 目标误差
  5. % 根据目标函数获得样本输入输出
  6. rand('state',sum(100*clock))
  7. NoiseVar = 0.1;
  8. Noise = NoiseVar*randn(1,SamNum);
  9. SamIn = 8*rand(1,SamNum)-4;
  10. SamOutNoNoise = 1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);
  11. SamOut = SamOutNoNoise + Noise;
  12. TestSamIn = -4:0.08:4;
  13. TestSamOut = 1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);
  14. figure
  15. hold on
  16. grid
  17. plot(SamIn,SamOut,'k+')
  18. plot(TestSamIn,TestSamOut,'k--')
  19. xlabel('Input x');
  20. ylabel('Output y');
  21. [InDim,MaxUnitNum] = size(SamIn); % 样本输入维数和最大允许隐节点数
  22. % 计算隐节点输出阵
  23. Distance = dist(SamIn',SamIn);
  24. HiddenUnitOut = radbas(Distance/SP);
  25. PosSelected = [];
  26. VectorsSelected = [];
  27. HiddenUnitOutSelected = [];
  28. ErrHistory = []; % 用于记录每次增加隐节点后的训练误差
  29. VectorsSelectFrom = HiddenUnitOut;
  30. dd = sum((SamOut.*SamOut)')';
  31. for k = 1 : MaxUnitNum
  32. % 计算各隐节点输出矢量与目标输出矢量的夹角平方值
  33. PP = sum(VectorsSelectFrom.*VectorsSelectFrom)';
  34. Denominator = dd * PP';
  35. [xxx,SelectedNum] = size(PosSelected);
  36. if SelectedNum>0,
  37. [lin,xxx] = size(Denominator);
  38. Denominator(:,PosSelected) = ones(lin,1);
  39. end
  40. Angle = ((SamOut*VectorsSelectFrom) .^ 2) ./ Denominator;
  41. % 选择具有最大投影的矢量,得到相应的数据中心
  42. [value,pos] = max(Angle);
  43. PosSelected = [PosSelected pos];
  44. % 计算RBF 网训练误差
  45. HiddenUnitOutSelected = [HiddenUnitOutSelected; HiddenUnitOut(pos,];
  46. HiddenUnitOutEx = [HiddenUnitOutSelected; ones(1,SamNum)];
  47. W2Ex = SamOut*pinv(HiddenUnitOutEx); % 用广义逆求广义输出权值
  48. W2 = W2Ex(:,1:k); % 得到输出权值
  49. B2 = W2Ex(:,k+1); % 得到偏移
  50. NNOut = W2*HiddenUnitOutSelected+B2; % 计算RBF 网输出
  51. SSE = sumsqr(SamOut-NNOut)
  52. % 记录每次增加隐节点后的训练误差
  53. ErrHistory = [ErrHistory SSE];
  54. if SSE < ErrorLimit, break, end
  55. % 作Gram-Schmidt 正交化
  56. NewVector = VectorsSelectFrom(:,pos);
  57. ProjectionLen = NewVector' * VectorsSelectFrom / (NewVector'*NewVector);
  58. VectorsSelectFrom = VectorsSelectFrom - NewVector * ProjectionLen;
  59. end
  60. UnitCenters = SamIn(PosSelected);%%%%%%%%%%%
  61. % 测试
  62. TestDistance = dist(UnitCenters',TestSamIn);%%%%%%%%
  63. TestHiddenUnitOut = radbas(TestDistance/SP);
  64. TestNNOut = W2*TestHiddenUnitOut+B2;
  65. plot(TestSamIn,TestNNOut,'k-')
  66. k
  67. UnitCenters
  68. W2
  69. B2
复制代码

[ 本帖最后由 suffer 于 2006-11-29 09:27 编辑 ]

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2006-11-26 10:17 | 显示全部楼层
有没有RBF预测的程序?
发表于 2006-11-29 15:58 | 显示全部楼层
好东东,谢谢楼主!
发表于 2006-12-11 15:24 | 显示全部楼层
太好了,kiss~~~~~~~
发表于 2006-12-24 20:23 | 显示全部楼层
学习~~
发表于 2008-9-1 21:15 | 显示全部楼层
谢谢了! !
发表于 2013-5-26 22:09 | 显示全部楼层
         
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-11-28 22:45 , Processed in 0.064166 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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