Matlab "trainNetwork" error Predictors and responses must have the same number of observations
73 views (last 30 days)
Show older comments
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 Comments
Pratyush Roy
on 24 Jan 2022
Hi,
Can you please share the csv file so that I can reproduce the issue on my end?
Thanks.
Answers (1)
Kumar Pallav
on 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.
0 Comments
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!