MATLAB Answers

cui
0

Why is the training set and the verification set label order different when training a deep network?

Asked by cui
on 8 May 2019
Latest activity Answered by David Prabhu on 8 Jul 2019
When training my network, the training set and the verification set are from different data, but the tag categories are the same, that is, the order is inconsistent. Is this a problem?
TrainClasses and ValClasses ,the categories are the same, only the order is inconsistent( use isequal function is false), as shown below
labels.png
...
TrainClasses = categories(labelsTrain)
ValClasses = categories(labels_val)
options = trainingOptions(...
...
'ValidationData',{sequencesValidation,labels_val}, ...
...
);
net = trainNetwork(X,labelsTrain,layers,options);
...
and then train the network,get a error,
Incorrect use of trainNetwork (line 165)
Training and validation responses must have the same categories. To view the categories of the responses, use the categories function.
Error myCNN2 (line 135)
[netLSTM,info] = trainNetwork(sequencesTrain,labelsTrain,layers,options);
the reason:
    Incorrect use of trainNetwork>iAssertClassNamesAreTheSame (line 413)
    Training and validation responses must have the same categories. To view the categories of the responses, use the categories function.
Do you need to sort the category tags before you can train, which sounds illogical? so I add "reordercats" function in built-in "trainNetwork"function ,it works,is right?
add.png

  0 Comments

Sign in to comment.

Products


Release

R2019a

1 Answer

Answer by David Prabhu on 8 Jul 2019

I had a similar issue, but I was able to resolve mine.
In my case, I read both my training and validation images into an image datastore. Initially, my datastores had no labels, so I had to create the labels for each datastore.
When I created the labels, I tried to pre-allocate a vector of data type 'categorical.' I then created the name for each element of the categorial vector. Finally, I set the Labels for the datastore equal to the categorial vector. However, when I did this, I ran into the same problem you had.
In order to resolve this issue, I had to pre-allocate my labels as a vector of datatype 'cell'. In my case, my datastore was called 'imdsTrain' and I called the cell vector containing the image names as 'train_lbl_names.' I then specified the name for each element of the categorical vector. Finally, I set the labels for the datastore as follows:
imdsTrain.Labels = categorical(train_lbls_names);
When I did it this way, this fixed the problem that you described above.
To summarize, when trying to specify the labels for your datastore, simply create a vector of type 'cell' that contains the names for the labels, and then convert the vector to datatype 'categorical' when you want to set the names of the files in the image datastore.
I hope this helps!
-David

  0 Comments

Sign in to comment.