Error using trainNetwork . Number of observations in X and Y disagree.
4 views (last 30 days)
Show older comments
Gowri Prasood
on 15 Jul 2023
Commented: Gowri Prasood
on 18 Jul 2023
Good Evening,
I'm trying to train a neural network but "Error using trainNetwork . Number of observations in X and Y disagree." error came out. I am not able to understand where is the problem.
XTrain is a 607x39x367x1 double and loadYTrain is 607x1 double (before training i changed to categorical also.).
can you please help me to resolve this?. its urgent.
Thank you all in advance.
Here is my code:
layers = [
imageInputLayer([39 367 1])
convolution2dLayer(3, 32, 'Padding', 'same')
reluLayer
convolution2dLayer(3, 64, 'Padding', 'same')
reluLayer
maxPooling2dLayer(2, 'Stride', 2)
dropoutLayer(0.2)
fullyConnectedLayer(128)
reluLayer
dropoutLayer(0.2)
fullyConnectedLayer(64)
reluLayer
fullyConnectedLayer(15)
softmaxLayer
classificationLayer];
% Set training options
options = trainingOptions('adam', ...
'MaxEpochs', 20, ...
'MiniBatchSize', 16, ...
'InitialLearnRate', 0.0001, ...
'ValidationData', {test_data, test_labels_categorical}, ...
'Plots', 'training-progress');
train_labels_categorical = categorical(train_labels);
% Train the model
trained_model = trainNetwork(train_data, train_labels_categorical, layers, options);
0 Comments
Accepted Answer
Katja Mogalle
on 17 Jul 2023
Hello,
The Deep Learning Toolbox in MATLAB expects in-memory image data to be presented as a h-by-w-by-c-by-N numeric array, where h, w, and c are the height, width, and number of channels of the images, respectively, and N is the number of images.
If I understand your data correctly, your array is currently of size N-by-h-by-w-by-c. You can permute the training and test data to bring them in the correct format as follows:
train_data = permute(train_data,[2,3,4,1]);
I hope this helps.
Katja
6 Comments
Katja Mogalle
on 18 Jul 2023
You mentioned initially, that "XTrain is a 607x39x367x1". Now you say that "x_train=607x14313". So I assume the data was somehow flattened to save it to file, but I don't know how exactly.
You'd need to reshape the data ack into a 4-D array which the convolutional neural network can interpret as height-by-width-by-channels-by-numObservations. But to to this, you need to figure out how the data was flattened. I suspect you'd need one of the following commands to unflatten the data:
x_train = reshape(x_train,[607,39,367,1])
or
x_train = reshape(x_train,[607,367,39,1])
Then, don't forget to permute the observation dimension into the fourth dimension as I showed earlier.
As you are using a 2D convolutional neural network, you have to decide which dimensions of your data represent the two "spatial" dimensions and which ones represents the "channel/feature" dimension. I suspect your data only has one dimension that could be interpreted as "space" (or time). So another option you could have a look at, are 1D CNNs or even recurrent networks. Here is an example showing speech emotion recognition based on gtcc coefficients and which uses a recurrent neural network: https://www.mathworks.com/help/audio/ug/speech-emotion-recognition.html
More Answers (0)
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!