LSTM input size mismatch

7 views (last 30 days)
Xiaoran Li
Xiaoran Li on 22 May 2020
Answered: Srivardhan Gadila on 26 May 2020
Hi,
I am meeting a trouble when I want to build a network with CNN and LSTM. Basically, I learn and follow from this link: https://uk.mathworks.com/help/deeplearning/ug/classify-videos-using-deep-learning.html. my dataset has 2000 samples (each sample contains 40 points), when I test my custom network, there exists an error on the LSTM layer:
Layer 'lstm': Input size mismatch. Size of input to this layer is different from the expected input size.
Inputs to this layer:
from layer 'flatten' (output size 100)
Attached please find the analysis of my network architecture

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 26 May 2020
With the following code I was able to construct layerGraph with no errors. Make sure you define the lstmLayer with right input arguments.
inputSize = [1 40 1];
filterSize = [1 3];
numFilters = 5;
numHiddenUnits = 25;
numClasses = 2;
layers = [ ...
sequenceInputLayer(inputSize,'Name','input')
sequenceFoldingLayer('Name','fold')
convolution2dLayer(filterSize,numFilters,'Name','conv','stride',[1 1],'padding','same')
maxPooling2dLayer([1 2],'padding',[0 0 0 0],'Name','maxpool','Stride',[1 2])
sequenceUnfoldingLayer('Name','unfold')
flattenLayer('Name','flatten')
lstmLayer(numHiddenUnits,'OutputMode','last','Name','lstm')
fullyConnectedLayer(numClasses, 'Name','fc')
softmaxLayer('Name','softmax')
classificationLayer('Name','classification')];
lgraph = layerGraph(layers);
lgraph = connectLayers(lgraph,'fold/miniBatchSize','unfold/miniBatchSize');
analyzeNetwork(lgraph)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!