Why is my data not working for the trainNetwork function?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2022-7-31
编辑:Walter Roberson
2022-7-31
Your .mat does not include raw2 so we cannot test your code.
Also, we need XVal and YVal.
采纳的回答
Walter Roberson
2022-7-31
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 个评论
Walter Roberson
2022-7-31
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!