Error with SequenceInputLayer - TrainNetwork Expects Sequence Dimensions
조회 수: 24 (최근 30일)
이전 댓글 표시
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
채택된 답변
Malay Agarwal
2024년 9월 24일
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!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!