Training TCN network in batches to predict a continuous variable
5 views (last 30 days)
Show older comments
Isabelle Museck
on 24 Jun 2024
Answered: Yash Sharma
on 26 Jun 2024
I am trying to train a ML model with data from 10 different trials in batches (one batch=one trial) in order to preserve the time series nature of the data. Right now the data is stored in cell array (XTrain) with each cell containing the prediction inputs (the 3 accelerometer mesurements for 541 seconds) for all 10 trials and another cell array that contains the correposding continous variable we are trying to predict/output values (a single continous variable for the 541 seconds) stored in (YTrain). The network I am using to train the model is stored in "net" as shown here along with the training options:
options = trainingOptions("adam", ...
MaxEpochs=60, ...
miniBatchSize=1, ...
InputDataFormats="CTB", ...
Plots="training-progress", ...
Metrics="rmse", ...
Verbose=0);
net = trainnet(Xtrain,Ytrain,net,"mse",options)
When I try to train the network with my Traning data in this format it is giving me an error that: Expected input to be of size 3x1, but it is of size 541x1. I am wondering if I need to make a custom loop that trains my network for each trial seperatley (in batches) and how I would go about doing this. Or is there something in the training options that I can change to train my network on all the data from each trial in batches to preserve the time-series for each trial.
0 Comments
Accepted Answer
Yash Sharma
on 26 Jun 2024
To train your network with time-series data from multiple trials while preserving the sequential nature of the data, you need to ensure that the data format and batch processing are correctly set up. Here's a step-by-step guide to help you achieve this:Step
1: Prepare the Data
Ensure your data is in the correct format. Since you have 10 trials, each with 3 accelerometer measurements over 541 seconds, your input data should be formatted as [features, time, batch] for each trial. Similarly, the output data should be formatted as [time, batch].
Step 2: Define the Network
Ensure your network architecture is suitable for time-series data. Typically, a recurrent neural network (RNN) or a long short-term memory (LSTM) network is used for time-series data.
Step 3: Train the Network
Use the trainNetwork function with the appropriate options to train your network.
Here’s an example code snippet to illustrate these steps:
% Example data dimensions
numFeatures = 3;
numTimeSteps = 541;
numTrials = 10;
% Generate example data (replace this with your actual data)
XTrain = cell(1, numTrials);
YTrain = cell(1, numTrials);
for i = 1:numTrials
XTrain{i} = randn(numFeatures, numTimeSteps, 1, 'single'); % 3x541x1
YTrain{i} = randn(numTimeSteps, 1, 'single'); % 541x1
end
% Convert cell arrays to dlarray format
for i = 1:numTrials
XTrain{i} = dlarray(XTrain{i}, 'CTB'); % 'CTB' stands for 'Channel', 'Time', 'Batch'
YTrain{i} = dlarray(YTrain{i}, 'TB'); % 'TB' stands for 'Time', 'Batch'
end
% Define the network architecture
layers = [
sequenceInputLayer(numFeatures, 'Name', 'input')
lstmLayer(50, 'OutputMode', 'sequence', 'Name', 'lstm')
fullyConnectedLayer(1, 'Name', 'fc')
regressionLayer('Name', 'output')
];
% Specify training options
options = trainingOptions('adam', ...
'MaxEpochs', 60, ...
'MiniBatchSize', 1, ...
'SequenceLength', 'longest', ...
'InputDataFormats', 'CTB', ...
'Plots', 'training-progress', ...
'Metrics', 'rmse', ...
'Verbose', 0);
% Train the network
net = trainNetwork(XTrain, YTrain, layers, options);
This approach ensures that your network is trained on each trial separately while preserving the sequential nature of the data. Adjust the network architecture and training options as needed to fit your specific dataset and requirements.
0 Comments
More Answers (0)
See Also
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!