Question
I am using weigth classfication fucntion which given as example in MATALAB documentaion. But whenI use it in my network it gives error "Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed". I think the error is due to input value but i am not sure where to change these valuse. The weighted classification function works well according to input valuse assigned in example. The link of example https://in.mathworks.com/help/deeplearning/ug/create-custom-weighted-cross-entropy-classification-layer.html the code I am using for weighted classification function %%%%%% classdef weightedClassificationLayer < nnet.layer.ClassificationLayer properties % Row vector of weights corresponding to the classes in the % training data. ClassWeights end methods function layer = weightedClassificationLayer(classWeights, name) % layer = weightedClassificationLayer(classWeights) creates a % weighted cross entropy loss layer. classWeights is a row % vector of weights corresponding to the classes in the order % that they appear in the training data. % % layer = weightedClassificationLayer(classWeights, name) % additionally specifies the layer name. % Set class weights. layer.ClassWeights = classWeights; % Set layer name. if nargin == 2 layer.Name = name; end % Set layer description layer.Description = 'Weighted cross entropy'; end function loss = forwardLoss(layer, Y, T) % loss = forwardLoss(layer, Y, T) returns the weighted cross % entropy loss between the predictions Y and the training % targets T. N = size(Y,4); Y = squeeze(Y); T = squeeze(T); W = layer.ClassWeights; loss = -sum(W*(T.*log(Y)))/N; end function dLdY = backwardLoss(layer, Y, T) % dLdX = backwardLoss(layer, Y, T) returns the derivatives of % the weighted cross entropy loss with respect to the % predictions Y. [~,~,K,N] = size(Y); Y = squeeze(Y); T = squeeze(T); W = layer.ClassWeights; dLdY = -(W'.*T./Y)/N; dLdY = reshape(dLdY,[1 1 K N]); end end end
Expert Answer
Neeta Dsouza
PhD Expert
Answered Aug 30, 2026
This is a way to initialize 'classWeights'
classWeights = 1./countcats(YTrain); classWeights = classWeights'/mean(classWeights);
and you can use it here:
Network = [
imageInputLayer([256 256 3],"Name","imageinput")
convolution2dLayer([3 3],2,"Name","conv","Padding","same")
reluLayer("Name","relu")
softmaxLayer("Name","softmax")
weightedClassificationLayer(classWeights)
];
I think this should solve the problem.
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related deep learning Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →