Using a trained NARX neural network to predict y(t) from x(t)

4 views (last 30 days)
Good day,
I am fairly new to the Matlab NN Toolbox and apologies if this question has been asked before but I really need some help.
I have trained a NARX network using:
  1. an input timeseries, which is 37723 X 25; and
  2. the corrosponding output/target timeseries, which is 37723 X 2
My objective: Using the trained NARX network, I have a new input timeseries, which is 1 X 25 and I want to predict the corrosponding output, which is therefore 1 X 2.
My problem: When I generate 10 ( as an example) 1 X 25 new input timeseries(i.e x2 in the code below) and I use x2 as input to netc to predict the corrosponding output(i.e y2 in the code below) for each of the 10 new input timeseries, I am always getting the same predicted output value/y2. It is important to note that the 10, 1X 25 new input timeseries have different values, therefore y2 for each of the 10 cannot be the same.
Thank you.
My code is below:
% Cd5_NN_Input - input time series.
% Cd5_NN_Output - feedback time series.
X = tonndata(Cd5_NN_Input,false,false);
T = tonndata(Cd5_NN_Output,false,false);
% Choose a Training Function
% For a list of all training functions type: help nntrain
% 'trainlm' is usually fastest.
% 'trainbr' takes longer but may be better for challenging problems.
% 'trainscg' uses less memory. Suitable in low memory situations.
trainFcn = 'trainlm'; % Levenberg-Marquardt backpropagation.
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:1;
feedbackDelays = 1:1;
hiddenLayerSize = 21;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
% 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.
[x,xi,ai,t] = preparets(net,X,{},T);
% Setup Division of Data for Training, Validation, Testing
% For a list of all data division functions type: help nndivision
net.divideFcn = 'dividerand'; % Divide data randomly
net.divideMode = 'time'; % Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% 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', 'ploterrhist', ...
'plotregression', 'plotresponse', 'ploterrcorr', 'plotinerrcorr'};
% Train the Network
[net,tr] = train(net,x,t,xi,ai);
% Test the Network
y = net(x,xi,ai);
e = gsubtract(t,y);
% Recalculate Training, Validation and Test Performance
trainTargets = gmultiply(t,tr.trainMask);
valTargets = gmultiply(t,tr.valMask);
testTargets = gmultiply(t,tr.testMask);
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, ploterrhist(e)
figure, plotregression(t,y)
%figure, plotresponse(t,y)
%figure, ploterrcorr(e)
%figure, plotinerrcorr(x,e)
% Convert the network to a closed-loop form
netc = closeloop(net);
% new input timeseries
x2 = tonndata(cd5',false,false);
% predict the corrosponding output for the new input timeseries
y2 = netc(x2);

Answers (0)

Community Treasure Hunt

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

Start Hunting!