马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
这个是1个神经网络集成训练程序~ 在里面好象要调用另一个程序不知道怎么调用 各位指导下谢谢
function main()
SamNum=500;
HiddenUnitNum=8;
EnsembleNum=5;
TrainSamIn=rand(2,SamNum)*2-1;
TrainSamOut=sign(TrainSamIn(1,:).*TrainSamIn(2,:));
TestSamIn=[];
for x1=-1:0.05:1
for x2=-1:0.05:1
x=[x1;x2];
TestSamIn=[TestSamIn x];
end
end
TestSamOut=sign(TestSamIn(1,:).*TestSamIn(2,:));
MaxEpochs=1000;
lr=0.005;
E0=0.01;
tp=[MaxEpochs,lr,E0];
AllTestNNOut=[];
for NetIndex=1:EnsembleNum
W1=0.1*rands(HiddenUnitNum,2);
B1=0.1*rands(HiddenUnitNum,1);
W2=0.1*rands(1,HiddenUnitNum);
B2=0.1*rands(1,1);
[NewW1,NewB1,NewW2,NewB2,ErrHistory]=BackProp(W1,B1,W2,B2,TrainSamIn,TrainSamOut,tp);
TestNNOut=BPNetwork(NewW1,NewB1,NewW2,NewB2,TestSamIn);
AllTestNNOut=[AllTestNNOut;TestNNOut];
GXORDraw(TestSamIn,TestNNOut);
end
[xxx,TestSamNum]=size(TestSamOut);
AllTestSamOut=repmat(TestSamOut,EnsembleNum,1);
SubnetErrMat=abs(sign(AllTestNNOut)-AllTestSamOut)/2;
SubnetErrorRate=sum(SubnetErrMat')/TsetSamNum
EsembleOut=sign(sum(AllTestNNOut));
EsembleErrorRate=sumsqr((TestSamOut-EsembleOut)/2)/TsetSamNum
GXORDraw(TestSamIn,EnsembleOut);
function[NewW1,NewB1,NewW2,NewB2,ErrHistory]=BackProp(W1,B1,W2,B2,SamIn,SamOut,tp);
参考附录A
function GXORDraw(X,Y)
[xxx,TestSamNum]=size(Y);
figure
echo off
axis([-1,1,-1,1])
axis on
grid
xlabel('Input x1');
ylabel('Input x2');
hold on
for i=1:TsetSamNum
x1=X(1,i);
x2=X(2,i);
if(Y(i)>0)
plot(x1,x2,'k*')
end
end
function SamOut=BPNetwork(W1,B1,W2,B2,SamIn)
[xxx,SamNum]=size(SamIn);
RepB1=repmat(B1,1,SamNum);
RepB2=repmat(B2,1,SamNum);
HiddenOut=tansig(W1*SamIn+RepB1);
SamOut=tansig(W2*HiddenOut+RepB2);
附录A程序 :
function main()
InDim=2;
OutDim=3;
figure
colordef(gcf,'white')
echo off
clc
axis([-2,2,-2,2])
axis on
grid
xlabel('Input x');
ylabel('Input y');
line([-1 1],[1 1])
line([1 -1],[1 0])
line([-1 -1],[0 1])
line([-1 1],[-0.5 -0.5])
line([-1 1],[-1.5 -1.5])
line([1 1],[-0.5 -1.5])
line([-1 -1],[-0.5 -1.5])
hold on
SamNum=200;
rand('state',sum(100*clock))
SamIn=(rand(2,SamNum)-0.5)*4;
SamOut=[];
for i=1:SamNum
Sam=SamIn(:,i);
x=Sam(1,1);
y=Sam(2,1);
if((x>-1)&(x<1))==1
if((y>x/2+1/2)&(y<1))==1
plot(x,y,'k+')
class=[0 1 0]';
elseif((y<-0.5)&(y>-1.5))==1
plot(x,y,'ks')
class=[0 0 1]';
else
plot(x,y,'ko')
class=[1 0 0]';
end
else
plot(x,y,'ko')
class=[1 0 0]';
end
SamOut=[SamOut class];
end
HiddenUnitNum=10;
MaxEpochs=10000;
lr=0.1;
E0=0.01;
W1=0.2*rand(HiddenUnitNum,InDim)-0.1;
B1=0.2*rand(HiddenUnitNum,1)-0.1;
W2=0.2*rand(OutDim,HiddenUnitNum)-0.1;
B2=0.2*rand(OutDim,1)-0.1;
W1Ex=[W1 B1];
W2Ex=[W2 B2];
SamInEx=[SamIn' ones(SamNum,1)]';
ErrHistory=[];
for i=1:MaxEpochs
HiddenOut=logsig(W1Ex*SamInEx);
HiddenOutEx=[HiddenOut' ones(SamNum,1)]';
NetworkOut=logsig(W2Ex*HiddenOutEx);
Error=SamOut-NetworkOut;
SSE=sumsqr(Error)
ErrHistory=[ErrHistory SSE];
if SSE<E0,break,end
Delta2=Error.*NetworkOut.*(1-NetworkOut);
Delta1=W2'*Delta2.*HiddenOut.*(1-HiddenOut);
dW2Ex=Delta2*HiddenOutEx';
dW1Ex=Delta1*SamInEx';
W1Ex=W1Ex+lr*dW1Ex;
W2Ex=W2Ex+lr*dW2Ex;
W2=W2Ex(:,1:HiddenUnitNum);
end
W1=W1Ex(:,1:InDim);
B1=W1Ex(:,InDim+1);
W2=W2Ex(:,1:HiddenUnitNum);
B2=W2Ex(:,1+HiddenUnitNum);
figure
hold on
grid
[xx,Num]=size(ErrHistory);
plot(1:Num,ErrHistory,'k-');
TestSamNum=5000;
TestSamIn=(rand(2,TestSamNum)-0.5)*4;
TestHiddenOut=logsig(W1*TestSamIn+repmat(B1,1,TestSamNum));
TestNetworkOut=logsig(W2*TestHiddenOut+repmat(B2,1,TestSamNum));
[Val,NNClass]=max(TestNetworkOut);
TestTargetOut=[];
for i=1:TestSamNum
Sam=TestSamIn(:,i);
x=Sam(1,1);
y=Sam(2,1);
if((x>-1)&(x<1))==1
if((y>x/2+1/2)&(y<1))==1
TestTargetOut=[TestTargetOut 2];
elseif((y<-0.5)&(y>-1.5))==1
TestTargetOut=[TestTargetOut 3];
else
TestTargetOut=[TestTargetOut 1];
end
else
TestTargetOut=[TestTargetOut 1];
end
end
NNC1Flag=abs(NNClass-1)<0.1;
NNC2Flag=abs(NNClass-2)<0.1;
NNC3Flag=abs(NNClass-3)<0.1;
TargetC1flag=abs(TestTargetOut-1)<0.1;
TargetC2flag=abs(TestTargetOut-1)<0.2;
TargetC3flag=abs(TestTargetOut-1)<0.3;
Test_C1_num=sum(NNC1Flag)
Test_C2_num=sum(NNC2Flag)
Test_C3_num=sum(NNC3Flag)
Test_C1_C1=1.0*NNC1Flag*TargetC1flag'
Test_C1_C2=1.0*NNC1Flag*TargetC2flag'
Test_C1_C3=1.0*NNC1Flag*TargetC3flag'
Test_C2_C1=1.0*NNC2Flag*TargetC1flag'
Test_C2_C2=1.0*NNC2Flag*TargetC2flag'
Test_C2_C3=1.0*NNC2Flag*TargetC3flag'
Test_C3_C1=1.0*NNC3Flag*TargetC1flag'
Test_C3_C2=1.0*NNC3Flag*TargetC2flag'
Test_C3_C3=1.0*NNC3Flag*TargetC3flag'
Test_Correct=(Test_C1_C1+Test_C2_C2+Test_C3_C3)/TestSamNum
很急帮我下 我是初学者~ |