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

조회 수: 6 (최근 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 CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by