MATLAB Answers

Example does not work for binary classification..

3 views (last 30 days)
Hello,
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).
Regards,
Oytun GUNES
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 [1] and [2]. 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
Xtrainnew=XTrain(1:60,:);
Ytrainnew=YTrain(1:60,:);
%
% 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 = [ ...
sequenceInputLayer(inputSize)
lstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
% 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', ...
'ExecutionEnvironment','cpu', ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'GradientThreshold',1, ...
'Plots','training-progress');
% Train the LSTM network with the specified training options.
net = trainNetwork(Xtrainnew,Ytrainnew,layers,options);

  0 Comments

Sign in to comment.

Accepted Answer

Hrishikesh Borate
Hrishikesh Borate on 18 Jun 2020
Hi,
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
categories(YTrainnew)
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

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by