Why do I get a channel mismatch error between predictions and targets when using "trainnet"?

조회 수: 22 (최근 30일)

I am trying to train a neural network using the "trainnet" function. My setup involves splitting a dataset into training and test sets, and then training a network with the following relevant code:

% Split data into training and test sets[idxTrain, idxTest] = trainingPartitions(size(myDataTable, 1), [0.85, 0.15]);trainData = myDataTable(idxTrain, :);
% Prepare training datapredictorNames = ["feature1", "feature2", "feature3", "feature4"];XTrain = table2array(trainData(:, predictorNames));TTrain = trainData.targetColumn;
% Define network layerslayers = [    featureInputLayer(numel(predictorNames), Normalization="zscore")    fullyConnectedLayer(16)    reluLayer    fullyConnectedLayer(2)    softmaxLayer];
% Train networkoptions = trainingOptions("lbfgs", ExecutionEnvironment="cpu", Plots="training-progress", Verbose=false);net = trainnet(XTrain, TTrain, layers, "crossentropy", options);
However, I encounter the following error:

Error using trainnet
Number of channels in predictions (2) must match the number of channels in the targets (1).

Why is this happpening?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2024년 11월 11일 0:00
This issue arises due to a mismatch between the expected format of the target data and what is provided to the "trainnet" function.
  • The error indicates that the network's output layer is configured for 2 classes.
  • The target data "TTrain" is currently a 431x1 double, and appears to be formatted for only 1 class, but this should be a categorical.
Please find the below workarounds:
1. If you are in R2024a or earlier, convert "TTrain" from type "double" to a categorical array to match the expected input format:
TTrain = categorical(TTrain);
2. Specify feature data directly into "trainnet" as a table, with targets as the last column:
trainData.targetColumn = categorical(trainData.targetColumn);dataTrain = [trainData(:, predictorNames) trainData(:, "targetColumn")];numFeatures = width(dataTrain) - 1;net = trainnet(dataTrain, layers, "crossentropy", options);
3. In R2024b, new cross entropy loss functions were introduced to avoid the need for converting to categoricals.
  • "index-crossentropy": for multiclass classification:
net = trainnet(dataTrain, layers, "index-crossentropy", options);
  • "binary-crossentropy": if the problem is a binary classification, modify the network to end with a "sigmoidLayer" and use "binary-crossentropy":
layers = [    featureInputLayer(numel(predictorNames), Normalization="zscore")    fullyConnectedLayer(16)    reluLayer    fullyConnectedLayer(1)    sigmoidLayer];net = trainnet(dataTrain, layers, "binary-crossentropy", options);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Portfolio Optimization and Asset Allocation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by