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

% internal_l1loss    Return the (weighted and masked) L1 loss between
% estimates X and true responses T averaged by normFactor.
%
% Syntax:
%   X = internal_l1loss(X, T, weights, mask, normFactor);
%
% Inputs (image):
%   X              Predictions made by network
%   T              Targets (actual values)
%   weights        Weights
%   mask           Mask
%   normFactor     Normalization factor

%   Copyright 2021-2023 The MathWorks, Inc.

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

% L1 loss
X = weights .* internal_mask(abs(X-T), mask);
X = deep.internal.dlarray.reduceLoss(X, normFactor);

end