How to predict 1 and 3 time steps with time series in NAR and after each forecast a retraining has to be done by increasing the size of the training set by one period and sliding validation set by another period till last test set period

1 次查看(过去 30 天)
My time series data has 150 points. I am using NARNET. I divide the data using "divideind" and then predict the value one time period ahead. One time period ahead can be done by open loop but i have no idea how to predict 3 time steps ahead. After each prediction, I have to slide the training set and validate set by 1 and then retrain to finally get mse and whole test data is alo utilized. This proces is to be repeated till the test set contains out of sample period. Here is my code:
targetSeries = tonndata(untitled,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); [trainInd,valInd,testInd] = divideind(150,1:80,81:90,91:150); 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)
Thanks in advance and please kindly help.

采纳的回答

Greg Heath
Greg Heath 2017-5-19
0. PLEASE use the braces {} to format your code.
> My time series data has 150 points. I am using NARNET. I divide the data using "divideind"
No, use DIVIDEBLOCK for unbiased future predictions.
>and then predict the value one time period ahead.
No.
1. "time period" is undefined and not used
2. The default "feedbackDelays = 1:2" yields a prediction of 2 "time steps" ahead.
>One time period ahead can be done by open loop but i have no idea how to predict 3 time steps ahead.
Open loop is just a temporary phase in the design. For implementation, the loop
has to be closed. The number of prediction timesteps is max(feedbackDelays).
>After each prediction, I have to slide the training set and validate set by 1 and then retrain to finally get mse and whole test data is also utilized. This process is to be repeated till the test set contains out of sample period.
No. For deployment after training:
After each prediction the prediction window AUTOMATICALLY slides one point ahead
and yields the next prediction. If d = max(feedbackDelays) you will have N-d predictions.
For training, the algorithm loops through the data by AUTOMATICALLY sliding one point
ahead and modifying the weights. The looping automatically stops when either
1. The training error drops to the specified goal,
2. The validation error increases continually for 6 (MATLAB DEFAULT) epochs
3. The maximum number of epochs (MATLAB DEFAULT = 1000) occurs
I suggest you see my NARNET posts in BOTH the NEWSREADER and ANSWERS.
HITS
NEWSREADER ANSWERS
greg NARNET 43 185
Hope this helps.
Thank you for formally accepting my answer
Greg
  5 个评论
Greg Heath
Greg Heath 2017-6-30
1. Set the rng before the loop
2. Record the current state of the rng at the top of the loop
rng(0)
j = 1:Ntrials
state(j) = rng
...
end
so that you know which rng state led to the best set of initial weigts.
See my NG & A posts.
Greg
Greg Heath
Greg Heath 2017-7-3
I do not recommend sequential training where updates are made after every input point.
TRAINING WILL TAKE FOREVER!!!
Stick with batch training and enjoy life.
Greg

请先登录,再进行评论。

更多回答(0 个)

类别

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