Why is my data not working for the trainNetwork function?
4 views (last 30 days)
Show older comments
Yasmin Ben Azouz
on 31 Jul 2022
Commented: Yasmin Ben Azouz
on 31 Jul 2022
Hi all, I am creating a CNN with matlab. My data consists of 16 measurements of 7200000x1 numerical sequence data that respresents anal pressure. Each sample is labelled either 'n/a' or 'contraction'.
When I try to use the trainnetwork function to create 250x1 sequences, I get the following error:
Error using trainNetwork (line 184)
Invalid training data. Responses must be a cell array of categorical response sequences.
Error in YASTrainNetwork (line 96)
net = trainNetwork(Xtrain, Ytrain, layers, options);
However, the training data I am putting in is exactly as is described. I don't understand what I am doing wrong. This usually always works.
My training code is below and i attached the vars.mat file containing my Xtrain and Ytrain. Help would be greatly appreciated!
numSequence = size(raw2{1,1},1);
numHiddenUnits = 60;
InitialLearnRate = 0.0011113;
MaxEpochs = 2;
MiniBatchSize = 30;
layers = [...
sequenceInputLayer(numSequence,'Normalization', 'zscore')
bilstmLayer(numHiddenUnits,'OutputMode','sequence')
fullyConnectedLayer(3)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',MaxEpochs, ...
'MiniBatchSize',MiniBatchSize, ...
'InitialLearnRate',InitialLearnRate, ...
'LearnRateDropPeriod',3, ...
'LearnRateSchedule','piecewise', ...
'GradientThreshold',1, ...
'Plots','training-progress',...
'shuffle','every-epoch',...
'SequenceLength',500,...
'Verbose',0,...
'ValidationData',{Xval,Yval},...
'ValidationFrequency',50,...
'ValidationPatience',inf,...
'DispatchInBackground',true );
net = trainNetwork(Xtrain, Ytrain, layers, options);
2 Comments
Walter Roberson
on 31 Jul 2022
Edited: Walter Roberson
on 31 Jul 2022
Your .mat does not include raw2 so we cannot test your code.
Also, we need XVal and YVal.
Accepted Answer
Walter Roberson
on 31 Jul 2022
Your y is a cell array of categoricals, in which each entry is a column vector. The entries must be row vectors instead.
Ytrain = cellfun(@(C) reshape(C, 1, []), Ytrain, 'UniformOutput',false);
You then run into the problem,
Invalid training data. The output size (3) of the last layer does not match the number of classes of the responses (2).
You fix that by changing to
fullyConnectedLayer(2)
After that you get
Invalid training data. Sequence responses must have the same sequence length as the corresponding predictors.
I think it is likely this has to do with the fact that you have exactly one y value for every x value. You would normally have one y value (class) for each sequence, not for each value in the sequence.
3 Comments
Walter Roberson
on 31 Jul 2022
Suppose that you have some data A1 that you have a class label L1 for. Suppose that you have a second set of data A2 that you have label L2 for. Now part of training is selection (usually with reordering) of subsets of the data: you should be able to (for example) Leave One Out. So if you have class labels for A1 and A2 then they would have to be independent, not sequences.
If the first element of your x data is independent of the second of the third, and so on, then they should not be part of a sequence .
A sequence is a series of ordered data that has a single aggregate class label.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows 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!