Error with SequenceInputLayer - TrainNetwork Expects Sequence Dimensions
24 views (last 30 days)
Show older comments
Hello! I'm working on a project that involves training an LSTM (or GRU) model with time-series data. My data consists of 2 features (strain and deflection) over 5 time steps. I am getting the following error:
Error using trainNetwork: The training sequences are of feature dimension 8 5 but the input layer expects sequences of feature dimension 2.
Here's a breakdown of the data:
- XTrain has dimensions: [8, 5, 2] (8 sequences, 5 time steps, 2 features).
- I'm reshaping the data to [sequenceLength, numFeatures, numSequences] using permute(XTrain, [2, 3, 1]).
- YTrain has dimensions: [8, 2] (8 sequences, 2 output values).
I'm using the following layer configuration:
layers = [ ...
sequenceInputLayer(2) % 2 features: strain and deflection
lstmLayer(100, 'OutputMode', 'last') % LSTM layer
fullyConnectedLayer(2) % Output layer for 2 values: strain and deflection
regressionLayer];
What am I missing? How should I format the input data to make this work with trainNetwork?
0 Comments
Accepted Answer
Malay Agarwal
on 24 Sep 2024
For sequence inputs, the trainNetwork function expects the sequences to be passed as a cell array of dimensions , where m is the number of sequences. Each of the m items in the cell array should have dimensions , where n is the number of features in each sequence and T is the length of the sequences (number of time steps). You can pass an array of dimensions directly only when you have a single sequence.
Refer to the following resource for more information regarding the expected dimensions of sequence inputs for trainNetwork: https://www.mathworks.com/help/deeplearning/ref/trainnetwork.html#mw_36a68d96-8505-4b8d-b338-44e1efa9cc5e.
Here is a small example for your reference:
load("data.mat");
layers = [ ...
sequenceInputLayer(2) % 2 features: strain and deflection
lstmLayer(100, 'OutputMode', 'last') % LSTM layer
fullyConnectedLayer(2) % Output layer for 2 values: strain and deflection
regressionLayer];
maxEpochs = 50;
miniBatchSize = 27;
options = trainingOptions("adam", ...
MiniBatchSize=miniBatchSize, ...
MaxEpochs=maxEpochs, ...
ExecutionEnvironment="cpu", ...
Verbose=true);
net = trainNetwork(XTrain, YTrain, layers, options);
Hope this helps!
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!