Error when using lstm with cnn

2 views (last 30 days)
Mohammed Firas
Mohammed Firas on 19 Jul 2024
Answered: Kaustab Pal on 20 Aug 2024
XTrain = single(DL_input_reshaped(:,1,1,Training_Ind));
YTrain = single(DL_output_reshaped(1,1,:,Training_Ind)); XValidation = single(DL_input_reshaped(:,1,1,Validation_Ind));
YValidation = single(DL_output_reshaped(1,1,:,Validation_Ind));
YValidation_un = single(DL_output_reshaped_un);
%% DL Model definition with adjusted pooling and convolution layers layers = [ imageInputLayer([size(XTrain,1), 1, 1],'Name','input','Normalization','none')
convolution2dLayer(3, 64, 'Padding', 'same', 'Name', 'conv1')
batchNormalizationLayer('Name', 'bn1')
reluLayer('Name', 'relu1')
maxPooling2dLayer([3,1], 'Stride', [3,1], 'Name', 'maxpool1')
convolution2dLayer(3, 128, 'Padding', 'same', 'Name', 'conv2')
batchNormalizationLayer('Name', 'bn2')
reluLayer('Name', 'relu2')
maxPooling2dLayer([3,1], 'Stride', [3,1], 'Name', 'maxpool2')
convolution2dLayer(3, 256, 'Padding', 'same', 'Name', 'conv3')
batchNormalizationLayer('Name', 'bn3')
reluLayer('Name', 'relu3')
maxPooling2dLayer([3,1], 'Stride', [3,1], 'Name', 'maxpool3')
flattenLayer('Name', 'flatten') % Flatten to 1D per sample
lstmLayer(200, 'OutputMode', 'last', 'Name', 'lstm1') % LSTM layer
fullyConnectedLayer(512, 'Name', 'fc1')
reluLayer('Name', 'relu4')
dropoutLayer(0.5, 'Name', 'dropout1')
fullyConnectedLayer(1024, 'Name', 'fc2')
reluLayer('Name', 'relu5')
dropoutLayer(0.5, 'Name', 'dropout2')
fullyConnectedLayer(2048, 'Name', 'fc3')
reluLayer('Name', 'relu6')
dropoutLayer(0.5, 'Name', 'dropout3')
fullyConnectedLayer(size(YTrain,3), 'Name', 'fc4')
regressionLayer('Name', 'output') ];
options = trainingOptions('rmsprop', ...
.
.
.
so this error is appear to me
((error useing trainNetwork Invalid training data.
The output size (1024) of the last layer does not match the response size (1).))
so the size or XTrain and YTrain is (features x 1 x 1 x minbatchsize)
  1 Comment
Walter Roberson
Walter Roberson on 19 Jul 2024
XTrain = single(DL_input_reshaped(:,1,1,Training_Ind));
You are training with (something by 1 by 1 by something-else) data.
The networks probably expect (something by something-else) -- 2D data instead of 4D data.

Sign in to comment.

Answers (1)

Kaustab Pal
Kaustab Pal on 20 Aug 2024
It appears you're facing an issue due to a size mismatch between the output of your final fully connected layer and the expected response size in YTrain. Specifically, the network's final layer is generating an output of size 1024, while the expected size is 1.
To resolve this, please ensure that YTrain is appropriately structured for regression tasks. It should have dimensions [1, 1, numResponses, miniBatchSize]. If numResponses is 1, the final fully connected layer should produce a single output value per sample.
To correct this, modify the final layer named "fc4" to output one value per sample by using the following configuration:
fullyConnectedLayer(1, 'Name', 'fc4')
I hope this resolves the issue.

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!