Error using trainNetwork (line 184): the order of the class names of layer 5 must match the order of the class names of the training data
12 views (last 30 days)
Show older comments
Dear community,
I’m developing a Sequence-to-Sequence Classification algorithm (Matlab 2021a). I’ve created 105 sequences (with all of the same length for now, but this could change in the future). I’ve added the data. Each sequence contains between 1 or 4 different classes; ‘No Leak’ ‘A’ ‘B’ ‘C’ and each class have a different amount of entries). So each sequence could contain one or more of these classes and the 'order' in which they appear in the sequence is not fixed. So for example: 'No Leak', 'No Leak', 'No Leak', 'A', 'No Leak', 'No Leak','C','C','C', 'No Leak', 'No Leak'
I want to include weights for the different classes to further optimize my algorithm. Without including the predefined classes and there weights my algorithm works. However, when I add ‘classes’ and ‘classWeights’ I get the following error:
Error using trainNetwork (line 184):
The order of the class names of layer 5 must match the order of the class names of the training data. To get the class names of the training data, use the categories function.
Error in MLA_v2 (line 42)
net = trainNetwork(leak_train_feat,leak_train_cat,layers,options);
My architecture:
inputSize = 1;
numHiddenUnits = 40;
numClasses = 4; numFeatures = 1;
classes = ["No Leak" "A" "B" "C"];
classWeights = [0.1 0.3 0.3 0.3];
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer('Classes',classes,'ClassWeights',classWeights)];
maxEpochs = 10;
miniBatchSize = 27;
options = trainingOptions('adam', ...
'ExecutionEnvironment','cpu', ...
'GradientThreshold',1, ...
'MaxEpochs',maxEpochs, ...
'MiniBatchSize',miniBatchSize, ...
'SequenceLength','longest', ...
'Shuffle','never', ...
'Verbose',0, ...
'Plots','training-progress');
net = trainNetwork(leak_train_feat,leak_train_cat,layers,options);
I think the problem is the fact that the order of the classes in the trainingsdata is not exactly the same as the predefined one;
Sequence 1:
{'C' }
{'No Leak'}
{'B' }
Sequence 2:
{'C' }
{'No Leak'}
{'A' }
{'B' }
Sequence 4:
{'B' }
{'No Leak'}
{'A' }
Is there a way to deal with this problem?
0 Comments
Accepted Answer
yanqi liu
on 5 Jan 2022
yes,sir,please check data
clc; clear all; close all;
load example
leak_train_cat2 = [];
for i = 1 : length(leak_train_cat)
leak_train_cat2{i,1} = char(categorical(leak_train_cat{i,1}(1)));
end
inputSize = 1;
numHiddenUnits = 40;
numClasses = 4; numFeatures = 1;
leak_train_cat2 = categorical(leak_train_cat2);
classes = categories(leak_train_cat2)
classes =
1×1 cell 数组
{'No Leak'}
only one class
More Answers (0)
See Also
Categories
Find more on Custom Training Loops 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!