How to transfer a 1D-CNN from Tensorflow?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Daniel Papp
el 3 de Jun. de 2022
Comentada: Tomaso Cetto
el 7 de Jun. de 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 comentarios
Respuesta aceptada
Tomaso Cetto
el 7 de Jun. de 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 comentarios
Tomaso Cetto
el 7 de Jun. de 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
Más respuestas (1)
David Willingham
el 6 de Jun. de 2022
Hi, are you trying to create the network from scratch? Or does the paper have a link to the TensorFlow code on GitHub?
Ver también
Categorías
Más información sobre Sequence and Numeric Feature Data Workflows en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!