CNN-LSTM in MATLAB
40 views (last 30 days)
Show older comments
I'm trying to implement a CNN + LSTM, but I have an error:
Invalid training data. If the network outputs sequences, then regression responses must be a cell array
of numeric sequences, or a single numeric sequence.
or
Invalid training data. Responses must be a matrix of numeric responses, or a N-by-1 cell array of
sequences, where N is the number of sequences. The feature dimension of all sequences must be the same.
or
Invalid training data. X and Y must have the same number of observations.
what is my mistake?
my code is:
inputSize = [20 25 3];
filterSize = 5;
numFilters = 20;
numHiddenUnits = 5;
numResponses = 1;
layers = [ ...
sequenceInputLayer(inputSize,'Name','input')
sequenceFoldingLayer('Name','fold')
convolution2dLayer(filterSize,numFilters,'Padding','same','Name','conv')
batchNormalizationLayer('Name','bn')
reluLayer('Name','relu')
sequenceUnfoldingLayer('Name','unfold')
flattenLayer('Name','flatten')
lstmLayer(numHiddenUnits,'OutputMode','sequence','Name','lstm')
fullyConnectedLayer(1, 'Name','fc')
regressionLayer('Name','regression')];
lgraph = layerGraph(layers);
lgraph = connectLayers(lgraph,'fold/miniBatchSize','unfold/miniBatchSize');
options = trainingOptions('adam', ...
'MaxEpochs',100, ...
'GradientThreshold',1, ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(input_train,target_train,lgraph,options);
My input_train is a 4-D double that its size is (20,25,3,58) in other words I have 3 sets of images that each set containes 58 set of images with size of 20x25
and when I set target_train a matrix of (58,500) or a 4-D double that its size is (1,500,1,58) or a 4-D double that its size is (1,20,25,58) and
fully connected layer to fullyConnectedLayer(500, 'Name','fc') or fullyConnectedLayer(1, 'Name','fc') or fullyConnectedLayer([20 25], 'Name','fc') the error is:
in all cases the error is:
"Invalid training data. Sequence responses must have the same sequence length as the corresponding
predictors."
2 Comments
Answers (1)
H W
on 3 Nov 2022
添加层分支
将网络分支添加到层次图中。每个分支均为一个线性层组。
tempLayers = [
sequenceInputLayer([20 25 3],"Name","sequence")
sequenceFoldingLayer("Name","seqfold")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
convolution2dLayer([5 5],32,"Name","conv","Padding","same")
batchNormalizationLayer("Name","batchnorm")
reluLayer("Name","relu")];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
sequenceUnfoldingLayer("Name","sequnfold")
flattenLayer("Name","flatten")
lstmLayer(5,"Name","lstm")
fullyConnectedLayer(1,"Name","fc")
regressionLayer("Name","regressionoutput")];
lgraph = addLayers(lgraph,tempLayers);
% 清理辅助变量
clear tempLayers;
连接层分支
连接网络的所有分支以创建网络图。
lgraph = connectLayers(lgraph,"seqfold/out","conv");
lgraph = connectLayers(lgraph,"seqfold/miniBatchSize","sequnfold/miniBatchSize");
lgraph = connectLayers(lgraph,"relu","sequnfold/in");
0 Comments
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!