Cannot assemble a convolutional network with output sequence length less than input sequence length
Show older comments
I have defined a simple CNN with one convolutional layer. When the stride is set to "1" and the "padding" is set to "same" the network can be assembled using assembleNetwork. However, when the stride is set to something greater than one (I have set it to "2" in the snippet below), or the padding is set to [0 0] (i.e. no padding), assembleNetwork complains that:
Network: Incompatible input and output sequence lengths. The network must return sequences with the same length as the input data or a sequence with length one.
This is correct; the output sequence is shorter than the input sequence by design. One can imagine a network in which a longer stride is used to reduce computational burden, and it is expected that the output will be shorter than the input... and the network can be built and trained in Keras/Tensorflow (in fact, the problem was initiallly encountered when using the importKerasNetwork function on a similar network).
Here's the code for the simple CNN:
%% a minimal cnn
clc;
clearvars;
% input layer
layers(1) = sequenceInputLayer(1, MinLength=8);
% convolutional layer spec
filter_length = 4;
num_channels = 1;
num_filters = 1;
% Layer weights for the transposed convolution operation, specified as a FilterSize-by-NumChannels-by-numFilters numeric array. (Matlab docs: convolution1dLayer)
w = reshape(1:filter_length * num_channels * num_filters, filter_length, num_channels, num_filters);
% Layer biases for the transposed convolutional operation, specified as a 1-by-NumFilters numeric array. (Matlab docs: convolution1dLayer)
b = reshape(1:num_filters(1), 1, num_filters(1));
% conv1-1
layers(2) = convolution1dLayer(filter_length(1), num_filters(1), Name='conv1', NumChannels=num_channels(1), Stride=2, Padding='same', Weights=w, Bias=b);
% activation
layers(3) = reluLayer;
% output
layers(4) = regressionLayer;
% net
net = assembleNetwork(layers);
I have tried using different input layer types (which are not subject to this sequence length check) to no avail. Neither the featureInputLayer or ImageInputLayer are accepted as valid inputs to a convolution1DLayer. I have also tacked on a fullyConnectedLayer at the end to reduce the sequence length to 1, and this does work, but is not the network I want.
Thanks in advance for any feedback. I'll be curious to see if others have run into the same issue.
Accepted Answer
More Answers (0)
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!