Effacer les filtres
Effacer les filtres

Training a network with multiple time series of a particular event (NARX)

4 vues (au cours des 30 derniers jours)
I am fairly new to neural networks and matlab coding so please bear with me. I am trying to train a neural network to predict the outcomes of a SDOF spring, mass and damper simulation time series with the data coming from another simulation package. I would be really like to be able to feed in several individual examples of a test while chaning verious parameters e.g spring rate, mass and damping.
I have successfully been able to cobble together some code which allows me to go from csv files and feed them into the network and get results however, I have been trying to do this for "n" samples and I can't quite make it work. The dream would be to have a for loop over "n" samples and then train the network and run an unseen scenario. I have ran into the example here: https://uk.mathworks.com/help/deeplearning/ug/multiple-sequences-with-dynamic-neural-networks.html;jsessionid=068d9effcfb971932f62560a9690
When trying to impliment that example I ran into dimension errors which I didn't understand. Any help would be seriously appreciated as I am a total novice
I have pasted my working rev1 code below:
%The nonlinear autoregressive network with exogenous inputs
%used for prediction
%Future Development this would be a for loop over several timeseries
%samples to build a network
inputs1=importdata('1height.csv'); %upload data input for training and test
targets1=importdata('2height.csv'); %upload data target
% need to convert to format preparets understands
%Future Development this would represent the unknown variable test
inputs2=importdata('3height.csv'); %upload data (input) to test the model with new data samples
targets2=importdata('3height.csv'); % upload data (target)
aa1=inputs1';
aa2=targets1';
bb1=inputs2';
bb2=targets2';
out1=num2cell(aa1,1); %to convert aa1 to cell
out2=num2cell(bb1,1); %convert bb1 to cell
aaa2=num2cell(aa2,1); %to convert aa2 to cell
[x]=[out1];
[t]=[aaa2];
inputSeries = x;
targetSeries = t;
%create NARX network
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize =10;
net=narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
%training proportion setup
net.divideParam.trainRatio = 70/100;
net.divideParam.calRatio = 15/100;
net.divideParam.testRatio = 15/100;
%prepare the data for training and simulation using PREPARETS
[Xs,Xi,Ai,Ts] = preparets(net,x,{},t);
%train network
%Allows for training method to be changed
%trainbr for real tests will be used
trainFcn = 'trainlm';
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize,'open',trainFcn);
[net,tr]=train(net,Xs,Ts,Xi,Ai);
%test network
Y=net(Xs,Xi,Ai);
perf=mse(net,Ts,Y); %performance
E=gsubtract(Ts,Y); %error
%view network
view(net)
%plots(predictied using series-parallel NARXNN)
figure(1);
hold on;
plotperform(tr);
plotresponse(Ts,Y);
ploterrcorr(E);
plotinerrcorr(Xs,E);
%closed Loop network for multistep prediction
%The function CLOSELOOOP replaces feedback input with direct connection to
%output
netc=closeloop(net);
netc.name=[net.name '-Closed Loop'];
view(netc)
[Xs,Xi,Ai,Ts]=preparets(netc,x,{},t);
yc=netc(Xs,Xi,Ai);
%prediction using NARXNN in the case of new samples
figure(2);
hold on;
q=netc(out2,Xi,Ai); %q is predicted time series
w=cell2mat(q); %convert q from cell to numeric matrix

Réponse acceptée

Roshan Swain
Roshan Swain le 28 Déc 2022
Modifié(e) : Roshan Swain le 28 Déc 2022
As per my understanding, you are having short samples of data and you wish to train the model on those. There can be two ways to proceed this:
1. Stitching multiple datasets together to create a single dataset as shown in the shared example by you.
2. Train in a "for loop" for "n" number of samples(as you talked above).
However, the results of both the ways will be dfferent. This is because in the for loop the weights and biases will be updated after every iteration and likely result into a different checkpoint compared to a single dataset. This iterative method can still be useful as training on on a variety of different samples, you can help it to generalize better to unseen data and improve its ability to make accurate predictions.Think of it as a form of data augmentation. By training on different samples, you are providing the network with more diverse training data, which can help it to learn more robust and generalizable patterns.
To train this on a for loop, something similar to this below code can be used:
numSamples = 100; % number of samples to train on
for i = 1:numSamples
% load input and target data for the current sample
% assuming your incoming data is saved in format 1height.csv,
% 2height.csv, 3height.csv, ....so on.
inputs = importdata(sprintf('%dheight.csv', i));
targets = importdata(sprintf('%dheight.csv', i));
% convert data to format that preparets understands
inputs = inputs';
targets = targets';
inputs = num2cell(inputs, 1);
targets = num2cell(targets, 1);
% create your net object here.
%create NARX network
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize =10;
net=narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
%training proportion setup
net.divideParam.trainRatio = 70/100;
net.divideParam.calRatio = 15/100;
net.divideParam.testRatio = 15/100;
% prepare the data for training and simulation using PREPARETS
[Xs,Xi,Ai,Ts] = preparets(net, inputs, {}, targets);
% train the network on the current sample
[net,tr] = train(net, Xs, Ts, Xi, Ai);
end
% Ideally replace this block with the way you wish to train the samples.
As for the first way of combining all the data, you mentioned that you ran into dimensional errors. If you wish to use that way of training, the first step would likely be to take a look at the data samples you are trying to combine. There might be some differences in the data samples you are trying to combine. Understanding the data would be a great first step in debugging this type of errors.
If you are sucessfully able to combine the data and train, you can compare both the methods and choose the one that performs better on the test set.
Hope this helps.
  1 commentaire
alex bradley
alex bradley le 9 Jan 2023
Thanks so much for the help! I am having a play with it and will post my findings once I have them.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by