Can't use a Validation set when training a sequence-to-sequence BiLSTM Classification model
Show older comments
I am trying to train a sequence-to-sequnce classifcation model, and i use a BiLSTM layer, with Data and labels, X and Y respectively. I am getting the following error:
Error using trainNetwork (line 184)
Training and validation responses must have the same categories. To view the categories of the
responses, use the categories function.
Error in my_DNN_script (line 146)
[net , netInfo] = trainNetwork(X,Y,layers,options);
Caused by:
Error using nnet.internal.cnn.trainNetwork.DLTDataPreprocessor>iAssertClassNamesAreTheSame (line
213)
Training and validation responses must have the same categories. To view the categories of the
responses, use the categories function.
--------------------------------------------------------------------------
I set a breakpoint at the corresponding line in nnet.internal.cnn.trainNetwork.DLTDataPreprocessor>iAssertClassNamesAreTheSame (line 213).
function iAssertClassNamesAreTheSame(trainingCategories, validationCategories)
% iHaveSameClassNames Assert that the class names for the training and
% validation responses are the same.
trainingClassNames = categories(trainingCategories);
validationClassNames = categories(validationCategories);
if ~isequal(trainingClassNames, validationClassNames)
error(message('nnet_cnn:trainNetwork:TrainingAndValidationDifferentClasses'));
end
end
the problem appears to be an ordering problem. I printed the outputs of the variables trainingClassNames and validationClassNames . The number of classes is the same, but the order is different
>> trainingClassNames =
13×1 cell array
{'2' }
{'3' }
{'4' }
{'5' }
{'6' }
{'7' }
{'8' }
{'9' }
{'10'}
{'11'}
{'12'}
{'1' }
{'0' }
>> validationClassNames
validationClassNames =
13×1 cell array
{'0' }
{'1' }
{'3' }
{'4' }
{'5' }
{'6' }
{'7' }
{'8' }
{'9' }
{'10'}
{'11'}
{'12'}
{'2' }
I modifed the function nnet.internal.cnn.trainNetwork.DLTDataPreprocessor>iAssertClassNamesAreTheSame:
I used the function reordercats to do so:
function iAssertClassNamesAreTheSame(trainingCategories, validationCategories)
% iHaveSameClassNames Assert that the class names for the training and
% validation responses are the same.
trainingClassNames = categories(reordercats(trainingCategories));
validationClassNames = categories(reordercats(validationCategories));
if ~isequal(trainingClassNames, validationClassNames)
error(message('nnet_cnn:trainNetwork:TrainingAndValidationDifferentClasses'));
end
end
With this modification, the trainnet function ran without an error.
Could you please tell me if this modification should be flowless, of if it could be leading to a hidden, wrong training behaviour
1 Comment
Steve Philbert
on 12 Oct 2023
I am training a classification network with k-fold cross-validation and ran into this same error. When the training and validation categories are reordered, as shown above, their values are equal.
Accepted Answer
More Answers (0)
Categories
Find more on Gaussian Process Regression 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!