应用了归一化的预测,在归一化的过程中使用了premnmx和postmnmx,并在最后给出了这两个函数的应用情况。
%应用了归一化的预测
clc;
clear;
originalData=rands(20,14);
inputSampledata=originalData';
outputData=rands(20,1);
outputSampledata=outputData';
% creating neural network and setting trainging parameters
gwwnet=newff(minmax(inputSampledata),[4,1],{'tansig','purelin'},'traingdm');
gwwnet.trainParam.show = 50;
gwwnet.trainParam.lr = 0.05;
gwwnet.trainParam.epochs = 5000;
gwwnet.trainParam.goal = 1e-3;
[input,mininput,maxinput,output,minoutput,maxoutput] = premnmx(inputSampledata,outputSampledata);
% Preprocess data so that minimum is -1 and maximum is 1
%
% Examples
%
% Here is the code to normalize a given data set so that the inputs
% and targets will fall in the range [-1,1].
% p = [-10 -7.5 -5 -2.5 0 2.5 5 7.5 10];
% t = [0 7.07 -10 -7.07 0 7.07 10 7.07 0];
% [pn,minp,maxp,tn,mint,maxt] = premnmx(p,t);
%
% If you just want to normalize the input, [pn,minp,maxp] = premnmx(p);
%
% Algorithm
% pn = 2*(p-minp)/(maxp-minp) - 1;
[gwwnet,tr]=train(gwwnet,input,output);
y=sim(gwwnet,input);
%data offset (converting the network output data to it original unit)
nnoutput = postmnmx(y,minoutput,maxoutput);
%
% Postprocess data that has been preprocessed by premnmx
% Syntax
%
% [P,T] = postmnmx(PN,minp,maxp,TN,mint,maxt)
% [p] = postmnmx(PN,minp,maxp)
% Description
%
% postmnmx postprocesses the network training set
% that was preprocessed by premnmx. It converts the data
% back into unnormalized units.
% postmnmx takes these inputs,
% PN -- R x Q matrix of normalized input vectors
% minp -- R x 1 vector containing minimums for each P
% maxp -- R x 1 vector containing maximums for each P
% TN -- S x Q matrix of normalized target vectors
% mint -- S x 1 vector containing minimums for each T
% maxt -- S x 1 vector containing maximums for each T
% and returns,
% P -- R x Q matrix of input (column) vectors
% T -- R x Q matrix of target vectors
% Examples
%
% In this example we normalize a set of training data with premnmx,
% create and train a network using the normalized data, simulate the network,
% unnormalize the output of the network using postmnmx,
% and perform a linear regression between
% the network outputs (unnormalized) and the targets
% to check the quality of the network training.
% p = [-0.92 0.73 -0.47 0.74 0.29; -0.08 0.86 -0.67 -0.52 0.93];
% t = [-0.08 3.4 -0.82 0.69 3.1];
% [pn,minp,maxp,tn,mint,maxt] = premnmx(p,t);
% net = newff(minmax(pn),[5 1],{'tansig' 'purelin'},'trainlm');
% net = train(net,pn,tn);
% an = sim(net,pn);
% [a] = postmnmx(an,mint,maxt);
% [m,b,r] = postreg(a,t);
%
% Algorithm
% p = 0.5(pn+1)*(maxp-minp) + minp;
%plot
time=1:1:20;
plot(time,outputSampledata,'-v',time,nnoutput,'o');
legend('actual output','NN output');
xlabel('time');ylabel('Learning fitting curve');
%scenario1 forecasting process
column=10;
for i=1:column;
SceInput(1,i)=inputSampledata(1,20)*(1.0464^i);
SceInput(2,i)=inputSampledata(2,20)*(1.0631^i);
SceInput(3,i)=inputSampledata(3,20)*(1.0872^i);
SceInput(4,i)=inputSampledata(4,20)*(1.2044^i);
SceInput(5,i)=inputSampledata(5,20)*(1.2326^i);
SceInput(6,i)=inputSampledata(6,20)*(1.0605^i);
SceInput(7,i)=2*(1.01^i);
SceInput(8,i)=42*(1.02^i);
SceInput(9,i)=inputSampledata(9,20)*(1.1426^i);
SceInput(10,i)=inputSampledata(10,20)*(1.017^i);
SceInput(11,i)=inputSampledata(11,20)*(1.0205^i);
SceInput(12,i)=inputSampledata(12,20)*(1.1336^i);
SceInput(13,i)=inputSampledata(13,20)*(1.1599^i);
SceInput(14,i)=inputSampledata(14,20)*(1.1783^i);
end
for j=1:20;
for i=1:14;
recalldata(i,j)=inputSampledata(i,j);
end
end
for j=21:30;
for i=1:14;
recalldata(i,j)=SceInput(i,j-20)
end
end
[alterinput,mininput,maxinput] = premnmx(recalldata);
%training
fvalue=sim(gwwnet,alterinput);
%data offset (converting the network output data to it original unit)
forecastvalue = postmnmx(fvalue,minoutput,maxoutput);
%plot
% waitforbuttonpress;
%
% Wait for key or mouse button press
% Syntax
% k = waitforbuttonpress
%
% Description
%
% k = waitforbuttonpress blocks the caller's execution stream
% until the function detects that the user has pressed a mouse button
% or a key while the figure window is active. The function returns 0
% if it detects a mouse button press 1 if it detects a key press
%
% Example
%
% These statements display text in the Command Window
% when the user either clicks a mouse button or
% types a key in the figure window:
% w = waitforbuttonpress;
% if w == 0
% disp('Button press')
% else
% disp('Key press')
% end
% clf;
figure;
time1=1:1:30;
time2=1:1:20;
plot(time1,forecastvalue,'r-',time2,outputSampledata,'-');
legend('预测曲线','实际曲线');
title('曲线');
xlabel('时间');ylabel('量');
%%%%%%%premnmx及postmnmx使用说明
% clc;
% clear;
% p = [1 2 3 4;
% 5 6 8 9 ];
% [p1 pmin pmax]=premnmx(p);
% p2=postmnmx(p1,pmin,pmax);
% p1
% pmin
% pmax
% p2
%
% p1 =
%
% -1.0000 -0.3333 0.3333 1.0000
% -1.0000 -0.5000 0.5000 1.0000
%
%
% pmin =
%
% 1
% 5
%
%
% pmax =
%
% 4
% 9
%
%
% p2 =
%
% 1 2 3 4
% 5 6 8 9