NARX with multiple time series: Data dividision problem

8 views (last 30 days)
Hello everyone,
I want to train a NARX-model for multi-step prediction of time series. The task has the following boundary conditions:
1) 100 time series of different lengths in minute resolution
2) 2 inputs, 1 target
3) All time series have the shape of an exponential decay
4) All time series were first mixed (randperm) and then distributed to the sets for training (1:70), validation (71:85) and testing (86:100).
5) The time series of the sets are transformed to cell-arrays via 'catsamples' in order to train the network with multiple time series at once.
I am not sure how to divide the data properly. I would like to train the net with all the complete time-series of the training set and the validation set by using early stopping in order to avoid overfitting. The test set is used for testing the trained net with unseen data. But when I use the built-in functions for data division, the time series are torn apart:
net.divideFcn = 'divideblock';
net.divideMode = 'sampletime';
net.divideParam.trainRatio = 0.7; % First 70% of timesteps are used for training
net.divideParam.valRatio = 0.15; % Next 15% of timesteps are used for validation
net.divideParam.testRatio = 0.15; % Last 15% of timesteps are put aside for testing
This means that the first 70% of the timesteps are put into training set, the next 15% of the timesteps are put into validation set and the last 15% of the timesteps are put into test set.
So my question is:
How can I divide the data into three sets with complete time series which are not torn apart? I illustrated my question with the following 2 pictures:
Best regards
Torsten
  1 Comment
Torsten K
Torsten K on 19 Oct 2020
I would also be very happy to receive feedback from matlab staff. Please, dear experts, give me a hint how I can solve the problem or what I'm doing wrong!
Best regards
Torsten

Sign in to comment.

Accepted Answer

Torsten K
Torsten K on 21 Oct 2020
OK, I found the solution. Here is a small example, based on this example:
load magmulseq;
y_mul = catsamples(y1,y2,y3,y1,y2,y3,'pad');
u_mul = catsamples(u1,u2,u3,u1,u2,u3,'pad');
d1 = [1:2];
d2 = [1:2];
narx_net = narxnet(d1,d2,10);
narx_net.divideMode = 'sample';
narx_net.divideFcn = 'divideind';
narx_net.divideParam.trainInd = 1:2;
narx_net.divideParam.valInd = 3:4;
narx_net.divideParam.testInd = 5:6;
[p,Pi,Ai,t] = preparets(narx_net,u_mul,{},y_mul);
[net tr] = train(narx_net,p,t,Pi);
If you check the training record tr, you can see the following for all 1484 time-steps:
>> tr.trainMask{1,1}
ans =
1 1 NaN NaN NaN NaN
>> tr.valMask{1,1}
ans =
NaN NaN 1 1 NaN NaN
>> tr.testMask{1,1}
ans =
NaN NaN NaN NaN 1 1
This is exactly what I wanted.
Best regards
Torsten
  2 Comments
Alex Ander
Alex Ander on 4 Feb 2022
Hello everyone,
I wanna use a NARX-model similar to the case of Torsten. I've got a MISO-system and different time series data. Due to the discontinuity of the data, I don't just want to use one large batch, but instead several smaller ones.
Does anyone know how to use aboves structure for MISO/ MIMO systems?
I started by expanding the Matlab example. So instead of 1 by x dimensions input and output data, I generated 2 by x dimensions input data. When I try to run it, I get an error: The number of input signals does not match network's non-feedback inputs.
Has anyone an idea, how to solve this?
Best Regards
Alex
% Parameters for Random Cell-Arrays
input_Par = 1;
input_Par2 = 2;
times = 100;
t1 = floor(times-randi([0,30])*1);
% 'Outputdata'
y1 = num2cell(rand(input_Par,times));
y2 = num2cell(rand(input_Par,t1));
y3 = num2cell(rand(input_Par,floor(times-t1/2)));
% 'Inputdata'
u1 = num2cell(rand(input_Par2,times));
u2 = num2cell(rand(input_Par2,t1));
u3 = num2cell(rand(input_Par2,floor(times-t1/2)));
y_mul = catsamples(y1,y2,y3,'pad');
u_mul = catsamples(u1,u2,u3,'pad');
% NARX Parameters
d1 = [1:2];
d2 = [1:2];
narx_net = narxnet(d1,d2,10);
narx_net.divideFcn = '';
narx_net.trainParam.min_grad = 1e-10;
[p,Pi,Ai,t] = preparets(narx_net,u_mul,{},y_mul);
narx_net = train(narx_net,p,t,Pi);
Alex Ander
Alex Ander on 8 Feb 2022
for multiple inputs, you have to watch out for the right data dimensions. In the for-loops the data is transformed to the right dimensions. Probably there is a more way, than the example shown below.
If anyone has better suggestions, feel free to comment.
% Parameters for Random Cell-Arrays
input_Par = 1;
input_Par2 = 2;
times = 100;
t1 = floor(times-randi([0,30])*1);
% 'Outputdata'
y1 = num2cell(rand(input_Par,times));
y2 = num2cell(rand(input_Par,t1));
y3 = num2cell(rand(input_Par,floor(times-t1/2)));
% 'Inputdata'
u1 = (rand(input_Par2,times));
u2 = (rand(input_Par2,t1));
u3 = (rand(input_Par2,floor(times-t1/2)));
for i=1:length(u1)
u1_tilde{i} = [u1(1,i);u1(2,i)];
end
i=1;
for i=1:length(u2)
u2_tilde{i} = [u2(1,i);u2(2,i)];
end
i=1;
for i=1:length(u3)
u3_tilde{i} = [u3(1,i);u3(2,i)];
end
y_mul = catsamples(y1,y2,y3,'pad');
u_mul = catsamples(u1_tilde,u2_tilde,u3_tilde,'pad');
% NARX Parameters
d1 = [1:2];
d2 = [1:2];
narx_net = narxnet(d1,d2,10);
narx_net.divideMode = 'sample';
narx_net.divideFcn = 'divideind';
narx_net.divideParam.trainInd = 1:2;
narx_net.divideParam.valInd = 3:4;
narx_net.divideParam.testInd = 5:6;
narx_net.trainParam.min_grad = 1e-10;
[p,Pi,Ai,t] = preparets(narx_net,u_mul,{},y_mul);
[narx_net, tr] = train(narx_net,p,t,Pi);
disp(tr.trainMask);

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!