# How to transfer a 1D-CNN from Tensorflow?

7 Ansichten (letzte 30 Tage)
Daniel Papp am 3 Jun. 2022
Kommentiert: Tomaso Cetto am 7 Jun. 2022
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?
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Tomaso Cetto am 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!
Best,
Tomaso
##### 2 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Tomaso Cetto am 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!
Best,
Tomaso

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

David Willingham am 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 KommentarKeine anzeigenKeine ausblenden
Daniel Papp am 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.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Sequence and Numeric Feature Data Workflows finden Sie in Help Center und File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by