How to input validation data correctly for neural network?

20 views (last 30 days)
I am having trouble getting MATLAB to accept my validation data correctly.
I am doing sequence-to-sequence classification, with inputs as doubles and responses as categorical arrays. I have separated out my data for training, validation, and testing. Here is some code.
inputSize = 1;
embeddingDimension = 256;
numClasses = numel(categories([YTrain{:}]));
batchSize = 121;
layers = [
sequenceInputLayer(inputSize)
lstmLayer(256,'OutputMode','sequence')
dropoutLayer(0.2)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',500, ...
'InitialLearnRate',0.01, ...
'GradientThreshold',2, ...
'MiniBatchSize',batchSize, ...
'Shuffle','never', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',[XVal,YVal], ...
'ValidationFrequency',1512, ...
'CheckpointPath','checkpoints', ...
'CheckpointFrequency',1, ...
'CheckpointFrequencyUnit','epoch');
net = trainNetwork(XTrain,YTrain,layers,options);
XVal is an N x 1 cell array of doubles, and YVal is an N x 1 cell array of categorical. Here are some errors I have recieved after trying [XVal,YVal] and {XVal,YVal}.
Error 1
Error using nnet.cnn.TrainingOptionsADAM
The value of 'ValidationData' is invalid. Cell array with validation data must have two elements:
the input data X and a numeric array of responses Y.
Error in trainingOptions (line 342)
opts = nnet.cnn.TrainingOptionsADAM(varargin{:});
Error 2
Error using trainNetwork
Training and validation responses must have the same categories. To view the categories of the responses, use the
categories function.
Error in script (line 198)
net = trainNetwork(XTrain,YTrain,layers,options);
Caused by:
Error using nnet.internal.cnn.trainNetwork.DLTDataPreprocessor>iAssertClassNamesAreTheSame
Training and validation responses must have the same categories. To view the categories of the responses, use
the categories function.
Thank you for any help that may be provided!
  2 Comments
Rahul
Rahul on 27 Feb 2023
Can you provide the dataset because each time you are receiving the different errors?Else, try working on any example given in MathWorks documentation pages. The link is given below:
Xinbin Wu
Xinbin Wu on 8 Aug 2023
Hello Rahul,
I have combined dataX and Y into one cell array , the following is the format of my data, but I still get the same error 1. May I ask where there is still a problem? Look forward to your reply. thank you.

Sign in to comment.

Answers (2)

Himanshu
Himanshu on 26 May 2023
Hello Miles,
I understand that you are facing errors in "trainingOptions" for sequence-to-sequence classification. The errors indicate problems with the format of the validation data and the categories of the responses.
Regarding Error 1:
The error message states that the value of "ValidationData" is invalid and that a cell array with validation data must have two elements: the input data X and a numeric array of responses Y. This error occurs because you are passing a cell array of categorical responses instead of a numeric array. To resolve this error, combine your input and response data into a single cell array before passing it to the "ValidationData" argument. For Example:
combinedValData = {XVal, YVal};
Regarding Error 2:
The error message states that the training and validation responses must have the same categories. This error occurred because the training and validation responses may have different categories. To resolve this error, check the categories of your training and validation responses using the "categories" function. If the categories are different, you need to make them consistent by merging or modifying the categories using "mergecats" function.
You can refer to the below documentation to learn more about "categories" and "mergecats" funcitons in MATLAB.
  1 Comment
Xinbin Wu
Xinbin Wu on 8 Aug 2023
Hello Himanshu,
I have combined dataX and Y into one cell array as requested, the following is the format of my data, but I still get the same error 1. May I ask where there is still a problem? Look forward to your reply. thank you.

Sign in to comment.


Xinbin Wu
Xinbin Wu on 8 Aug 2023
Would you solve this problem yet? If possible, could you tell me how to solve it.I have the same problem as you.
Thanks for your help.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!