|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
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> |
|