function loss = maskedCrossEntropy(dlY,T,sequenceLengths)
%MASKEDCROSSENTROPYLOSS Cross entropy loss ignoring padding
% loss = maskedCrossEntropy(dlY,T,sequenceLengths) returns the loss between
% the dlY and T ignoring any time steps containing padding using the
% specified vector of sequence lengths.

% Initialize loss.
loss = 0;

% Loop over mini-batch.
miniBatchSize = size(dlY,2);
for n = 1:miniBatchSize
    idx = 1:sequenceLengths(n);
    loss = loss + crossentropy(dlY(:,n,idx), T(:,n,idx),'DataFormat','CBT');
end

% Normalize.
loss = loss / miniBatchSize;

end