%% Find Cross-Entropy Loss Between Predicted and Target Labels
% The cross-entropy loss evaluates how well the network predictions correspond to the target classification.
% Create the input classification data as a matrix of random variables. The data contains 12 observations that can be in any of 10 categories.

numCategories = 10;
observations = 12;

X = rand(numCategories,observations);
dlX = dlarray(X,'CB');

% Convert the category values in the data to probability scores for each category.

dlX = softmax(dlX);

% Create the target data, which holds the correct category for each observation in dlX.

targetsIdx = randi(10,1,12);
targets = zeros(10,12);
for i = 1:numel(targetsIdx)
    targets(targetsIdx(i),i) = 1;
end

% Compute the cross-entropy loss between the predictions and the targets.

dlY = crossentropy(dlX,targets)

% Copyright 2019 The MathWorks, Inc.