1D convolution layer and Understanding Filter Size

16 次查看(过去 30 天)
Hi there I am trying to understand the number of filters parameter within the 1D convolution layer. Why is it that I am not able to set the number of fiilters in this model to 20. I have one feature I am trying to use to predict a continuous variable. Xtrain is the a 1x500 double containing the data from the one feature and Ytrain is the corresponging 1x500 double of the vairavle I am trying to predict. How come I am getting an:
Error using trainnet (line 46)
Number of channels in predictions (20) must match the number of channels in the targets (1).1
It works when I set the number of filters =1 but I dont understand why the number of filters cant be larger? Does the number of filters always have to equal the number of features/inputs? In this example there are 3 input features and 64 filters so I am confused: https://www.mathworks.com/help/deeplearning/ug/sequence-to-sequence-classification-using-1-d-convolutions.html
Any help would be greatly appreciated!
numFilters = 20;
filterSize = 5;
net = dlnetwork;
layers = [
sequenceInputLayer(1, Name="inputaccelerometers")
convolution1dLayer(filterSize,numFilters,Padding="causal", Name="conv1_")
];
net = addLayers(net,layers);
options = trainingOptions("adam", ...
MaxEpochs=60, ...
miniBatchSize=1, ...
InputDataFormats="CTB", ...
Metrics="rmse", ...
Verbose=0);
net = trainnet(Xtrain',Ytrain',net,"mse",options)

采纳的回答

Ayush Aniket
Ayush Aniket 2024-6-13
Hi Isabelle,
The error you are encountering is related to how the output of your network is structured. The number of filters in a convolutional layer determines the number of unique feature maps (or channels) that layer will produce. If you have 20 filters, your layer will output 20 different representations of the input data, each highlighting different features learned by those filters.
When you set the number of filters to 20, the convolutional layer produces an output with 20 channels. This is fine for the feature extraction part of the network. However, the problem arises when this output is passed to the loss function for comparison with your target data ('Ytrain'), which only has 1 channel. The network is trying to compare a 20-channel prediction with a 1-channel target, leading to the error message.
To resolve this, you need to ensure that the final output of your network has the same number of channels (or dimensions in your case) as your target data. This is usually achieved by adding a layer at the end of the network that reduces the number of channels down to match the target. For a regression task, this can often be a fully connected layer with a single output unit as shown below:
layers = [
sequenceInputLayer(1, Name="inputaccelerometers")
convolution1dLayer(filterSize, numFilters, Padding="causal", Name="conv1_")
fullyConnectedLayer(1,Name="fc")
];

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by