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

53 次查看(过去 30 天)
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 个评论
SM
SM 2022-1-24
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

请先登录,再进行评论。

回答(1 个)

Kumar Pallav
Kumar Pallav 2022-2-1
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.

类别

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