%m是自变量因素行数,n是因变量因素行数,o是所要预测用的自变量因素行数,一般m==o
warning off;
format long g;
[filename filepath]=uigetfile('*.xlsx','选择神经网络需要参考训练的样本数据');
xlsx=[filepath filename];
XLSX=xlsread(xlsx);
[row column]=size(XLSX);
samnum=column; %暂时选择数据列数作为样本数量和训练数量
testsamnum=column;
forcastsam=o;
hiddenunit=8;
indim=M;outdim=N;
p=XLSX(1:M,:);
t=XLSX(M+1:row,:);
[samin,minp,maxp,tn,mint,maxt]=premnmx(p,t);
rand('state',sum(100*clock));
noisevar=0.01;
noise=noisevar*randn(o,samnum);
samout=tn+noise;
testsamin=samin;
testsamout=samout;
maxepochs=50000; %最大训练次数
learnrate=0.035; %学习速率
maxerror=0.65*10^(-3);
w1=0.5*rand(hiddenunit,M)-0.1;
B1=0.5*rand(hiddenunit,1)-0.1;
w2=0.5*rand(N,hiddenunit)-0.1;
B2=0.5*rand(N,1)-0.1;
errhistory=[];
for i=1:maxepochs;
hiddenout=logsig(w1*samin+repmat(B1,1,samnum));
networkout=w2*hiddenout+repmat(B2,1,samnum);
error=samout-networkout;
sse=sumsqr(error);
errhistory=[errhistory sse];
if sse<maxerror,break,end
%------------------神经网络核心--------------------
delta2=error;
delta1=w2'*delta2.*hiddenout.*(1-hiddenout);
dw2=delta2*hiddenout';
dB2=delta2*ones(samnum,1);
dw1=delta1*samin';
dB1=delta1*ones(samnum,1);
%-------------权值/阀值完成重新动态调整--------------
w2=w2+learnrate*dw2;
B2=B2+learnrate*dB2;
w1=w1+learnrate*dw1;
B1=B1+learnrate*dB1;
end
disp(['神经网络已经完成训练,请导入预测数据'])
hiddenout=logsig(w1*samin+repmat(B1,1,testsamnum));
anew=w2*hiddenout+repmat(B2,1,testsamnum);
ANS=postmnmx(anew,mint,maxt);
%绘制
x=1:column;
plot(x,ANS,'o',x,t,'b--+')
[filename2 filepath2]=uigetfile('*.xlsx','请选择神经网络需要预测的样本数据');
xlsx2=[filepath2 filename2];
XLSX2=xlsread(xlsx2);
pnew=tramnmx(XLSX2,minp,maxp);
hiddenout=logsig(w1*pnew+repmat(B1,1,forcastsam));
anew=w2*hiddenout+repmat(B2,1,forcastsam);
answer=postmnmx(anew,mint,maxt) %输出预测数据
%以下函数实现最优化功能,不需要可直接删去
[row2 column2]=size(pnew);
zero=eye(row2);
zero2=eye(row2);
A=pnew
for i=1:M
zero(i,i)=1.01;
zero2(i,i)=0.09;
testnum1=0;testnum2=0;
B=zero*A;
C=zero2*A;
for j=1:10000
B=zero*A;
C=zero2*A;
%如果说在对目前参数值左右变化时输出结果总不是最大,则循环继续
if postmnmx(w2*logsig(w1*A+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)<postmnmx(w2*logsig(w1*B...
+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)||...
postmnmx(w2*logsig(w1*A+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)<...
postmnmx(w2*logsig(w1*C+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)
if postmnmx(w2*logsig(w1*B+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)>...
postmnmx(w2*logsig(w1*A+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)
A=B;testnum1=testnum1+1;
elseif postmnmx(w2*logsig(w1*A+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)>...
postmnmx(w2*logsig(w1*C+repmat(B1,1,forcastsam))+repmat(B2,1,forcastsam),mint,maxt)
A=C;testnum2=tsetnum2+1;
end
else break;
end
end
zero(i,i)=1;
zero2(i,i)=1;
end
postmnmx(A,minp,maxp)
testnum1
testnum2
end
ANN神经网络代码在Matlab中的简单实现
于 2016-04-19 22:06:22 首次发布