
CNN and LSTM error with input size
3 views (last 30 days)
Show older comments
Hi,
I am building the following network
layers = [
sequenceInputLayer([13, 13,1],'Name','input')
sequenceFoldingLayer
convolution2dLayer(5,8)
batchNormalizationLayer
reluLayer
convolution2dLayer(5,16)
batchNormalizationLayer
reluLayer
convolution2dLayer(5,32)
batchNormalizationLayer
reluLayer
convolution2dLayer(5,64)
batchNormalizationLayer
reluLayer
sequenceUnfoldingLayer
flattenLayer
bilstmLayer(vars.numHiddenUnits,'OutputMode', 'sequence')
fullyConnectedLayer(100)
dropoutLayer(0.4)
fullyConnectedLayer(1)
regressionLayer];
I train the network with X and Y of sizes
size(X):
13 13 1 52
size(Y):
52 1
Ans I get the errors "Invalid training data. X and Y must have the same number of observations.". I have backtracted the error and it fails in file "NetworkDataValidator.m" line 858 because ismatrix(x) is False. Given that x is a 4D matrix.
I can't understand what I am doing wrong. Any help is greatly appreciated.
Best regards,
Annalisa
0 Comments
Answers (1)
Srivardhan Gadila
on 9 Apr 2020
Edited: Srivardhan Gadila
on 16 Apr 2020
Latest Edit:
In order to model the LSTM regression networks with 2-D data, the input should be a Nx1 cell, N being the number of observations. Each cell entry should then comprise a HxWxCxS array, where H = height, W=width, C=channels and S= sequence length. The responses, Y, can be a NxR matrix, where N = observations, R = number of responses (or output of the network) for sequence-to-one problems or a Nx1 cell array of RxS responses for sequence-to-sequence problems.
If in the above script S=54 and it's a sequence-to-sequence task with N=1, then the fix is simple:
X = {rand(13,11,1,54)};
Y = {rand(1,54)};
net = trainNetwork(X,Y,lgraph,options);
If N=54, and it's a sequence-to-one task, then I see two issues:
1. I don't see the S dimension. Assuming N=54 is the number of observations, I see only 54 arrays of 13x11x1 dimensions but I don't see a sequence of these arrays.
2. The 'OutputMode' of the bilstmLayer is set to 'sequence'. This would correspond to a sequence-to-sequence problem but the responses are only Nx1, so I assume the 'OutputMode' needs to be changed to 'last' to correspond to a sequence-to-one task.
For example, if you change the input and response to (where S=10):
X = arrayfun(@(x)rand(13,11,1,10),1:54,'UniformOutput',false)';
Y = rand(54,1);
Where, H=13,W=11,C=1,S=10,N=54,R=1, and change the OutputMode to 'last', then you can train the network.
bilstmLayer(100,'OutputMode', 'last','Name', 'bilstm')
Earlier suggestion:
Your layers itself has some errors, refer to the attached image for the errors. Use analyzeNetwork to check for the errors in your network as follows:
analyzeNetwork(layers)
I would suggest you to fix the errors in your network and then train the network. Since I don't have the value of the variable vars.numHiddenUnits, I used the value 200 for the bilstmLayer.
Refer to the example Train Convolutional Neural Network for Regression, and check the size of XTrain & YTrain to get an idea on what should be the dimension of the input data and the target data.

5 Comments
Srivardhan Gadila
on 16 Apr 2020
HONGYI LI
on 29 Sep 2022
For N=54 and it's a sequence-to-one task,only 54 arrays of [13 13 1] is correct if S=1.
See Also
Categories
Find more on Deep Learning Toolbox 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!