The class names of layer must match the class names of the validation/training data

8 views (last 30 days)
Benjamin Ballintyn
Benjamin Ballintyn on 5 Sep 2019
Commented: Yuri Khudyakov on 25 Jun 2021
Hi,
I am trying to train a bi-directional lstm (although I have had the same issue with normal lstms) to do sequence-to-sequence classification. The layers of the network are:
5x1 Layer array with layers:
1 'sequenceinput' Sequence Input Sequence input with 2 dimensions
2 'biLSTM' BiLSTM BiLSTM with 100 hidden units
3 'fc' Fully Connected 3 fully connected layer
4 'softmax' Softmax softmax
5 'classoutput' Classification Output crossentropyex with '1' and 2 other classes
The inputs are 2D sequences of length 2000 and the targets consist of 3 different classes (also of length 2000, so one class per timestep). I can initially train the layers using a subset (1000-2000 samples) of the training data and the validation data. However, when I then try to again train those already trained layers on a new set of training data, I often (but not always) get the following error:
The class names of layer 5 must match the class names of the training data. The training data class names are given by categories(Y), where Y are the training data labels.
Or sometimes:
The class names of layer 5 must match the class names of the validation data. The validation data class names are given by categories(Y), where Y are the validation data labels.
This confuses me since I get the following output from net.Layers(5).Classes:
ans =
3×1 categorical array
1
3
2
So all of the classes are represented in the last layer's output. I've checked both my training and validation datasets are there are no instances of classes outside of the 1-3 range (and again the layers initially train just fine). However, most of the training/validation examples only contain 1 or 2 of the 3 classes per "datapoint". What am I missing here?

Answers (2)

Benjamin Ballintyn
Benjamin Ballintyn on 7 Sep 2019
Edited: Benjamin Ballintyn on 7 Sep 2019
After a little more playing around, I believe this is a bug. I tried to train with a dataset Xtr and received the above error. Then I simply changed the order of the dataset (the indices of each datapoint) and then it ran fine. This suggests to me that there is a bug in the way MATLAB is finding class labels in the training/validation sets.

Bence Cserkuti
Bence Cserkuti on 6 Dec 2020
Hi Benjamin,
It's great to hear that you found a solution. I faced the same error so I thought I would share how I solved it.
The issue in my case was that the order of the class labels was not the same. Using your example, net.Layers(5).Classes would be
ans =
3×1 categorical array
1
3
2
However, in my training data, when I run categories(train.Labels) I got:
ans =
3×1 categorical array
1
2
3
As you can see, the order of these two are not the same, and that's why the error was thrown.
Please note that this still has to be true even though you might not have any elements with class label 2 in your training dataset.
Hope this will help others.

Community Treasure Hunt

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

Start Hunting!