⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 三分类bp程序.m

📁 这是一个用MATLAB语言编写的三分类BP程序,绘制了误差曲线
💻 M
字号:
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('Output 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,'r+')
            class=[0 1 0]';
        elseif((y<-0.5)&(y>-1.5))==1
            plot(x,y,'rs')
            class=[0 0 1]';
        else
            plot(x,y,'ro')
            class=[1 0 0]';
        end
    else 
        plot(x,y,'ro')
        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(:,HiddenUnitNum+1);
    %绘制误差曲线
    figure
    hold on
    grid
    [xx,Num]=size(ErrHistory);
    plot(1:Num,ErrHistory,'r-');
    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-2)<0.1;
      targetc3flag=abs(testtargetout-3)<0.1;
      testc1num=sum(nnc1flag)
       testc2num=sum(nnc2flag)
     testc3num=sum(nnc3flag)
     testc1c1=1.0*nnc1flag*targetc1flag'
     testc1c2=1.0*nnc1flag*targetc2flag'
     testc1c3=1.0*nnc1flag*targetc3flag'
     testc2c1=1.0*nnc2flag*targetc1flag'
     testc2c2=1.0*nnc2flag*targetc2flag'
     testc2c3=1.0*nnc2flag*targetc3flag'
     testc3c1=1.0*nnc3flag*targetc1flag'
     testc3c2=1.0*nnc3flag*targetc2flag'
     testc3c3=1.0*nnc3flag*targetc3flag'
     testcorrect=(testc1c1+testc2c2+testc3c3)/testsamnum

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -