Error with SequenceInputLayer - TrainNetwork Expects Sequence Dimensions

24 views (last 30 days)
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?

Accepted Answer

Malay Agarwal
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);
|========================================================================================| | Epoch | Iteration | Time Elapsed | Mini-batch | Mini-batch | Base Learning | | | | (hh:mm:ss) | RMSE | Loss | Rate | |========================================================================================| | 1 | 1 | 00:00:00 | 0.85 | 0.4 | 0.0010 | | 5 | 50 | 00:00:01 | 0.38 | 7.4e-02 | 0.0010 | | 10 | 100 | 00:00:02 | 0.38 | 7.2e-02 | 0.0010 | | 15 | 150 | 00:00:03 | 0.38 | 7.3e-02 | 0.0010 | | 20 | 200 | 00:00:03 | 0.38 | 7.2e-02 | 0.0010 | | 25 | 250 | 00:00:04 | 0.38 | 7.2e-02 | 0.0010 | | 30 | 300 | 00:00:05 | 0.38 | 7.2e-02 | 0.0010 | | 35 | 350 | 00:00:06 | 0.38 | 7.2e-02 | 0.0010 | | 40 | 400 | 00:00:07 | 0.38 | 7.2e-02 | 0.0010 | | 45 | 450 | 00:00:08 | 0.38 | 7.2e-02 | 0.0010 | | 50 | 500 | 00:00:09 | 0.38 | 7.2e-02 | 0.0010 | |========================================================================================| Training finished: Max epochs completed.
Hope this helps!
  1 Comment
Huzaifa
Huzaifa on 24 Sep 2024
Hi,
Thank you for the clarification. I’ve already resolved the issue by correctly formatting the input sequences as cell arrays, and the model is now working as expected. Your guidance was helpful in understanding the input structure.
Best regards,
Huzaifa

Sign in to comment.

More Answers (0)

Categories

Find more on Image 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!