%D-FNN程序
function [w1,w2,width,rule,e,RMSE]=DFNN(p,t,parameters)
%this is D-FNN training program.
%Input:
% p is the input data,which is r by Q matrix
%t is the output data,which is s2 by q matrix
%q is the No. of sample data
%parameters is a vector which define the predefined parameters
%parameters(1)=kdmax (max of acocommodation criterion)
%parameters(2)=kdmin (min of acocommodation criterion)
%parameters(3)=gama (decay constant)
%parameters(4)=emax (max of error)
%parameters(5)=emin (min of error)
%parameters(6)=beta (convergence constant)
%parameters(7)=width0 (the width of the first units)
%parameters(8)=k (overlap factor of RBF units)
%parameters(9)=km (width updating factor)
%parameters(10)=kerr (significance of a rule)
%Output:
%w1 is the centers of RBF units,which is a u by r matrix
%w2 is the weights,which is s2 by u(1+r) matrix
%width is the widths of RBF units,which is a 1 by u matrix
%Revised 11-3-2006,wushiqian
if nargin<2
error('Not enough input arguments')
end
if size(p,2)~=size(t,2)%注意可能有错
error('The input data are not correct')
end
[r,q]=size(p);[s2,q]=size(t);
%Setting predefined parameters
kdmax=parameters(1);kdmin=parameters(2);gana=parameters(3);
emax=parameters(4);emin=parameters(5);beta=parameters(6);
width0=parameters(7);k=parameters(8);kw=parameters(9);
kerr=parameters(10);
ALLIN=[];ALLOUT=[];CRBF=[];
%when first sample data coming
ALLIN=p(:,1);ALLOUT=t(:,1);
%Setting uo the initial DFNN
CRBF=(:,1);w1=CRBF';width(1)=width0;rule(1)=1;
%Calculata the first out error
a0=RBF(dist(w1,ALLIN),1./width');
a01=[a0 p(:,1)'];%注意可能有错
w2=ALLOUT/a01';
a02=w2*a01';
RMSE(1)=sqrt(sumsqrt(ALLOUT-a02)/s2);
%when other sample data comging
for i=2:q
IN=p(:,i);OUT=t(:,i);
ALLIN=[ALLIN IN];ALLOUT=[ALLOUT OUT];
[r,N]=size(ALLIN);[s r]=size(w1);%注意可能有错
dd=dist(w1,IN);
[d,ind]=min(dd);
kd=max(kdmax*gama.^(i-1),kdmin);
%Calculating the actual output of the sampledata
ai=RBF(dist(w1,IN),1./width');
ai=ai/sum(ai);ai1=transf(ai,IN);
ai2=w2*ai1;
errout=t(:,i)-ai2;errout=sum(errout.*errout)/s2;
e(i)=sqrt(errout);
ke=max(emax*beta.^(i-1),kdmin);
if d>kd
if e(i)>ke
CRBF=[CRBF IN];%Add anew rule
wb=k*d;
width=[width wb];
w1=CRBF';
[u,v]=size(w1);
%Calculate outputs of RBF after growing for all data
A=RBF(dist(w1,ALLIN),1./width');
A0=sum(a);
A1=A./(ones(u,1)*A0);
A2=transf(A1,ALLIN);
if u*(r+1)<=N
%calculate error reduction rate
tT=ALLOUT';
PAT=A2';
[W,AW]=orthogonalize()PAT;
SSW=sum(W.*W)';SStT=SUM(tT.*tT)';
err=((W'*tT)'.^2)./(SStT*SSW');errT=err';
err1=zeros(u,s2*(r+1));
err1(:)=errT;err21=err';
err22=sum(err21.*err21)/(s2*(r+1));
err23=sqrt(err22);
No=find(err23<kerr);
if ~isempty(No)
CRBF(:,No)=[];w1(No,:)=[];
width(:,No)=[];err21(:,No)=[];
[uu,vv]=size(w1);
AA=RBF(dist(w1,ALLIN),1./width');
AA0=sum(AA);
AA1=AA./(ones(uU,1)*AA0);
AA2=transf(AA1,ALLIN);
w2=ALLOUT/AA2;
outAA2=w2*AA2;
sse0=sumsqr(ALLOUT-outAA2)/(i*s2);
RMSE(i)=sqrt(see0);
rule(i)=uu;
w2T=w2';ww2=zeros(uu,s2*(r+1));
ww2(:)=w2T;
w21=ww2';
else
w2=ALLOUT/A2;%w2 is s2*s2u(r+1)
outA2=w2*A2;
sse0=sumsqr(ALLOUT-outA2)/(i*s2);
RMSE(i)=sqrt(see0);
rule(i)=u;
w2T=w2';ww2=zeros(u,s2*(r+1));
ww2(:)=w2T;
w21=ww2';
end
else
w2=ALLOUT/A2;%w2 is s2*s2u(r+1)
outA2=w2*A2;
sse0=sumsqr(ALLOUT-outA2)/(i*s2);
RMSE(i)=sqrt(see0);
rule(i)=u;
w2T=w2';ww2=zeros(u,s2*(r+1));
ww2(:)=w2T;
w21=ww2';
end
else
a=RBF(dist(w1,ALLIN),1./width');
a0=sum(a);
a1=a./(ones(s,1)*a0);
a2=transf(a1,ALLIN);
w2=ALLOUT/a2;
outa2=w2*a2;
sse1=sumsqr(ALLOUT-outa2)/(i*s2);
RMSE(i)=sqrt(see1);
rule(i)=s;
end
else
if e(i)>ke
width(ind)=kw*width(ind);
aa=RBF(dist(w1,ALLIN),1./width');
aa0=sum(aa);
aa1=aa./(ones(s,1)*aa0);
aa2=transf(aa1,ALLIN);
w2=ALLOUT/aa2;
outaa2=w2*aa2;
sse2=sumsqr(ALLOUT-outaa2)/(i*s2);
RMSE(i)=sqrt(see2);
rule(i)=s;
else
aa1=RBF(dist(w1,ALLIN),1./width');
aa01=sum(aa1);
aa11=aa1./(ones(s,1)*aa01);
aa21=transf(aa11,ALLIN);
w2=ALLOUT/aa21;
outaa21=w2*aa21;
sse3=sumsqr(ALLOUT-outaa21)/(i*s2);
RMSE(i)=sqrt(see3);
rule(i)=s;
end
end
end
%orthogonalize 正交化程序
function [w,a]=orthogonalize(p)
%this program is used to transform p into orthogonalized w
%Revised 11-3-2006,wushiqian
[u,v]=size(p);w(:,1)=p(:,1);a=eye(v);
for k=2:v
b=zeros(u,1);
for i=1:k-1
a(i,k)=w(:,i)'*p(:,k)/(w(:,i)'*w(:,i));
b=b+a(i,k)*w(:,i);
end
w(:,k)=p(:,k)-b;
end
%RBF程序
function a=RBF(d,w)
%this program is used to calculate the RBF units
%input:
%d is the distance matrix
%w is the width matrix
%output:
%a is the output of the RBF units
%Revised 11-3-2006,wushiqian
if nargin<2
error('Not enough input arguments')
end
d=d.*w(:,ones(1,size(d,2)));
a=exp(-(d.*d));
%Trans程序
function BB=transf(A,P)
%this program is used to generate a matrice in order to calculate
%consequent parameters
%A ---output of RBF
%P---sample of input
%Revised 11-3-2006,wushiqian
[u,N]=size(A);
[r,N]=size(P);
for j=1:N
for i=1:r
PA((i-1)*u+1:i*u,j)=P(i,j)*A(:,j);
end
end
BB[A;PA];