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 ビュー (過去 30 日間)
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?

採用された回答

yanqi liu
yanqi liu 2022 年 1 月 5 日
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
  1 件のコメント
Bram Stegeman
Bram Stegeman 2022 年 1 月 5 日
Hi,
Yes thank you. I solved the problem.
The issue was in the sequences; not every sequence contains all 4 classes (as already indicated).
I solved it with by tempory adding all classes (at a location in your sequence) and reorder the catagories (in the same order at which they are defined at the network):
%add extra catagories
for i=1:length(leak_train_cat)
leak_train_cat{i,1}(1,1)= 'A';
leak_train_cat{i,1}(1,1)= 'No Leak';
leak_train_cat{i,1}(1,2)= 'B';
leak_train_cat{i,1}(1,2)= 'No Leak';
leak_train_cat{i,1}(1,3)= 'C';
leak_train_cat{i,1}(1,3)= 'No Leak';
leak_train_cat{i,1}=reordercats(leak_train_cat{i,1},{'No Leak','A','B','C'});
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCustom Training Loops についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by