Should modify the number of layers to make multi-step closed loop NarX

1 view (last 30 days)
Should modify the number of layers to make multi-step closed loop NarX. How can i have multi-step with closeloop narx?

Accepted Answer

Shashank Prasanna
Shashank Prasanna on 1 Feb 2013
Maybe this example should help you FRANCISCO. This example predicts 30 time steps ahead for a SINE wave.
time = 1:10:7200;
timeh = time(1:end/2);
timenh = time(end/2 +1 : end);
input = sind(time(1:end/2));
toPredict = sind(time(end/2 +1 : end));
% plot(time(1:end/2),input,'o-g',time(end/2+1:end), toPredict,'+-r')
% legend('input to neural network','expected output');
inputSeries = tonndata(timeh,true,false);
targetSeries = tonndata(input,true,false);
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:10;
feedbackDelays = 1:10;
hiddenLayerSize = 50;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
% 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
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% 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)
% View the Network
% view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotregression(targets,outputs)
%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);
netc.name = [net.name ' - Closed Loop'];
% Only a certain number of predictions can be made
%accurately modulate this parameter to check it.
NumberOfPredictions = 30
% Creating a new input series which has the time
% inputs for the second half of the time but also
% includes last 10 time steps from the previous
% timeSeries
newInputSeries = timenh(1:NumberOfPredictions);
newInputSeries = [cell2mat(inputSeries(end-10:end) ) (newInputSeries)]
newInputSeries = num2cell(newInputSeries);
%Creating a new target with first 10 values which
%are the expected outputs network and the
%remaining targets are set to NAN, These values
%which are set to NAN will be predicted.
newTargetSet = nan(size(newInputSeries))
newTargetSet = num2cell(newTargetSet )
newTargetSet (1:10) = targetSeries(end-9:end)
[xc,xic,aic,tc] = preparets(netc,newInputSeries,{},newTargetSet);
yPredicted = sim(netc,xc,xic,aic)
% timenh = [ timenh timenh(end)+10];
figure,plot(timeh,cell2mat(targetSeries),'.-',timenh(1:NumberOfPredictions +1),cell2mat(yPredicted),'+-')
title(['PREDICTION PLOT - NumberOfPredictions =' num2str(NumberOfPredictions)])
legend('INPUT DATA','PREDICTED DATA')
  7 Comments
FRANCISCO
FRANCISCO on 5 Feb 2013
The boot files are twofold:
-One contains the inputs and consists of 2272 rows by 13 columns -the other file is composed of 2272 rows by 1 column.
The code already imported the split data for direct use.
I think the problem is that the network has been trained with dimensions and to create a new input data and targets, I do not meet those dimensions and still gives me error.
The code you posted I think it works correctly because I tested with some data in compliance with the network size and predicts well.
I will try to put the code used as clear as possible to see if you can give me some idea. I continued to test and I get the same error.
if true
% code
*% PREDICTION %*
xlsread p1; %matrix 1136x13
xlsread p2; %matrix 1136x13
xlsread t1; %matrix 1136x1
xlsread t2; %matrix 1136x1
p1=p1'; %matrix 13x1136
p2=p2'; %matrix 13X1136
t1=t1'; %matrix 1X1136
t2=t2'; %matrix 1x1136
inputSeries = tonndata(p1,true,false); %cell 1x1136
targetSeries = tonndata(t1,true,false); %cell 1x1136
% Create a Nonlinear Autoregressive Network with External Input
inputDelays = 1:1;
feedbackDelays = 1:1;
hiddenLayerSize = 16;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% 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);
% Closed Loop Network
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
NumberOfPredictions = 3;
% Creating a new input series
newInputSeries = p2(1:NumberOfPredictions); %here I built the array must be
%dimensioned 13x3
newInputSeries = [cell2mat(inputSeries(end-1:end) ) (newInputSeries)]; %
%matrix 13x5
newInputSeries = num2cell(newInputSeries); %cell 13x5
%Creating a new target
newTargetSet = nan(size(newInputSeries)); %matrix 13x5
newTargetSet = num2cell(newTargetSet );
newTargetSet (1:1) = targetSeries(end:end); %cell 13x5
end
Many thanks
FRANCISCO
FRANCISCO on 5 Feb 2013
I tried to apply to normalize newInputSeries tonndata newTargetSet and out and in both cases a 1x5 matrix. Applying preparets out well but I simulate values for prediction makes me the following error:
"Error using network/sim (line 130) Layer state sizes does not match net.layers{:}.size."
Many thanks

Sign in to comment.

More Answers (1)

Greg Heath
Greg Heath on 4 Feb 2013
A single hidden layer is sufficient.
Use the autocorrelation function of the target and the crosscorrelation function of the target and input to determine which input and feedback lags are significant.
Do not use the default division function 'dividerand' because the randomness destroys the correlations needed for prediction. I find 'divideblock' to be more appropriate. However, you may prefer 'divideind' or 'divideint'.
Hope this helps.
Greg
  1 Comment
FRANCISCO
FRANCISCO on 4 Feb 2013
Thank you very much. I'd like to see some examples and I guess what you are saying but I have a failure of concept and would like to see an example to clarify concepts. Many thanks for your reply

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!