How to transfer a 1D-CNN from Tensorflow?
18 views (last 30 days)
I'm trying to recreate the CNN to MATLAB from this paper: https://doi.org/10.1021/acs.analchem.8b05821. 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?
Tomaso Cetto on 7 Jun 2022
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!