How to predict with 1Dconvolution layers: get 3 channels to produce one output?

11 views (last 30 days)
Hello there I am tying to build a model to test what a 1D convolution layer will do to my data. I have 3 input accelerometer mesurements in the x,y,z directions and am trying to predict a continuous output. The Xtrain is a 3x500 double with the 3 input accelerometer mesurements over 500 time steps and the Ytrain is a 1x500 double of the target output variable. The code I have developed is here:
numFilters = 64;
filterSize = 5;
droupoutFactor = 0.005;
numBlocks = 4;
dilationFactor = 1;
net = dlnetwork;
layers = sequenceInputLayer(NumFeatures,Normalization="rescale-symmetric",Name="inputaccelerometers")
convolution1dLayer(filterSize,numFilters,DilationFactor=dilationFactor,Padding="causal",Name="conv1_")
net = addLayers(net,layers)
options = trainingOptions("adam", ...
MaxEpochs=60, ...
miniBatchSize=1, ...
InputDataFormats="CTB", ...
Plots="training-progress", ...
Metrics="rmse", ...
Verbose=0);
net = trainnet(Xtrain',Ytrain',net,"mse",options)
But when I try and train the model I get and error that says: Number of channels in predictions (3) must match the number of channels in the targets (1). Do I need to add another layer to convert the inputs into one predicted output and which layer should I use to do this? Any suggestions on how to remove this error and get the network to run accurately? Thanks so much!

Accepted Answer

Milan Bansal
Milan Bansal on 7 Jun 2024
Hi Isabelle Museck,
The error you're encountering is due to a mismatch between the number of channels in your predictions and the number of channels in your targets. Since your target is a single continuous output (a 1x500 double), you need to ensure that your network's final output layer produces a single output channel.
To resolve this, you can add a fully connected layer with a single output neuron after your convolutional layers. This fully connected layer will map the multi-channel output of the convolutional layers to a single continuous output.
Here's a revised version of your code to include this adjustment:
numFilters = 64;
filterSize = 5;
dropoutFactor = 0.005;
numBlocks = 4;
dilationFactor = 1;
net = dlnetwork;
% Define the layers
layers = [
sequenceInputLayer(3, Normalization="rescale-symmetric", Name="inputaccelerometers")
convolution1dLayer(filterSize, numFilters, DilationFactor=dilationFactor, Padding="causal", Name="conv1_")
reluLayer(Name="relu1_")
fullyConnectedLayer(1, Name="fc")
];
net = addLayers(net,layers)
% Define training options
options = trainingOptions("adam", ...
MaxEpochs=60, ...
MiniBatchSize=1, ...
Plots="training-progress", ...
Metrics="rmse", ...
Verbose=0);
% Train the network
net = trainnet(Xtrain', Ytrain', net,"mse",options );
Hope this helps!

More Answers (0)

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!