马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
- SamNum = 100; % 训练样本数
- TestSamNum = 101; % 测试样本数
- SP = 0.6; % 隐节点扩展常数
- ErrorLimit = 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;
- TestSamIn = -4:0.08:4;
- TestSamOut = 1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);
- figure
- hold on
- grid
- plot(SamIn,SamOut,'k+')
- plot(TestSamIn,TestSamOut,'k--')
- xlabel('Input x');
- ylabel('Output y');
- [InDim,MaxUnitNum] = size(SamIn); % 样本输入维数和最大允许隐节点数
- % 计算隐节点输出阵
- Distance = dist(SamIn',SamIn);
- HiddenUnitOut = radbas(Distance/SP);
- PosSelected = [];
- VectorsSelected = [];
- HiddenUnitOutSelected = [];
- ErrHistory = []; % 用于记录每次增加隐节点后的训练误差
- VectorsSelectFrom = HiddenUnitOut;
- dd = sum((SamOut.*SamOut)')';
- for k = 1 : MaxUnitNum
- % 计算各隐节点输出矢量与目标输出矢量的夹角平方值
- PP = sum(VectorsSelectFrom.*VectorsSelectFrom)';
- Denominator = dd * PP';
- [xxx,SelectedNum] = size(PosSelected);
- if SelectedNum>0,
- [lin,xxx] = size(Denominator);
- Denominator(:,PosSelected) = ones(lin,1);
- end
- Angle = ((SamOut*VectorsSelectFrom) .^ 2) ./ Denominator;
- % 选择具有最大投影的矢量,得到相应的数据中心
- [value,pos] = max(Angle);
- PosSelected = [PosSelected pos];
- % 计算RBF 网训练误差
- HiddenUnitOutSelected = [HiddenUnitOutSelected; HiddenUnitOut(pos,];
- HiddenUnitOutEx = [HiddenUnitOutSelected; ones(1,SamNum)];
- W2Ex = SamOut*pinv(HiddenUnitOutEx); % 用广义逆求广义输出权值
- W2 = W2Ex(:,1:k); % 得到输出权值
- B2 = W2Ex(:,k+1); % 得到偏移
- NNOut = W2*HiddenUnitOutSelected+B2; % 计算RBF 网输出
- SSE = sumsqr(SamOut-NNOut)
- % 记录每次增加隐节点后的训练误差
- ErrHistory = [ErrHistory SSE];
- if SSE < ErrorLimit, break, end
- % 作Gram-Schmidt 正交化
- NewVector = VectorsSelectFrom(:,pos);
- ProjectionLen = NewVector' * VectorsSelectFrom / (NewVector'*NewVector);
- VectorsSelectFrom = VectorsSelectFrom - NewVector * ProjectionLen;
- end
- UnitCenters = SamIn(PosSelected);%%%%%%%%%%%
- % 测试
- TestDistance = dist(UnitCenters',TestSamIn);%%%%%%%%
- TestHiddenUnitOut = radbas(TestDistance/SP);
- TestNNOut = W2*TestHiddenUnitOut+B2;
- plot(TestSamIn,TestNNOut,'k-')
- k
- UnitCenters
- W2
- B2
复制代码 |