onehotencoder for LSTM sequence to label classification

10 views (last 30 days)
Hi everyone,
I want to ask you to give me an example on how I should prepare the data with onehotencoder for a sequence to label classification with LSTM NNs.
The task is similar to https://it.mathworks.com/help/deeplearning/ug/classify-sequence-data-using-lstm-networks.html. I have N sequences (all of them of the same length) that has to be classified into 2 categories, so a N-by-1 cell array of sequences. I have already succesfully trained a network using a N-by-1 cell array for the labels, which are "1" and "2". Now I would like to try the onehotencoder, but, also if I follow all the instructions, I can't start the training. I would like to ask you how to prepare the labels with the onehotencoder.
This is what I have arleady tried:
label=["1";"1";"2";"2"];
label=categorical(label);
label_=onehotencode(label,2);
But I get the error "Invalid training data. Predictors must be a N-by-1 cell array of sequences, where N is the number of sequences. All sequences must have the same feature dimension and at least one time step.", since I have a N-by-1 cellarray for the sequences and a N-by-2 array for labels.
Then I tried:
label=["1";"1";"2";"2"];
classes=["1", "2"];
label=onehotencode(label,"ClassNames",classes);
but I get the "Invalid training data. Predictors and responses must have the same number of observations." error (which I get usually if the total number of sequences and of the labels are different). The table that I get is a N-by-2 table.
I don't get how to use the onehotencoder to prepare these labels. Could you provide an example of arrays (or tables, I don't know) that has to be provided to the LSTM NN? I just would like to understand the structure needded to let the pc understand how the labels are correlated wth the correspective sequences.
Thanks you all!
Floriana
Additional info: the net that I use is
inputSize = 1;
numClasses = 2;
layers = [ ...
sequenceInputLayer(inputSize)
bilstmLayer(numHiddenUnits,'OutputMode','last')
dropoutLayer(0.5,"Name","dropout")
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
and it worked previously, with the classes '1' and '2'. I just would like to use the onehotencoder.

Answers (1)

Binaya
Binaya on 6 Oct 2023
Hi Floriana,
Based on your description, it appears that you would like your classification layer to be compatible with one-hot encoded labels.
Upon reviewing the documentation for the Classification layer in MATLAB, it is evident that the classification layer outputs a single categorical label for each instance or sample, represented as a one-dimensional scalar. However, one-hot encoded labels are represented as vectors, which are not directly compatible with the classification layer. To utilize one-hot labels, you will need to use the 'onehotdecode' function to decode the output of the 'softmaxLayer' while removing the 'classificationLayer'. This approach will help resolve the error related to "invalid training data".
For more detailed information, please refer to the following documentation:
I hope this helps.
Regards
Binaya

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!