MATLAB: I have a problem in the trainNetwork for Xtrain and Ytrain it gives me X and Y must have the same number of observations.

deep neural networks

XTrain = importdata('C:\Users\m7mod\Documents\MATLAB\TrainM.mat'); % size of XTrain = 9 * 44 YTrain = categorical([1 1 1 1 0 0 0 0 0]'); % size of YTrain = 9 * 1 layers = [ … imageInputLayer([44 1]) convolution2dLayer(5,20) reluLayer fullyConnectedLayer(10) softmaxLayer classificationLayer()] options = trainingOptions('sgdm'); XNew = zeros(size(XTrain,1),1,1,size(XTrain,2)); XNew(:,1,1,:) = XTrain(:,:); net = trainNetwork(XNew,YTrain',layers,options);

Best Answer

You are facing the problem because you are trying to use imageInputLayer and convolution2dLayer which will only work if your input sample have at least 2 non-singleton dimensions (i.e. m*n and m*n*k will work but 1*m or m*1 will not work). For a single dimension data (as in your case 1*44), you can use sequenceInputLayers. For your case, if you can change the layers combination as shown in following script snippet the code will work
XTrain = importdata('C:\Users\m7mod\Documents\MATLAB\TrainM.mat'); % size of XTrain = 9 * 44
YTrain = categorical([1 1 1 1 0 0 0 0 0]'); % size of YTrain = 9 * 1
layers = [ ...
sequenceInputLayer(44)
fullyConnectedLayer(2)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm');
net = trainNetwork(XTrain',YTrain',layers,options);
Related Question