How to prepare data for Convolutional1dlayer for spectra analysis in Regression Problem

25 views (last 30 days)
Hi,
I am Machine Learning beginner and trying to do a calibration using MATLAB. The calibration will take 656 spectra (observations) in total, with each spectra having 242 data point (feature), and trained with supervised learning given their individual true value, and then be able to Estimate given an unknown true value's spectrum.
I stucked at choosing whether to have sequenceInputLayer or featureInputLayer
From what I read, I should choose featureInputLayer because my features are not a timeseries data, they are just intensities at different wavelength. However, with the layers I have below, I was told to have 0 spatial dimension for my Convolutional1dlayer.
layers = [
featureInputLayer(numFeatures,"Name","input","Normalization","zscore")
convolution1dLayer(30,50,"Name","conv1d1","Padding","same")
maxPooling1dLayer(5,"Name","maxpool1d1","Padding","same")
tanhLayer("Name","relu_2")
convolution1dLayer(10,100,"Name","conv1d2","Padding","same")
maxPooling1dLayer(10,"Name","maxpool1d2","Padding","same")
tanhLayer()
fullyConnectedLayer(50,"Name","fc1")
sigmoidLayer()
fullyConnectedLayer(12,"Name","fc2")
fullyConnectedLayer(1,"Name","fc3")
regressionLayer("Name","regressionoutput")];
options = trainingOptions("adam", ...
LearnRateSchedule="piecewise", ...
LearnRateDropFactor=0.2, ...
LearnRateDropPeriod=5, ...
MaxEpochs=300, ...
MiniBatchSize=10)
Error in Command Window
Error using trainNetwork
Invalid network.
Error in G_CNN_model_Si (line 56)
net=trainNetwork(xTrain,yTrain_m,layers,options);
Caused by:
Layer 'conv1d1': Input data must have one spatial dimension only, one temporal dimension only, or one of each. Instead, it
has 0 spatial dimensions and 0 temporal dimensions.
However, if I replace featureInputLayer with SequenceInputLayer, It has no problem. It was able to get through and give me prediction. But SequenceInputLayer is for timeseries data. I am confused why
The Training dataset were prepared in format numerical array and cell, both failed for featureInputLayer.
Let me know how to prepare Training dataset for conv1dlayer. Many Thanks.
Update: I tried with dlarray but it didn't work out,
dlarray(xTrain,'SCB')
dlarray(xTrain,'SB')
dlarray(xTrain,'SBC')
All failed

Answers (1)

Himanshu
Himanshu on 28 Sep 2023
Hello,
I understand that you are facing an error while preparing your data for a convolutional 1D layer in MATLAB for a regression problem.
The error occurred because the input layer is not compatible with the structure of your data. Convolutional 1D layers in MATLAB expect data with one spatial dimension, one temporal dimension, or one of each. However, your data doesn't have any of these dimensions when it is being processed through the "featureInputLayer".
Use "sequenceInputLayer" when dealing with sequential data, such as time series or text data, and the order of observations or features matters for the task at hand. Use "featureInputLayer" when working with non-sequential data, like images, audio, or tabular data, and the order of features is not significant for the task.
Since your data consists of spectra with 242 data points each, using a "featureInputLayer" is more appropriate. This layer is suitable for non-timeseries data, such as your intensity values at different wavelengths. Ensure that your input data is in the correct format. For a "featureInputLayer", you should have a 3D array with dimensions [numFeatures, numObservations, 1]. In your case, "numFeatures" is 242, and "numObservations" is the number of spectra, 656.
To correctly convert your data for use with a featureInputLayer, you can reshape your data as follows:
xTrain = reshape(xTrain, [numFeatures, numObservations, 1]);
You can refer to the documentation below to understand more about the "sequenceInputLayer" and the "featureInputLayer" layers in MATLAB.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!