Number of observations in X and Y disagree.
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!