Future value prediction with neural network method and right input and target format data

4 次查看(过去 30 天)
Hello, Could anyone explain, how to do following with matlab neural network NARX method I have six variables numbers (they depends on each other) for each day and for 10 days.
  • day1 x1, x2, x3, x4, x5, x6
  • day2 x1, x2, x3, x4 ,x5,x6
  • ..........................
  • day10 x1, x2, x3, x4, x5, x6
and I want to predict these six variables for 11th day using matlab neural network So prediction:
  • day11 y1,y2,y3,y4,y5, y6
I am starting to work with neural networks, so I know that I have to use
ntstool and to select NARX method, but there I stopped with right format of data at matlab work space variables table. Could anyone please help how to enter input and target data in right format for this case at matlab work space variables table that could allow to simulate this.
  3 个评论
AKHILA GOUDA
AKHILA GOUDA 2017-9-20
Hello sir, If you got your ans then please help me . I have same problem that how can I predict next day data.. Thank you

请先登录,再进行评论。

采纳的回答

Greg Heath
Greg Heath 2013-5-30
Your inputs are outputs. Therefore you should be using NAR, not NARX.
How many days, weeks, months or years of data do you have?
Did you look at the output auto and cross-correlation functions to determine that you need feedback lags of 10 days ?
Hope this helps.
Greg
  1 个评论
Kranthi Kumar
Kranthi Kumar 2015-10-30
Sir, I have also same kind of query. I have 5 variables.I need to predict those 5 variables in future. i have some 259 data points of 259 days.What would be my input and target values? Those 5 variables are some what interrelated to each other.I am using nts tool NAR. Please help me with this.

请先登录,再进行评论。

更多回答(8 个)

