Filter löschen
Filter löschen

Unable to reshape the data to 3D on Matlab for LSTM training?

13 Ansichten (letzte 30 Tage)
Jingyi Li
Jingyi Li am 30 Aug. 2024 um 15:39
Beantwortet: Ayush Aniket am 31 Aug. 2024 um 4:57
I was trying to reshape my data array from 2D to 3D to be able to be loaded to LSTM for training, i encountered the dimensionality problem in the beginning when I could not reshape my data into ```[samples, time steps, features]``` (in my case, ```[2097 1000 1]```, since the MATLAB always automatically turns it into ```[[2097 1000]```.
The reshaping method I used is
sequence_length = 1000;
num_features = 1; % 1D time series
% Check the number of samples
num_samples = floor(length(final_filtered_signal) / sequence_length);
X = reshape(final_filtered_signal(1:num_samples * sequence_length), [num_samples, sequence_length, num_features]);
My initial layer structure is as following:
% Define the CNN-LSTM model
layers = [
sequenceInputLayer(num_features, 'MinLength', sequence_length)
convolution1dLayer(3, 64, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2, 'Stride', 2)
convolution1dLayer(3, 128, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2, 'Stride', 2)
convolution1dLayer(3, 256, 'Padding', 'same')
batchNormalizationLayer
reluLayer
lstmLayer(100, 'OutputMode', 'last')
fullyConnectedLayer(64)
reluLayer
dropoutLayer(0.5)
fullyConnectedLayer(2) % 2 output neurons for binary classification
softmaxLayer
classificationLayer
];
With this layer set-up I run into error:
Error using trainNetwork
The training sequences are of feature dimension 1677 but the input layer expects sequences of feature dimension 1.
Error in noise_kernel_deconvolve (line 260)
net = trainNetwork(X_train, Y_train, layers, options);
Since I keep run into dimensionality problem, I changed the layer to :
% Define the CNN-LSTM model
layers = [
sequenceInputLayer([sequence_length, num_features])
convolution1dLayer(3, 64, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2, 'Stride', 2)
convolution1dLayer(3, 128, 'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2, 'Stride', 2)
convolution1dLayer(3, 256, 'Padding', 'same')
batchNormalizationLayer
reluLayer
flattenLayer % Add this layer to flatten the output before LSTM
lstmLayer(100, 'OutputMode', 'last')
fullyConnectedLayer(64)
reluLayer
dropoutLayer(0.5)
fullyConnectedLayer(2) % 2 output neurons for binary classification
softmaxLayer
classificationLayer
];
basically changing the ```sequenceInputLayer(num_features, 'MinLength', sequence_length)``` to accept the 2D matrix format of my input data. After this I encountered new error:
Error using trainNetwork
The training sequences are of feature dimension 1677 1000 but the input layer expects sequences of feature dimension 1000 1.
Error in noise_kernel_deconvolve (line 230)
net = trainNetwork(X_train, Y_train, layers, options);
I am now a bit confused how does LSTM require the input data and the layer to be structured... since it seems doesn't matter if I have 2D input X or 3D input ```X```, I would always encounter the dimension problem when I start running the
net = trainNetwork(X_train, Y_train, layers, options);

Antworten (1)

Ayush Aniket
Ayush Aniket am 31 Aug. 2024 um 4:57
Hi Jingyi,
The input format required by the LSTM network in MATLAB for dataset of sequences is a Nx1 cell array where each element is a c-by-s matrix, where c is the number of features of the sequence and s is the sequence length. Refer to the following document link to read about various input formats: https://www.mathworks.com/help/deeplearning/ref/trainnetwork.html#mw_36a68d96-8505-4b8d-b338-44e1efa9cc5e
In your first approach, the correct format for X_Train would be a cell array with samples x 1 (i.e. 2097 x 1) dimension, wherein each entry should be in the fromat of features x sequence_length (i.e. 1 x 1000).

Produkte


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by