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

📄 ex_2.m

📁 是1-n-1和2-n-1型的基于MATLAB的标准BP算法程序
💻 M
字号:
% function tr_err=EX_2 %2-n-1
clear;clc; close all;
n=10;
A=0.03; %MIU
B=0.01; %YITA
ERR=0.02; err1=1;
EPO=500; %epoch
fh='s-shape'; %hidden layer function
fo='s'; %output layer function
    tr_err_min=100; tr_w1=[]; tr_w2=[]; tr_th=[]; tr_to=[]; tr_epo=0; tr_y=[]; tr_yy=[];
    te_err_min=100; te_w1=[]; te_w2=[]; te_th=[]; te_to=[]; te_epo=0; te_yy=[];
% GUIYI=0.1; %nomalized exponent
% x1=-1:0.1:1; x2=-1:0.1:1; %9*9
% % x1=[-1:0.2:-0.2 -0.16:0.04:0.16 0.2:0.2:1]; %29*29
% % x2=[-1:0.2:-0.2 -0.16:0.04:0.16 0.2:0.2:1];
% x1=[-1, (-0.3*pi):(pi/40):(0.3*pi), 1]; x2=x1;
x1=[-1, (-0.3*pi):(pi/20):(0.3*pi),1]; x2=x1;
% x1=-pi:pi/4:pi; x2=x1;
input_x1=length(x1); input_x2=length(x2);
[X1,X2]=meshgrid(10.*x1,10.*x2);
% [X1,X2]=meshgrid(x1,x2);
y=(sin(X1)./X1).*(sin(X2)./X2);
for i=1:input_x1
    for j=1:input_x2
        if (x1(i)==0)&&(x2(j)==0)
            y(j,i)=1;
        elseif x1(i)==0
            y(j,i)=sin(10*x2(j))/10/x2(j);
%             y(j,i)=sin(x2(j))/x2(j);
        elseif x2(j)==0
            y(j,i)=sin(10*x1(i))/10/x1(i);
%             y(j,i)=sin(x1(i))/x1(i);
        end
    end
end
ry=[];
figure(1);surf(X1,X2,y)
% -----------------
% xx1=-1:0.05:1; xx2=-1:0.05:1; %41*41
xx1=[-1, (-0.3*pi):(pi/100):(0.3*pi), 1]; xx2=xx1;
[XX1,XX2]=meshgrid(10.*xx1,10.*xx2);
% xx1=-pi:pi/10:pi; xx2=xx1;
% [XX1,XX2]=meshgrid(xx1,xx2);
input_xx1=length(xx1); input_xx2=length(xx2);
yy=(sin(XX1)./XX1).*(sin(XX2)./XX2);
for i=1:input_xx1
    for j=1:input_xx2
        if (xx1(i)==0)&&(xx2(j)==0)
            yy(j,i)=1;
        elseif xx1(i)==0
            yy(j,i)=sin(10*xx2(j))/10/xx2(j);
%             yy(j,i)=sin(xx2(j))/xx2(j);
        elseif xx2(j)==0
            yy(j,i)=sin(10*xx1(i))/10/xx1(i);
%             yy(j,i)=sin(xx1(i))/xx1(i);
        end
    end
end
ryy=[];
figure(2);surf(XX1,XX2,yy)
% -----------------
lm=0.7^2*sqrt(n);
w1=rand(2,n)-0.5; w2=rand(1,n)-0.5;
w1(1,:)=lm.*w1(1,:)./sqrt(w1(1,:)*w1(1,:)');
w1(2,:)=lm.*w1(2,:)./sqrt(w1(2,:)*w1(2,:)'); ow1=w1;
w1, w2=lm.*w2./sqrt(w2*w2'), ow2=w2;
th=zeros(n,1); oth=th;
to=0; oto=to;
% -----------------
epo=1; %epoch
tr_err=[]; te_err=[];
while epo<=EPO
    for i=1:input_x1
        for j=1:input_x2
            xh=fun((w1'*[x1(i);x2(j)]+th),0,fh); %+/-
            xo=fun((w2*xh+to),0,fo); %+/-
            %=============
            temp1=fun((w2*xh+to),1,fo); %+/-
            eo=temp1*(y(j,i)-xo);
            temp2=fun((w1'*[x1(i);x2(j)]+th),1,fh); %+/-
            eh=temp2.*(eo*w2');
            %=============
            nw2=w2+A*eo*xh'+B*(w2-ow2);
            nw1=w1+A*(eh*[x1(i),x2(j)])'+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
    end
    %=============
    temp1=0;
    for i=1:input_x1
        for j=1:input_x2
            xh=fun((w1'*[x1(i);x2(j)]+th),0,fh); %+/-
            xo=fun((w2*xh+to),0,fo); %+/-
            %=============
            ry(j,i)=xo;
            %=============
            eo=y(j,i)-xo;
            temp1=temp1+0.5*eo^2;
        end
    end
    err1=temp1;
    tr_err=[tr_err err1];
    % -----------------
    temp2=0;
    for i=1:input_xx1 %test
        for j=1:input_xx2
            xxh=fun((w1'*[xx1(i);xx2(j)]+th),0,fh); %+/-
            xxo=fun((w2*xxh+to),0,fo); %+/-
            %=============
            ryy(j,i)=xxo;
            %=============
            te_eo=yy(j,i)-xxo;
            temp2=temp2+0.5*te_eo^2;
        end
    end
    err2=temp2;
    te_err=[te_err err2];
    % -----------------
    if err1<tr_err_min
        tr_err_min=err1;
        tr_w1=w1; tr_w2=w2; tr_th=th; tr_to=to; tr_y=ry; tr_yy=ryy; tr_epo=epo;
    end
    %=============
    if err2<te_err_min
        te_err_min=err2;
        te_w1=w1; te_w2=w2; te_th=th; te_to=to; te_y=ryy; te_epo=epo;
    end
    % -----------------
    if err1<=ERR
        EPO=epo;
    end
    epo=epo+1; %figure(5); hold on;plot(epo,1);hold off
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,'r'); ylabel('test error');
% -----------------
figure(4)
% rxx1=-10:0.5:10; rxx2=-10:0.5:10;
rxx1=xx1; rxx2=rxx1;
[RXX1,RXX2]=meshgrid(rxx1,rxx2);
% [tr_err_min, rEPO]=min(tr_err);
% ryy_temp=squeeze(ryy(rEPO,:,:));
subplot(2,2,1); surf(RXX1,RXX2,tr_yy);
subplot(2,2,2); surf(RXX1,RXX2,yy);
subplot('position',[0.1,0.05,0.8,0.45]); 
    surf(RXX1,RXX2, (yy-tr_yy));
% -----------------
train_error_Min=tr_err(tr_epo)
train_epoch=tr_epo
test_error_ForTrainErrorMin=te_err(tr_epo)
test_error_Min=te_err(te_epo)
test_epoch=te_epo

⌨️ 快捷键说明

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