Invalid training data. The output size (4) of the last layer does not match the number of classes (5).How to match the size for neural network?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
clear all;
load emg_E1.mat;
for i = 1:24
namestr = ['s' num2str(i) '=','emg_E1(4441:504440,i)',';'];
eval(namestr);
end
rowDist=ones(1,160);
rowDist=3125*rowDist;
for i=1:24
namestr = ['c' num2str(i) '=','mat2cell','(','s' num2str(i) ',' 'rowDist',')',';'];
eval(namestr);
end
for i=1:24
for j=1:160
namestr = ['m' num2str(j) '=','c' num2str(i),'{',num2str(j),'}',';'...
'm' num2str(j) '=','m' num2str(j),'''',';'...
'c' num2str(i) '{',num2str(j),'}' '=','m' num2str(j) ';'];
eval(namestr);
end
end
Signals=[c1;c2;c3;c4;c5;c6;c7;c8;c9;c10;c11;c12;c13;c14;c15;c16;c17;c18;c19;c20;c21;c22;c23;c24;];
Labels=zeros(3840,1);
Labels=categorical(Labels);
Labels(1:960)='1';Labels(961:1920)='2';Labels(1921:2880)='3';Labels(2881:3840)='4';
XTrain=Signals;YTrain=Labels;
%=====定义 LSTM 网络架构=====%
inputSize = 1;
numHiddenUnits = 100;
numClasses = 4;
layers = [ ...
sequenceInputLayer(inputSize)
bilstmLayer(numHiddenUnits,'OutputMode','last')
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer]
maxEpochs = 100;
miniBatchSize = 3125;
options = trainingOptions('adam', ...
'MaxEpochs',10, ...
'MiniBatchSize', 150, ...
'InitialLearnRate', 0.001, ...
'SequenceLength', 625, ...
'GradientThreshold', 1, ...
'ExecutionEnvironment',"auto",...
'plots','training-progress', ...
'Verbose',false);
net = trainNetwork(XTrain,YTrain,layers,options);
0 comentarios
Respuestas (1)
Soumya
el 23 de Jun. de 2025
Editada: Soumya
el 23 de Jun. de 2025
The ‘categorical’ data type in MATLAB is used to represent data that can take on a fixed number of discrete categories. Initially, ‘Labels’ is created as a numeric vector of ‘zeros’ with length 3840. When it is immediately converted to a categorical array, a single category ('0') is created based on the initial numeric values. Later, when the entries in ‘Labels’ are reassigned with the string values ('1', '2', '3', and '4'), they get added as new categories. As a result, the ‘Labels’ array ends up having five categories.
This mismatch causes the error when training the neural network, which is configured for only four output classes. The presence of an unintended fifth category ('0') causes the output layer to be incompatible with the label data.
To resolve this issue, assign the labels before converting the array to categorical, as shown below:
Labels = zeros(3840,1);
Labels(1:960) = 1;
Labels(961:1920) = 2;
Labels(1921:2880) = 3;
Labels(2881:3840) = 4;
Labels = categorical(Labels);
With this approach, the number of categories in the categorical array matches the number of defined output classes.
Please refer to the following documentation to know more about the ‘categorical’ function:
I hope this helps.
0 comentarios
Ver también
Categorías
Más información sobre Deep Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!