求助~有关神经网络集成
这个是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=;
TestSamIn=;
end
end
TestSamOut=sign(TestSamIn(1,:).*TestSamIn(2,:));
MaxEpochs=1000;
lr=0.005;
E0=0.01;
tp=;
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);
=BackProp(W1,B1,W2,B2,TrainSamIn,TrainSamOut,tp);
TestNNOut=BPNetwork(NewW1,NewB1,NewW2,NewB2,TestSamIn);
AllTestNNOut=;
GXORDraw(TestSamIn,TestNNOut);
end
=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=BackProp(W1,B1,W2,B2,SamIn,SamOut,tp);
参考附录A
function GXORDraw(X,Y)
=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)
=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],)
line(,)
line([-1 -1],)
line([-1 1],[-0.5 -0.5])
line([-1 1],[-1.5 -1.5])
line(,[-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=';
elseif((y<-0.5)&(y>-1.5))==1
plot(x,y,'ks')
class=';
else
plot(x,y,'ko')
class=';
end
else
plot(x,y,'ko')
class=';
end
SamOut=;
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=;
W2Ex=;
SamInEx=';
ErrHistory=[];
for i=1:MaxEpochs
HiddenOut=logsig(W1Ex*SamInEx);
HiddenOutEx=';
NetworkOut=logsig(W2Ex*HiddenOutEx);
Error=SamOut-NetworkOut;
SSE=sumsqr(Error)
ErrHistory=;
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
=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));
=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=;
elseif((y<-0.5)&(y>-1.5))==1
TestTargetOut=;
else
TestTargetOut=;
end
else
TestTargetOut=;
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
很急帮我下 我是初学者~
页:
[1]