Clear Filters
Clear Filters

How to transfer a 1D-CNN from Tensorflow?

6 views (last 30 days)
I'm trying to recreate the CNN to MATLAB from this paper: In the code uploaded to GitHub, the authors use a series on Conv1D layers which is equivalent to MATLAB's convolution1dLayer. Their inputs are 250x36 onehot encoded matrices. The network slides convolutional filters along the longer dimension of this matrix to extract features and use them for regression purposes.
However, when I try to feed the network similar matrices, I receive an "Input data must have one spatial dimension only" error message at the first convolution layer. Am I supposed to flatten the input matrix first before the convolutional layer? I didn't find any evidence for such an operation in the code. Or is there any workaround to this using a 2D convolution layer?

Accepted Answer

Tomaso Cetto
Tomaso Cetto on 7 Jun 2022
Hi Daniel!
I think the convolution1dLayer is appropriate here, but you might need to do a small piece of data pre-processing to get this to work.
You say your inputs are '250x36 onehot encoded matrices' - presumably this dimension layout corresponds to numFeatures x numObservations. This isn't working with the convolution1dLayer because that layer expects data with three dimensions: the two you have, plus a continuous space or time dimension (that is, it wants data in the format spatialDim x numFeatures x numObservations OR numFeatures x numObservations x numTimeSteps). It is this continuous space or time dimension that the layer will convolve over.
So all you need to do is ensure that the dimension you want to convolve over (the one of size 250) is in the appropriate position. The easiest thing to do is probably to reformat your data as sequences. The fact your data doesn't actually represent a time series doesn't make a difference here, it's just so we can convolve over the right dimension. You can then use a sequenceInputLayer as your input layer - this will require you to put your data into a cell array with numObservations cells. Each cell needs contains one observation of size numFeatures x numTimeSteps. So here we need to make 250 the numTimSteps, and we can just have numFeatures as 1. The line of of code below will do that for you, if your data is a matrix X of size 250x36:
X = rand(250,36);
cellData = num2cell(X',2);
After this, you should be good to go!
Let me know if this makes sense and if you have any other questions, I'd be happy to answer them!
Daniel Papp
Daniel Papp on 7 Jun 2022
Hej Tomaso,
Thanks for the answer, it gave me a great insight how conv1dLayer works. Still, I believe that my data structure is a bit misunderstood. Let me clarify it.
I onehot-encode strings (corresponding to observations) to 250x36 matrices so I end up with "images" with the size of 250x36 pixels. I have 1000 of such images which I split into training and validation sets. What I want my network to do is to convolve over the height dimension of the image (instead of the convolution filter moving in 2D, such as in image analysis). So if I follow your suggestion it'd be the transposed data (250x36 matrices) in a cell array, each cell containing one observation, right?
Tomaso Cetto
Tomaso Cetto on 7 Jun 2022
Hi Daniel,
Thanks for the clarifications there - I did indeed misunderstand your data shape, sorry about that! If what you in fact have is standard 2D images, and you only want to convolve over one dimension of those images, then the preferred approach is to use convolution2dLayer with a singleton filter size in the dimension you don't want to convolve over. So lets say you want to convolve over the first dimension with 16 filters of size of 3, then your layer will look as follows:
convolution2dLayer([3 1], 16)
That singleton filter in the second dimension will effectively cause a no-op, ignoring the convolution in that dimension. Using this apporach means you can stick with the imageInputLayer as your input layer, and forget all about the cell arrays!

Sign in to comment.

More Answers (1)

David Willingham
David Willingham on 6 Jun 2022
Hi, are you trying to create the network from scratch? Or does the paper have a link to the TensorFlow code on GitHub?
If they do, one option is to try the importTensorFlowNetwork function if they do.
  1 Comment
Daniel Papp
Daniel Papp on 6 Jun 2022
I'm trying to recreate the network from scratch, both for the learning experience and because I'm absolutely not fluent in TensorFlow. They do have the code on GitHub though, so I can use the layer properties from there.

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!