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

📄 nnexamples.m

📁 神经网络的经典算例
💻 M
字号:
function nnexamples(selection)

if nargin==0
    selection = 14;
end

switch selection
case 1
    nnex1('incbp') % linear curve fit
case 2
    nnex2('L', 1) % linear curve fit demonstrating use of bias
case 3
    nnex1('batbp') % linear curve fit using batch back propagation
case 4
    nnex1('marq') % linear curve fit using LevMar algorithm
case 5
    nnex3('batbp', 1, ['H';'L']) % non linear curve fit using batch back propagation, with sine noise
case 6
    nnex3('marq', 1, ['H';'L']) % non linear curve fit using Lev Mar, with sine noise
case 7
    nnex3('marq', 1, ['HHHHHHH';'L------']) % non linear curve fit, overfitting to sine noise
case 8
    nnex2('H', 1) % linear curve fit with wrong type of output neuron and missing bias
case 9
    nnex4(10000) % poorly scaled data
case 10
    nnex4(1) % well scaled data
case 11
    nnex5(0, ['HHHHHHHHHHHHH';'L------------'], 2000) % overfitting without cross validation
case 12
    nnex5(1, ['HHHHHHHHHHHHH';'L------------'], 2000) % training and testing with cross validation
case 13
    nnex5(1, ['HHHHHHHHHHHHH';'L------------'], 10) % avoid overfitting by limiting training iterations
case 14
    nnex5(1, ['HHH';'L--'], 10) % avoid overfitting by simplifying model
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
function nnex5(crossvalidate, NetDef, maxiter)

x1 = 2*(0:0.1:1)-1;
x2 = x1+0.03;
x3 = -1:0.01:1;
d1 = 1*sin(x1*5)+0.9*randn(size(x1));
d2 = 1*sin(x2*5)+0.9*randn(size(x2));
d3 = 1*sin(x3*5)+0.9*randn(size(x3));

if crossvalidate
    x=[x1 x2];
    d=[d1 d2];
    rnd=rand(size(x));
    trFrac=0.5;
    trIdx=find(rnd<trFrac);
    teIdx=find(rnd>=trFrac);
    xTr = x(trIdx);
    dTr = d(trIdx);
    xTe = x(teIdx);
    dTe = d(teIdx);
else
    xTr = [x1 x2];
    dTr = [d1 d2];
    xTe = xTr;
    dTe = dTr;
end
    
params=settrain;
params=settrain(params,'maxiter',maxiter);

w1=[];
w2=[];

% train and test on first set
[w1,w2,critvec,it]=marq(NetDef,w1,w2,xTr,dTr,params);
[yTr,ETr,NSSETr]=nneval(NetDef,w1,w2,xTr,dTr,1);
[yTe,ETe,NSSETe]=nneval(NetDef,w1,w2,xTe,dTe,1);
[y3,E3,NSSE3]=nneval(NetDef,w1,w2,x3,d3,1);

% plot results
figure(3)
clf
subplot(2,1,1)
plot(xTr,yTr,'+',xTe,yTe,'x',xTe,dTe,'.')
legend('Train Output','Test Output','Desired')
title(['Train Error = ' num2str(NSSETr,2) ' Test Error = ' num2str(NSSETe,2)])
pause
subplot(2,1,2)
plot(x3,y3,'x',x3,d3,'.')
legend('Ouptut 3','Desired')
title(['Error 3 = ' num2str(NSSE3,2)])


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
function nnex4(offset)

x = 2*rand([1 100])-1;
d = x * 3 + offset;

params=settrain;
params=settrain(params,'maxiter',10);
NetDef=['L';'L'];

W=[];

w1=randn([1 2]);
w2=randn([1 2]);

for iter=50:-1:1
[w1,w2,critvec,it]=incbp(NetDef,w1,w2,x,d,params);
[y,E,NSSE]=nneval(NetDef,w1,w2,x,d,iter==1);
W=[W;w1 w2];

figure(3)
subplot(2,1,1)
plot(x,y,'x',x,d,'o')
subplot(2,1,2)
plot(W)
pause(0.1)

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
function nnex3(method, noise, NetDef)

x = 2*rand([1 100])-1;
d = (x > 0.5)*2 - 1 + noise*sin(x*50);

if strcmp(method,'marq')==0
    maxiter=1;
else
    maxiter=50;
end
params=settrain;
params=settrain(params,'maxiter',maxiter);


W=[];

w1=[];
w2=[];

for iter=50:-1:1
[w1,w2,critvec,it]=feval(method,NetDef,w1,w2,x,d,params);
[y,E,NSSE]=nneval(NetDef,w1,w2,x,d,iter==1);
W=[W;w1(:)' w2(:)'];

figure(3)
subplot(2,1,1)
plot(x,y,'x',x,d,'o')
subplot(2,1,2)
plot(W)
pause(0.1)
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
function nnex2(OutputNeuron, nobias)

x = 2*rand([1 100])-1;
d = x * 3 + 1;

params=settrain;
params=settrain(params,'maxiter',10);
NetDef=['L';OutputNeuron];

W=[];

w1=randn([1 2]);
w2=randn([1 2]);

if nobias
    w1(2) = 0; % zero out bias
    w2(2) = 0; % zero out bias
end

for iter=50:-1:1
[w1,w2,critvec,it]=incbp(NetDef,w1,w2,x,d,params);
[y,E,NSSE]=nneval(NetDef,w1,w2,x,d,iter==1);
if nobias
    w1(2) = 0; % zero out bias
    w2(2) = 0; % zero out bias
end
W=[W;w1 w2];

figure(3)
subplot(2,1,1)
plot(x,y,'x',x,d,'o')
subplot(2,1,2)
plot(W)
pause(0.1)
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%555
function nnex1(method)

x = 2*rand([1 100])-1;
d = x * 3 + 1;

params=settrain;
params=settrain(params,'maxiter',10);
NetDef=['L';'L'];

W=[];

w1=randn([1 2]);
w2=randn([1 2]);

for iter=50:-1:1
[w1,w2,critvec,it]=feval(method,NetDef,w1,w2,x,d,params);
[y,E,NSSE]=nneval(NetDef,w1,w2,x,d,iter==1);
W=[W;w1 w2];

figure(3)
subplot(2,1,1)
plot(x,y,'x',x,d,'o')
subplot(2,1,2)
plot(W)
pause(0.1)

end

⌨️ 快捷键说明

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