Error with 1dPooling in dlnetwork

21 次查看(过去 30 天)
Clara
Clara 2024-10-21,20:03
评论: Clara 2024-10-23,18:39
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];

采纳的回答

Taylor
Taylor 2024-10-21,21:15
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 个评论
Taylor
Taylor 2024-10-22,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".
Clara
Clara 2024-10-23,18:39
I was trying to classify the sequence as a whole, so the "last" was in fact the issue here. Thanks a lot !

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2024-10-21,22:05
编辑:Matt J 2024-10-22,1:53
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'))

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by