Greg Heath
Greg Heath 2013-6-5
编辑:Greg Heath 2013-6-5
This is an example of using a double loop to choose as small a number of hidden nodes as possible to mitigate overtraining an overfit net and to mitigate the failure of random initial weights by obtaining multiple designs.
To make it easier to understand, I used 'dividetrain' which is valid for this data because the number of training equations, Neq, is much greater than the number of unknown weights that have to be estimated, Nw.
For small data sets where Neq >> Nw is not possible, regularization (see help mse and doc mse) or validation set stopping using nonrandom data division (see help divideblock and divideind) should be used. If validation stopping is used, the validation performance is used to choose the best net. A completely unbiased estimate of performance on new data is then obtained from the corresponding test set.
Hope this helps.
Greg
close all,clear all, clc, plt=0;
tic
T = simplenar_dataset;
t = cell2mat(T);
whos
[ O N ] = size(T) % [ 1 100]
Neq = prod(size(T)) % 100
rng(0)
for k = 1:100
n = randn(1,N);
autocorrn = nncorr(n,n,N-1,'biased');
sortabsautocorrn = sort(abs(autocorrn));
M = floor(0.95*(2*N-1)) % 189
thresh95(k) = sortabsautocorrn(M);
end
sigthresh95 = mean(thresh95) % 0.2194
zt = zscore(t,1);
autocorrzt = nncorr(zt,zt,N-1,'biased');
lags = 0:N-1;
siglags = -1+find(abs(autocorrzt(N:end))>sigthresh95);
plt = plt+1, figure(plt) % Fig 1
hold on
plot( zeros(1,N), 'k--', 'LineWidth', 2 )
plot(sigthresh95*ones(1,N), 'r--', 'LineWidth', 2 )
plot(-sigthresh95*ones(1,N), 'r--', 'LineWidth', 2 )
plot( lags, autocorrzt(N:end), 'LineWidth', 2 )
plot( siglags, autocorrzt(N+siglags), 'o', 'LineWidth', 2 )
FD = 1:2
NFD = length(FD) % 2
LDB = max(FD) % 2
Ns = N-LDB % 98
Nseq = Ns*O % 98
% Nw = (NFD*O+1)*H+(H+1)*O
Hub = -1+ceil( (Nseq-O) / (NFD*O+O+1)) % 24
Hmax = floor(Hub/10) % Hmax = 2 ==> Nseq >>Nw :
Hmin = 0
dH = 1
Ntrials = 10
j=0
rng(4151941)
for h = Hmin:dH:Hmax
j = j+1
if h == 0
net = narnet( FD, [] );
Nw = ( NFD*O + 1)*O
else
net = narnet( FD, h );
Nw = ( NFD*O + 1)*h + ( h + 1)*O
end
Ndof = Nseq-Nw
[ Xs Xi Ai Ts ] = preparets(net,{},{},T);
ts = cell2mat(Ts);
MSE00s = mean(var(ts',1))
MSE00as = mean(var(ts'))
MSEgoal = 0.01*Ndof*MSE00as/Neq
MinGrad = MSEgoal/10
net.trainParam.goal = MSEgoal;
net.trainParam.min_grad = MinGrad;
net.divideFcn = 'dividetrain';
for i = 1:Ntrials
net = configure(net,Xs,Ts);
[ net tr Ys ] = train(net,Xs,Ts,Xi,Ai);
ys = cell2mat(Ys);
stopcrit{i,j} = tr.stop;
bestepoch(i,j) = tr.best_epoch;
MSE = mse(ts-ys);
MSEa = Nseq*MSE/Ndof;
R2(i,j) = 1-MSE/MSE00s;
R2a(i,j) = 1-MSEa/MSE00as;
end
end
stopcrit = stopcrit %Min grad reached (for all).
bestepoch = bestepoch
R2 = R2
R2a = R2a
Totaltime = toc
% H = 0 1 2
%
% bestepoch = 1 7 16
% 1 7 7
% 1 7 4
% 1 5 8
% 1 6 5
% 1 5 11
% 1 8 5
% 1 4 16
% 1 5 6
% 1 3 6
%
% R2 = 0.8885 0.9948 0.9951
% 0.8885 0.9954 0.9968
% 0.8885 0.9950 0.9983
% 0.8885 0.9946 0.9958
% 0.8885 0.9951 0.9951
% 0.8885 0.9929 0.9915
% 0.8885 0.9908 0.9956
% 0.8885 0.9926 0.9914
% 0.8885 0.9922 0.9972
% 0.8885 -0.0000 0.9934
%
% R2a = 0.8861 0.9945 0.9947
% 0.8861 0.9952 0.9965
% 0.8861 0.9947 0.9981
% 0.8861 0.9944 0.9955
% 0.8861 0.9949 0.9946
% 0.8861 0.9926 0.9908
% 0.8861 0.9904 0.9952
% 0.8861 0.9923 0.9907
% 0.8861 0.9919 0.9969
% 0.8861 -0.0430 0.9928
  6 个评论
Tomas Simonson
Tomas Simonson 2015-7-1
Mr. Heath, I am also getting a different number for the value of sigthresh95. I am using matlab 2015a, is it possible that the random number generator I am using is different than when if was tested? I tried it with rng(o,'v4') and the result was still not what was posted in the comments. Any help is greatly appreciated. -Tomas
Greg Heath
Greg Heath 2015-7-1
T = simplenar_dataset;
t = cell2mat(T);
[I N ] = size(t)
meant = mean(t)
stdtb = std(t,1) % biased( div by N)
ztb = (t-meant)/stdtb;
minmaxdztb = minmax(ztb-zscore(t,1))
stdtu = std(t,0) % unbiased( div by N-1)
ztu = (t-meant)/stdtu;
minmaxdztu = minmax(ztu-zscore(t,0))
I = 1
N = 100
meant = 0.72345
stdtb = 0.25161
minmaxdztb = [ 0 0 ]
stdtu = 0.25287
minmaxdztu = [ 0 0 ]

请先登录,再进行评论。


Povi Nike
Povi Nike 2013-5-30
Hello thanks for answer. I have data for 1 year , it is 212 sets of x1, x2, x3, x4, x5, x6. My purpose is to predict next date future values of number set and correlation functions of sets 212 of values.
Ok I shall use NAR method by using ntstool. AS I understood I enter targets and NAR method perform calculations and saved output file gives predicted values? I entered 13 sets and it calculated 11 sets of output values Why less than targets sets?

Greg Heath
Greg Heath 2013-5-31
Your comments are not clear.
You have data for N = 212 consecutive days of the same year?
You want to calculate the O=6 autocorrelation functions and 6*5/2= 15 cross-correlation functions to find a good range of delays to use?
To avoid confusion, I suggest first concentrating on the positive lags of the 6 autocorrelation functions. If final results need improvement take a look at the cross-correlation functions of the worst predicted variables.
Apparently you used the NAR defaults FD = 1:2 (ND = 2 delays) and H = 10 (hidden nodes); With O = 6 outputs, there are
Nw = (ND*O+1)*H+(H+1)*O = 130+66 = 196
unknown weights weights to estimate with
Ntrneq = Ntrn*O = 6*Ntrn
training equations. For Ntrneq >> Nw, you need
Ntrn >> ~33
However, you only had the default Ntrn = 13-2*round(0.15*13)= 9.
Therefore I suggest
1. Use all of the data Ntrn = 221-(0.3*221)=155
2. Replace dividerand with divideblock
3. Design 10 nets to mitigate the random weight initialization.
If you need more help, please post your code.
Hope this helps.
Greg
  1 个评论
Povi Nike
Povi Nike 2013-6-3
Thanks for answer. I built a code.
Where and how I should to replace dividerand with divideblock. How to indicate 10 nets to mitigate the random weight initialization at code?
% Solve an Autoregression Time-Series Problem with a NAR Neural Network % Script generated by NTSTOOL % Created Mon Jun 03 13:58:06 EEST 2013 % % This script assumes this variable is defined: % % unnamed - feedback time series.
targetSeries = tonndata(unnamed,false,false);
% Create a Nonlinear Autoregressive Network feedbackDelays = 1:2; hiddenLayerSize = 10; net = narnet(feedbackDelays,hiddenLayerSize);
% Choose Feedback Pre/Post-Processing Functions % Settings for feedback input are automatically applied to feedback output % For a list of all processing functions type: help nnprocess net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
% Prepare the Data for Training and Simulation % The function PREPARETS prepares timeseries data for a particular network, % shifting time by the minimum amount to fill input states and layer states. % Using PREPARETS allows you to keep your original time series data unchanged, while % easily customizing it for networks with differing numbers of delays, with % open loop or closed loop feedback modes. [inputs,inputStates,layerStates,targets] = preparets(net,{},{},targetSeries);
% Setup Division of Data for Training, Validation, Testing % For a list of all data division functions type: help nndivide net.divideFcn = 'dividerand'; % Divide data randomly net.divideMode = 'time'; % Divide up every value net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100;
% Choose a Training Function % For a list of all training functions type: help nntrain net.trainFcn = 'trainlm'; % Levenberg-Marquardt
% Choose a Performance Function % For a list of all performance functions type: help nnperformance net.performFcn = 'mse'; % Mean squared error
% Choose Plot Functions % For a list of all plot functions type: help nnplot net.plotFcns = {'plotperform','plottrainstate','plotresponse', ... 'ploterrcorr', 'plotinerrcorr'};
% Train the Network [net,tr] = train(net,inputs,targets,inputStates,layerStates);
% Test the Network outputs = net(inputs,inputStates,layerStates); errors = gsubtract(targets,outputs); performance = perform(net,targets,outputs)
% Recalculate Training, Validation and Test Performance trainTargets = gmultiply(targets,tr.trainMask); valTargets = gmultiply(targets,tr.valMask); testTargets = gmultiply(targets,tr.testMask); trainPerformance = perform(net,trainTargets,outputs) valPerformance = perform(net,valTargets,outputs) testPerformance = perform(net,testTargets,outputs)
% View the Network view(net)
% Plots % Uncomment these lines to enable various plots. %figure, plotperform(tr) %figure, plottrainstate(tr) %figure, plotresponse(targets,outputs) %figure, ploterrcorr(errors) %figure, plotinerrcorr(inputs,errors)
% Closed Loop Network % Use this network to do multi-step prediction. % The function CLOSELOOP replaces the feedback input with a direct % connection from the outout layer. netc = closeloop(net); [xc,xic,aic,tc] = preparets(netc,{},{},targetSeries); yc = netc(xc,xic,aic); perfc = perform(net,tc,yc)
% Early Prediction Network % For some applications it helps to get the prediction a timestep early. % The original network returns predicted y(t+1) at the same time it is given y(t+1). % For some applications such as decision making, it would help to have predicted % y(t+1) once y(t) is available, but before the actual y(t+1) occurs. % The network can be made to return its output a timestep early by removing one delay % so that its minimal tap delay is now 0 instead of 1. The new network returns the % same outputs as the original network, but outputs are shifted left one timestep. nets = removedelay(net); [xs,xis,ais,ts] = preparets(nets,{},{},targetSeries); ys = nets(xs,xis,ais); closedLoopPerformance = perform(net,tc,yc)

请先登录,再进行评论。


Greg Heath
Greg Heath 2013-6-3
%Where and how I should to replace dividerand with divideblock.
net = narnet % No semicolon
See the entry for net.divideFcn? To change it use
net.divideFcn = 'divideblock';
Similarly for any other property of the net that you wish to change.
%How to indicate 10 nets to mitigate the random weight initialization at code?
Search for my double loop codes using
greg Ntrials
I did not look at the rest of your post.
Come back with specifics if you have problems.
You might want to search for more info using
greg narnet
Hope this helps.
Greg

Povi Nike
Povi Nike 2013-6-4
编辑:Povi Nike 2013-6-5
OK I changed: net = narnet % No semicolon; net.divideFcn = 'divideblock'; For undestanding clearly results Are saved output my future predicted values?

Povi Nike
Povi Nike 2013-6-5
Thanks for example. Ok I adapt this double loop model to my model. I have general question. When NAR method is used , neural network is trained, I get results, where to find predicted values of next steps? First I thought that saved output values are predicted values but after some examples I understood that is not.
  1 个评论
Greg Heath
Greg Heath 2013-10-1
Yes, the outputs are predicted values.
However, if you wish to continue the predictions beyond the current data,
1. Make sure the current output with target feedback yields a very low error rate
2. Convert to a closeloop configuration.
3. Test to see if the closeloop configuration with output (not target) feedback also yields a sufficiently low error.
4. If the closeloop configuration doesn't measure up, use train to modify the weights and improve performance.
5. Finally, run the closeloop net beyond the original data.

请先登录,再进行评论。


Greg Heath
Greg Heath 2013-6-8
help closeloop
doc closeloop
help removedelay
doc removedelay
Also search for these terms in NEWSGROUP and ANSWERS.
Greg

akhilesh
akhilesh 2016-6-27
Using Time series toolbox I have generated a network model and it takes 4 delay input and gives 4 delay output. Confusion is, what 4 delay output values represent. Is they are 4 predicted values, if so then which one is more accurate. Please clear.

类别

Help CenterFile Exchange 中查找有关 Sequence and Numeric Feature Data Workflows 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by