Number of observations in X and Y disagree.

조회 수: 5 (최근 30일)
Ben Payne
Ben Payne 2020년 10월 23일
답변: Shashank Gupta 2020년 10월 28일
Hey everyone - Cannot for the life of me get this to work. No matter what I do, I get the "Number of observations in X and Y disagree." error. I understand that X train should be a HxWxCxN 4-D matrix and that YTrain should be a Nx1 matrix. I've checked that already and this seems correct.
Any help would be appreciated.
Thanks,
Ben Payne
data = load('C:\Users\Ben Payne\Downloads\matlab\matlab\emnist-letters.mat');
testImages = reshape(data.dataset.test.images.', 28, 28, []);
figure;
for i = 1:20
subplot(4,5,i);
imshow(testImages(:,:,i));
end
trainingImages = reshape(data.dataset.train.images.', 28, 28, []);
idx = randperm(size(trainingImages,3),1000);
XValidation = trainingImages(:,:,idx);
YValidation = categorical(data.dataset.train.labels(idx));
layers = [
imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer
];
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',8, ...
'Shuffle','every-epoch', ...
'ValidationData',{XValidation,YValidation}, ...
'ValidationFrequency',30, ...
'Verbose',false, ...
'Plots','training-progress');
XTrain = reshape(data.dataset.train.images', 28, 28, 1, []);
YTrain = categorical(data.dataset.train.labels);
net = trainNetwork(XTrain, YTrain, layers, options);
The dataset can be downloaded from here

답변 (1개)

Shashank Gupta
Shashank Gupta 2020년 10월 28일
Hi Ben,
I am not sure if you already figured out the mistake. Neverthless it seems the YTrain has 26 classes which is conflcting with the number of neuron(10) defined at the last fully connnected layer. Ideally both should be same, I did not dig much in to the code but surely changing the neuron to 26 would solve your problem.
I hope it helps you.
Cheers.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by