Clear Filters
Clear Filters

Error in deep learning classification code

23 views (last 30 days)
Hi. I am writing a deep learning classification code below.
The values in YTrain1 are numbers, and numClass is 5. The error code is as follows:
Error using validateTrueValues (line 45)
Number of channels in predictions (5) must match the number of channels in the targets (1).
Thank you.
clc; clear;
load("paddedData_Train.mat","-mat")
XTrain = paddedData_Train(:,3);
YTrain1 = cell2mat(paddedData_Train(:,1));
dsXTrain = arrayDatastore(XTrain, 'OutputType', 'same');
dsYTrain1 = arrayDatastore(YTrain1);
dsTrainA = combine(dsXTrain, dsYTrain1);
numClass = numel(unique(YTrain1));
net = dlnetwork;
tempNet = [
sequenceInputLayer([440 5 1],"Name","sequenceinput")
convolution2dLayer([3 3],16,"Name","conv_A1")
batchNormalizationLayer("Name","batchnorm_A1")
reluLayer("Name","relu_A1")
convolution2dLayer([3 3],16,"Name","conv_2")
batchNormalizationLayer("Name","batchnorm_2")
reluLayer("Name","relu_2")
flattenLayer("Name","flatten")
lstmLayer(200,"Name","lstm","OutputMode","last")
fullyConnectedLayer(numClass,"Name","fc_1")
softmaxLayer];
net = addLayers(net,tempNet);
plot (net)
options = trainingOptions('sgdm', ...
'MaxEpochs', 10000, ...
'MiniBatchSize', 50, ...
'Shuffle', 'every-epoch', ...
'Plots', 'training-progress');
lossFcnA = @(Y1,dsYTrain1) crossentropy(Y1,dsYTrain1);
net = trainnet(dsTrainA, net, lossFcnA, options);

Accepted Answer

Milan Bansal
Milan Bansal on 28 May 2024
Hi Daeyeon Koh
The error message you're encountering indicates that the output of your network does not match the expected format of your target labels YTrain1. The network is configured to output 5 channels (due to numClass being 5, which leads to a fullyConnectedLayer with 5 outputs), but your target labels YTrain1 are likely not in a one-hot encoded format, which is expected for a multi-class classification problem when using crossentropy as the loss function.
To resolve this issue, make sure that your target labels YTrain1 are in the correct format. Since numClass is 5, each target should be a one-hot encoded vector of size 5. Here's how you can adjust your code:
1.) One-Hot Encode YTrain1: Convert YTrain1 into a one-hot encoded format. Assuming YTrain1 contains class labels as integers ranging from 1 to numClass, you can use the following code to one-hot encode these labels:
YTrain1_oneHot = full(ind2vec(YTrain1', numClass))';
2.) Adjust the Datastore for One-Hot Encoded Labels:
dsYTrain1 = arrayDatastore(YTrain1_oneHot, 'OutputType', 'same');
3.) Update the Loss Function
lossFcnA = @(Y1, T1) crossentropy(Y1, T1, 'DataFormat', 'BC');
Here, Y1 is the output of your network, and T1 is the one-hot encoded truth from dsYTrain1. The 'DataFormat', 'BC' argument specifies that both inputs and targets are in the format [BatchSize, NumClasses], which should match the format of your one-hot encoded labels.
Please refer to the following documentation link to learn more about ind2vec function.
Hope it helps!

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!