Number of observations in X and Y disagree.

5 vues (au cours des 30 derniers jours)
Ben Payne
Ben Payne le 23 Oct 2020
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

Réponses (1)

Shashank Gupta
Shashank Gupta le 28 Oct 2020
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.

Catégories

En savoir plus sur Image Data Workflows dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by