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

📄 ex_1.m

📁 是1-n-1和2-n-1型的基于MATLAB的标准BP算法程序
💻 M
字号:
% function EX_1 %1-n-1
clear;clc; close all;
n=8; %number of hidden layer's units
fh='s-shape'; %hidden layer function
fo='s'; %output layer function
A=0.3; %MIU
B=0.1; %YITA
ERR=0.02; err1=1;
EPO=1000; %epoch
x=0:(1/8):1; %9
y=sin(2*pi*x);
% -----------------
xx=0:(1/360):1;
yy=sin(2*pi*xx);
% -----------------
lm=0.7^1*sqrt(n);
w1=rand(1,n)-0.5; w2=rand(1,n)-0.5;
w1=lm.*w1/sqrt(w1*w1'), ow1=w1;
w2=lm.*w2/sqrt(w2*w2'), ow2=w2;
th=zeros(n,1); oth=th;
to=0; oto=to;
ty=[];
% -----------------
% w1=[-0.1530  0.8128  0.5713  0.8984  0.3045 -0.9077  0.6826  0.8485]; ow1=w1;
% w2=[ 0.4716  0.6801  0.6415 -0.2844  0.4102 -0.8676  0.5437 -1.2353]; ow2=w2;
% -----------------
train_error_min=100; train_epoch=0; train_yy=[];
train_w1=[]; train_w2=[]; train_th=[]; train_to=[];
test_error_min=100; test_epoch=0; test_yy=[];
test_w1=[]; test_w2=[]; test_th=[]; test_to=[];
epo=1; %epoch
tr_err=[]; te_err=[];
while epo<=EPO
    for i=1:9
            xh=fun((w1'*x(i)+th),0,fh); %+/-
            xo=fun((w2*xh+to),0,fo); %+/-
            %=============
            temp1=fun((w2*xh+to),1,fo); %+/-
            eo=temp1*(y(i)-xo);
            temp2=fun((w1'*x(i)+th),1,fh); %+/-
            eh=temp2.*(eo*w2');
            %=============
            nw2=w2+A*eo*xh'+B*(w2-ow2);
            nw1=w1+A*(eh*x(i))'+B*(w1-ow1);
            nto=to+A*eo+B*(to-oto);
            nth=th+A*eh+B*(th-oth);
            %=============
            ow2=w2;w2=nw2;
            ow1=w1;w1=nw1;
            oto=to;to=nto;
            oth=th;th=nth;
    end
    %=============
    temp1=0;
    for i=1:9
            xh=fun((w1'*x(i)+th),0,fh); %+/-
            xo=fun((w2*xh+to),0,fo); %+/-
            %=============
            eo=y(i)-xo;
            temp1=temp1+0.5*eo^2;
    end
    err1=temp1;
    tr_err=[tr_err err1];
%     figure(1); hold on; subplot(2,1,1); plot(epo,err1,'r'); ylabel('train error'); hold off
    % -----------------
    temp2=0;
    for i=1:361 %test
            xxh=fun((w1'*xx(i)+th),0,fh); %+/-
            xxo=fun((w2*xxh+to),0,fo); %+/-
            %=============
            ty(i)=xxo;
            %=============
            te_eo=yy(i)-xxo;
            temp2=temp2+0.5*te_eo^2;
    end
    err2=temp2;
    te_err=[te_err err2];
%     figure(1); hold on; subplot(2,1,2); plot(epo,err2,'r'); ylabel('test error'); hold off
    % -----------------
    figure(2); plot(2*pi*x,y,'k.');
    tx=0:(pi/180):(2*pi); hold on; plot(tx,ty,'r'); ezplot('y=sin(x)',[0,2*pi,-1,1]); hold off
    % -----------------
    if err1<train_error_min
        train_error_min=err1;
        train_epoch=epo; train_yy=ty;
        train_w1=w1; train_w2=w2;
        train_th=th; train_to=to;
    end
    if err2<test_error_min
        test_error_min=err2;
        test_epoch=epo; test_yy=ty;
        test_w1=w1; test_w2=w2;
        test_th=th; train_to=to;
    end
    % -----------------
    if err1<=ERR
        EPO=epo;
    end
    epo=epo+1;
end
% -----------------
xl=1:EPO;
figure(3)
subplot(2,1,1); plot(xl,tr_err,'r'); ylabel('train error');
subplot(2,1,2); plot(xl,te_err,'k'); ylabel('test error');
% -----------------
figure(4); plot(2*pi*x,y,'k.');
tx=0:(pi/180):(2*pi);
% [tr_err_min, rEPO]=min(tr_err);
% ty_min=squeeze(ty(rEPO,:));
hold on; plot(tx,train_yy,'r'); ezplot('y=sin(x)',[0,2*pi,-1,1]); hold off
% -----------------
train_error_Min=tr_err(train_epoch)
test_error_ForTrainErrorMin=te_err(train_epoch), train_epoch
test_error_Min=te_err(test_epoch), test_epoch

⌨️ 快捷键说明

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