NARX perform additional tests on network: from GUI to code

2 views (last 30 days)
[EDIT: 20110722 22:22 CDT - reformat - WDR]
Hi,
I used the GUI to create my NARX network, and the GUI allows me to perform additional tests on my network with new input and output, but I need to know how to write that in code. When I generate the simple script of the GUI, it doesn't show that code.
Thanks so much from your help. Below is the script that the GUI creates.
% Solve an Autoregression Problem with External Input with a NARX Neural Network
% Script generated by NTSTOOL
% Created Fri Jul 22 13:33:47 EDT 2011
%
% This script assumes these variables are defined:
%
% p1 - input time series.
% p2 - feedback time series.
inputSeries = tonndata(p1,false,false);
targetSeries = tonndata(p2,false,false);
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 10;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
% Choose Input and 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
% Customize input parameters at: net.inputs{i}.processParam
% Customize output parameters at: net.outputs{i}.processParam
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.inputs{2}.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,inputSeries,{},targetSeries);
% Setup Division of Data for Training, Validation, Testing
% The function DIVIDERAND randomly assigns target values to training,
% validation and test sets during training.
% For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand'; % Divide data randomly
% The property DIVIDEMODE set to TIMESTEP means that targets are divided
% into training, validation and test sets according to timesteps.
% For a list of data division modes type: help nntype_data_division_mode
net.divideMode = 'value'; % 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
% Customize training parameters at: net.trainParam
net.trainFcn = 'trainlm'; % Levenberg-Marquardt
% Choose a Performance Function
% For a list of all performance functions type: help nnperformance
% Customize performance parameters at: net.performParam
net.performFcn = 'mse'; % Mean squared error
% Choose Plot Functions
% For a list of all plot functions type: help nnplot
% Customize plot parameters at: net.plotParam
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, plotregression(targets,outputs)
%figure, ploterrcorr(errors)
%figure, plotinerrcorr(inputs,errors)

Accepted Answer

Greg Heath
Greg Heath on 16 May 2013
1. If the uniform time series spacing is to be preserved to prevent destroying correlations, the default data division function 'dividerand' must not be accepted. A much better choice is use the override
net.divideFcn = 'divideblock'
2. Using arbitrary or default values for ID and FD (e.g., defaults ([1:2,1:2]) does not make sense when you can use the cross-correlation function to obtain the significant delays for ID and the autocorrelation function to obtain the significant delays for FD.
3. If you want to use NARXNET or TIMEDELAYNET in the NONPREDICTIVE REGRESSION MODE, include the value 0 as the first component in the input delay vector ID, i.e., ID(1) = 0
y(t) = f(x(t),x(t-1),...,x(t-id),y(t-1),...,y(t-fd)) % NONPREDICTIVE NARXNET
or
y(t) = f(x(t),x(t-1),...,x(t-id) % NONPREDICTIVE TIMEDELAYNET
However, if you want to use the time series in the PREDICTIVE MODE, exclude the value 0 from the input delay vector ID:
y(t) = f(x(t-1),...,x(t-id),y(t-1),...,y(t-fd))% PREDICTIVE NARXNET
or
y(t) = f(x(t-1),...,x(t-id) % PREDICTIVE TIMEDELAYNET
3. To continue a time series without gaps, the final input and layer states (Af,Xf) should be preserved
[ net tr Ys Es Xf Af ] = train(net, Xs, Ts, Xi, Ai);
%Ys = net( Xs, Xi, Ai); Es = Ts-Ys;
Ysnew = net( Xsnew, Xf, Af);
4. However, if there is a gap and intermediate values of X and Y are unknown, the best you can do is
a. Use the first id values of Xnew for Xinew, and the remaining values for Xsnew
b. Estimate the first fd values of Ynew for Ainew.
5. The best way I can think of estimating Ynew is to predict it using NARNET designed with the original data.
Hope this helps.
Thank you for formally accepting my answer
Greg

More Answers (1)

Mark Hudson Beale
Mark Hudson Beale on 9 Sep 2011
To apply the network to new data after training do the following:
inputSeries2 = { ... your new input series ... };
[inputs2,inputStates2,layerStates2,targets2] = preparets(net,inputSeries2);
outputs2 = net(inputs2,inputStates2,layerStates2);
  2 Comments
esra
esra on 15 May 2013
It does not work. If I do not put targetSeries as an input to preparets function, it gives NAN in outputs..
any help?
Greg Heath
Greg Heath on 30 Jul 2014
See the documentation for proper use of inputs
help/doc preparets
help doc timedelaynet
help/doc narnet
help/doc narxnet

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!