Why is my data not working for the trainNetwork function?

2 次查看(过去 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
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.
Yasmin Ben Azouz
Yasmin Ben Azouz 2022-7-31
@Walter Roberson Apologies, I now see I am also missing Yval and Xval. I added a new .mat file wit all vars needed.

请先登录,再进行评论。

采纳的回答

Walter Roberson
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
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.
Yasmin Ben Azouz
Yasmin Ben Azouz 2022-7-31
@Walter Roberson I do get this, but the elements of my x data are not independent. The course of their behaviour is what i am trying to use to detect a contraction region. As an example I have similar data that i have used to detect a measurement region, a start and n/a categories for the same purpose. The network training does run with these x and y training sets.
I simply don't understand the difference between the first set of training data I sent and the one attached here? I only attached the y training data, because the x was too large. The set up was the exact same, so they were numerical doubles the same size as the y data.
Do you have any idea what the difference is? Thanks in advance.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by