clc
clear
tic
SamNum=30;
HiddenNum=7;
InDim=1;
OutDim=1;
load train_x
load train_f
a=train_x';
d=train_f';
p=[a];
t=[d];
[SamIn,minp,maxp,tn,mint,maxt]=premnmx(p,t);
NoiseVar=0.01;
Noise=NoiseVar*randn(1,SamNum);
SamOut=tn + Noise;
SamIn=SamIn';
SamOut=SamOut';
MaxEpochs=60000;
lr=0.025;
E0=0.65*10^(-6);
%%
%the begin of PSO
E0=0.001;
Max_num=500;
particlesize=200;
c1=1;
c2=1;
w=2;
vc=2;
vmax=5;
dims=InDim*HiddenNum+HiddenNum+HiddenNum*OutDim+OutDim;
x=-4+7*rand(particlesize,dims);
v=-4+5*rand(particlesize,dims);
f=zeros(particlesize,1);
%%
for jjj=1:particlesize
trans_x=x(jjj,:);
W1=zeros(InDim,HiddenNum);
B1=zeros(HiddenNum,1);
W2=zeros(HiddenNum,OutDim);
B2=zeros(OutDim,1);
W1=trans_x(1,1:HiddenNum);
B1=trans_x(1,HiddenNum+1:2*HiddenNum)';
W2=trans_x(1,2*HiddenNum+1:3*HiddenNum)';
B2=trans_x(1,3*HiddenNum+1);
Hiddenout=logsig(SamIn*W1+repmat(B1',SamNum,1));
Networkout=Hiddenout*W2+repmat(B2',SamNum,1);
Error=Networkout-SamOut;
SSE=sumsqr(Error)
f(jjj)=SSE;
end
personalbest_x=x;
personalbest_f=f;
[groupbest_f i]=min(personalbest_f);
groupbest_x=x(i,:);
for j_Num=1:Max_num
vc=(5/3*Max_num-j_Num)/Max_num;
%%
v=w*v+c1*rand*(personalbest_x-x)+c2*rand*(repmat(groupbest_x,particlesize,1)-x);
for kk=1:particlesize
for kk0=1:dims
if v(kk,kk0)>vmax
v(kk,kk0)=vmax;
else if v(kk,kk0)<-vmax
v(kk,kk0)=-vmax;
end
end
end
end
x=x+vc*v;
%%
for jjj=1:particlesize
trans_x=x(jjj,:);
W1=zeros(InDim,HiddenNum);
B1=zeros(HiddenNum,1);
W2=zeros(HiddenNum,OutDim);
B2=zeros(OutDim,1);
W1=trans_x(1,1:HiddenNum);
B1=trans_x(1,HiddenNum+1:2*HiddenNum)';
W2=trans_x(1,2*HiddenNum+1:3*HiddenNum)';
B2=trans_x(1,3*HiddenNum+1);
Hiddenout=logsig(SamIn*W1+repmat(B1',SamNum,1));
Networkout=Hiddenout*W2+repmat(B2',SamNum,1);
Error=Networkout-SamOut;
SSE=sumsqr(Error);
f(jjj)=SSE;
end
%%
for kk=1:particlesize
if f(kk)<personalbest_f(kk)
personalbest_f(kk)=f(kk);
personalbest_x(kk)=x(kk);
end
end
[groupbest_f0 i]=min(personalbest_f);
if groupbest_f0<groupbest_f
groupbest_x=x(i,:);
groupbest_f=groupbest_f0;
end
ddd(j_Num)=groupbest_f
end
str=num2str(groupbest_f);
trans_x=groupbest_x;
W1=trans_x(1,1:HiddenNum);
B1=trans_x(1,HiddenNum+1:2*HiddenNum)';
W2=trans_x(1,2*HiddenNum+1:3*HiddenNum)';
B2=trans_x(1,3*HiddenNum+1);
%the end of PSO
%%
for i=1:MaxEpochs
%%
Hiddenout=logsig(SamIn*W1+repmat(B1',SamNum,1));
Networkout=Hiddenout*W2+repmat(B2',SamNum,1);
Error=Networkout-SamOut;
SSE=sumsqr(Error)
ErrHistory=[ SSE];
if SSE<E0,break, end
dB2=zeros(OutDim,1);
dW2=zeros(HiddenNum,OutDim);
for jj=1:HiddenNum
for k=1:SamNum
dW2(jj,OutDim)=dW2(jj,OutDim)+Error(k)*Hiddenout(k,jj);
end
end
for k=1:SamNum
dB2(OutDim,1)=dB2(OutDim,1)+Error(k);
end
dW1=zeros(InDim,HiddenNum);
dB1=zeros(HiddenNum,1);
for ii=1:InDim
for jj=1:HiddenNum
for k=1:SamNum
dW1(ii,jj)=dW1(ii,jj)+Error(k)*W2(jj,OutDim)*Hiddenout(k,jj)*(1-Hiddenout(k,jj))*(SamIn(k,ii));
dB1(jj,1)=dB1(jj,1)+Error(k)*W2(jj,OutDim)*Hiddenout(k,jj)*(1-Hiddenout(k,jj));
end
end
end
W2=W2-lr*dW2;
B2=B2-lr*dB2;
W1=W1-lr*dW1;
B1=B1-lr*dB1;
end
Hiddenout=logsig(SamIn*W1+repmat(B1',SamNum,1));
Networkout=Hiddenout*W2+repmat(B2',SamNum,1);
aa=postmnmx(Networkout,mint,maxt);
x=a;
newk=aa;
figure
plot(x,d,'r-o',x,newk,'b--+')
legend('原始数据','训练后的数据');
xlabel('x');ylabel('y');
toc