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

조회 수: 50 (최근 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
Pratyush Roy
Pratyush Roy 2022년 1월 24일
Hi,
Can you please share the csv file so that I can reproduce the issue on my end?
Thanks.
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