Matlab "trainNetwork" error Predictors and responses must have the same number of observations

54 visualizzazioni (ultimi 30 giorni)
Hi
I am using one of matlab dataset, (transmissionCasingData.csv), to use 1D convolution layer to train a network. Eventhough, the size of my predictors and response are the same, but matlab throws the error that the size of the predictors and response must be the same.
I am wondering if anyone have any idea how to resole the issue.
clear
clc
filename = "transmissionCasingData.csv";
tbl = readtable(filename,'TextType','String');
labelName = "GearToothCondition";
tbl = convertvars(tbl,labelName,'categorical');
categoricalInputNames = ["SensorCondition" "ShaftCondition"];
tbl = convertvars(tbl,categoricalInputNames,'categorical');
for i = 1:numel(categoricalInputNames)
name = categoricalInputNames(i);
oh = onehotencode(tbl(:,name));
tbl = addvars(tbl,oh,'After',name);
tbl(:,name) = [];
end
tbl = splitvars(tbl);
classNames = categories(tbl{:,labelName});
numObservations = size(tbl,1);
numObservationsTrain = floor(0.85*numObservations);
numObservationsTest = numObservations - numObservationsTrain;
idx = randperm(numObservations);
idxTrain = idx(1:numObservationsTrain);
idxTest = idx(numObservationsTrain+1:end);
tblTrain = tbl(idxTrain,:);
tblTest = tbl(idxTest,:);
numFeatures = size(tbl,2) - 1;
numClasses = numel(classNames);
%% Define Layers
classificationLayer];
%}
numFilters = 64;
filterSize = 5;
layers = [
% featureInputLayer(numFeatures)
sequenceInputLayer(numFeatures)
convolution1dLayer(filterSize,numFilters,Padding="causal")
convolution1dLayer(24,3,Padding="causal")
convolution1dLayer(24,3,Padding="causal")
convolution1dLayer(24,3,Padding="causal")
dropoutLayer(0.2)
convolution1dLayer(128,3,Padding="causal")
convolution1dLayer(128,3,Padding="causal")
convolution1dLayer(128,3,Padding="causal")
maxPooling1dLayer(3,Padding="same")
dropoutLayer(0.2)
reluLayer
softmaxLayer
classificationLayer]
miniBatchSize = 16;
options = trainingOptions('adam', ...
'MiniBatchSize',miniBatchSize, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false);
% Op=table2cell(tblTrain);
TragetData=(tblTrain.GearToothCondition);
% TragetData=table2cell(TragetData);
TrainData=(tblTrain(:,1:22));
TrainData_Cell=(table2cell(TrainData));
% net = trainNetwork(tblTrain,layers,options);
TrainData=(TrainData_Cell');
ResponseData=TragetData';
net = trainNetwork(TrainData,ResponseData,layers,options); % error happnes here
YPred = classify(net,tblTest,'MiniBatchSize',miniBatchSize);
YTest = tblTest{:,labelName};
accuracy = sum(YPred == YTest)/numel(YTest)
%{
The error is:
Error using trainNetwork (line 184)
Invalid training data. Predictors and responses must have the same number of
observations.
Error in test (line 85)
net = trainNetwork(TrainData,ResponseData,layers,options);
%}
  2 Commenti
SM
SM il 24 Gen 2022
Hi,
The "transmissionCasingData.csv" is an inbuild MATLAB dataset, and you should be able to access it by default.
However, I have attached the '.mat' file in case you do not have access it.
I am using MATLAB 2021b.
Thank you for your help in advance

Accedi per commentare.

Risposte (1)

Kumar Pallav
Kumar Pallav il 1 Feb 2022
The error is generally caused due to mismatch in shapes in the data provided to the trainNetwork. You may refer to a similar problem here to resolve the issue.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by