Example does not work for binary classification..
7 views (last 30 days)
Show older comments
OYTUN GUNES on 17 Jun 2020
Answered: Hrishikesh Borate on 18 Jun 2020
I have modified the example of Sequence Classification with LSTM on MATLAB, by modifying data and changing number of class but I am having the following error, could you help?
Invalid training data. The output size (2) of the last layer does not match the number of classes (9).
PhD Student at Bilkent University
clear all; clc;
% Train Network for Sequence Classification
% Train a deep learning LSTM network for sequence-to-label classification.
% Load the Japanese Vowels data set as described in  and . XTrain is a cell array containing 270 sequences of varying length with a feature dimension of 12. Y is a categorical vector of labels 1,2,...,9. The entries in XTrain are matrices with 12 rows (one row for each feature) and a varying number of columns (one column for each time step).
[XTrain,YTrain] = japaneseVowelsTrainData;
% select only 2 class speaker 1 and speaker 2
% Define the LSTM network architecture. Specify the input size as 12 (the number of features of the input data). Specify an LSTM layer to have 100 hidden units and to output the last element of the sequence. Finally, specify nine classes by including a fully connected layer of size 9, followed by a softmax layer and a classification layer.
inputSize = 12;
numHiddenUnits = 100;
numClasses = 2;
layers = [ ...
% Specify the training options. Specify the solver as 'adam' and 'GradientThreshold' as 1. Set the mini-batch size to 27 and set the maximum number of epochs to 100.
% Because the mini-batches are small with short sequences, the CPU is better suited for training. Set 'ExecutionEnvironment' to 'cpu'. To train on a GPU, if available, set 'ExecutionEnvironment' to 'auto' (the default value).
maxEpochs = 100;
miniBatchSize = 10;
options = trainingOptions('sgdm', ...
% Train the LSTM network with the specified training options.
net = trainNetwork(Xtrainnew,Ytrainnew,layers,options);
Hrishikesh Borate on 18 Jun 2020
It’s my understanding that you are trying to construct a binary classifier using Sequence Classification with LSTM on MATLAB.
In the code, YTrain and hence, Ytrainnew is a categorical variable, and if you try running
the following will be output: -
So, although you are selecting labels corresponding to classes 1 and 2, but there is a possibility that they can take the values from 1 to 9 as shown above.
The solution is: -
YTrainnew = categorical([ones(30,1);2*ones(30,1)]);
For more information, refer to Categorical Documentation
More Answers (0)
Find more on Pattern Recognition and Classification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!