Error when using lstm with cnn

조회 수: 2 (최근 30일)
Mohammed Firas
Mohammed Firas 2024년 7월 19일
답변: Kaustab Pal 2024년 8월 20일
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
Walter Roberson
Walter Roberson 2024년 7월 19일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Kaustab Pal
Kaustab Pal 2024년 8월 20일
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.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by