function X = internal_catcrossentropy(X, T, weights, mask, normFactor)
% This function is undocumented and will change in a future release.

% internal_crossentropy    Return the (weighted and masked) categorical
% cross-entropy between estimates X and true responses T averaged by
% normFactor.
%
% Syntax:
%   Y = internal_catcrossentropy(X, T, weights, mask, normFactor);
%
% Inputs (image):
%   X              Predictions made by network
%   T              Targets (actual values)
%   weights        Weights
%   mask           Mask
%   normFactor     Normalization factor

%   Copyright 2019-2023 The MathWorks, Inc.

if ~isreal(X) || ~isreal(T) || ~isreal(weights) || ~isreal(mask) || ~isreal(normFactor)
    error(message('deep:dlarray:ComplexNotSupported'))
end

X = nnet.internal.cnn.util.boundAwayFromZero(X);
X = weights .* T .* log(X);
X = internal_mask(X, mask);
X = -deep.internal.dlarray.reduceLoss(X, normFactor);

end
