Error with 1dPooling in dlnetwork
9 views (last 30 days)
Show older comments
Hi,
I'm having the following error when trying to initialize my dlnetwork :
Layer 'Attention_D': Input data must have two spatial dimensions only, one spatial dimension and one temporal dimension, or two spatial dimensions and one temporal dimension. Instead, it has 0 spatial dimensions and 0 temporal dimensions.
I have seen the work around proposed in https://www.mathworks.com/matlabcentral/answers/1935274-i-m-having-trouble-with-convolution1dlayer, that recommends to use 2d pooling with a poolsize of [ D 1 ] instead of 1d pooling with size D. However, this work-around requires to replace the FeatureInputLayer by an ImageInputLayer.
My problem is : my network aims to classify sequential data, and as a result uses a SequentialInputLayer.
I can't figure how to use the work around on it. Is it possible to use an ImageInputLayer in my case ? My sequences have more than 1 feature/channel.
Or is there another way to deal with this error ?
FYI, my layers are as follow :
layers = [ ...
sequenceInputLayer(numPredictors, Name="Query")
lstmLayer(numHiddenUnits,OutputMode="last", Name="Encoder")
lstmLayer(numHiddenUnits,OutputMode="sequence", Name="Decoder")
averagePooling2dLayer([poolSizeD,1], Name="Attention_D")
fullyConnectedLayer(numClasses, Name="Classifier")
softmaxLayer];
1 Comment
Accepted Answer
Taylor
on 21 Oct 2024
Have you tried working with the Deep Network Designer and Network Analyzer? They're quite useful for debugging network architecture.
In the attached images, you can see that the averagePooling1dLayer is what you want to use. The dimensionality in the name of the average pooling layers refers to the nature of the data coming in via the input layer. Image data is generally referred to as 2D (height x width x numChannel) whereas sequence/signal data is generally referred to as 1D (sequence length x numChannels), omitting the "numChannel" dimenion that occurs in both of them. In the attached images, you can see that I'm assuming sequence input with 10 dimensions or 10 sequences input to the network, but using the averagePooling1dLayer does not yield an error as the averagePooling2dLayer does.
5 Comments
Taylor
on 22 Oct 2024 at 14:02
Looks like I reached my attachment limit and will have to wait 24 hours to attach the images.
I overlooked this earlier, but the reason you are getting the error is that you are setting the first LSTM layer to the output mode "last". This is related to data format/"dimension label" that @Matt J was mentioning. Here is a page that talks about data formats for deep learning in more detail. Setting the ouput mode to "last" for the LSTM layer outputs the last step of the sequence, eliminating the Time dimension. The Average Pooling 1D layer averages over the Time dimension for sequences. Matt's approach is essentially reformatting the data so the Average Pooling 1D layer recognizes it as 1D image data and averages over the Spatial dimension.
My question is whether you are trying to classify each time point of the sequence or the sequence as a whole. In the former case, you will use the output mode "last" from the LSTM layer, and in the latter, you will use "sequence".
More Answers (1)
Matt J
on 21 Oct 2024
Edited: Matt J
on 22 Oct 2024
You can use functionLayers to reformat the activations as the averagePooling layers require, but make sure you set the spatial dimension S to the dimension you intend to pool.
numPredictors=10;
numHiddenUnits=14;
poolSizeD=3;
sequenceLength=20;
numClasses=2;
numBatch=5;
layers = [ ...
sequenceInputLayer(numPredictors, Name="Query")
lstmLayer(numHiddenUnits,OutputMode="last", Name="Encoder")
lstmLayer(numHiddenUnits,OutputMode="sequence", Name="Decoder")
%reformat activation as spatial-batch-channel (SBC)
functionLayer( @(x)dlarray(x, "SBC"), Formattable=true)
averagePooling1dLayer(poolSizeD, Name="Attention_D")
fullyConnectedLayer(numClasses, Name="Classifier")
softmaxLayer];
analyzeNetwork(layers,...
dlarray(rand(sequenceLength,numPredictors,numBatch),'TCB'))
0 Comments
See Also
Categories
Find more on Image Data Workflows 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!