Compute the mean multiclass cross-entropy loss from correct class labels and predicted class probabilities. For sample i, select the probability assigned to its correct class:
Li=−log(pi,yi)Average the sample losses:
L=−N1i=1∑Nlog(pi,yi)Here, N is the number of samples, yi is the correct class index for sample i, and pi,yi is the predicted probability of that class. Use the natural logarithm and return the mean loss as a Python float.
Input: y_true = [0, 1], y_pred = [[0.9, 0.1], [0.3, 0.7]]
Output: 0.231018
Explanation: The selected probabilities are 0.9 and 0.7, and the output is the mean of their negative logarithms.
Input: y_true = [2], y_pred = [[0.1, 0.1, 0.8]]
Output: 0.223144
Input: y_true = [1, 0, 1], y_pred = [[0.2, 0.8], [0.6, 0.4], [0.49, 0.51]]
Output: 0.469105
np.arange(len(y_true)) provides the row indices for advanced indexing.
y_pred[row_indices, y_true] selects one correct-class probability per sample.
Use np.log() followed by np.mean() for the final reduction.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Compute the mean multiclass cross-entropy loss from correct class labels and predicted class probabilities. For sample i, select the probability assigned to its correct class:
Li=−log(pi,yi)Average the sample losses:
L=−N1i=1∑Nlog(pi,yi)Here, N is the number of samples, yi is the correct class index for sample i, and pi,yi is the predicted probability of that class. Use the natural logarithm and return the mean loss as a Python float.
Input: y_true = [0, 1], y_pred = [[0.9, 0.1], [0.3, 0.7]]
Output: 0.231018
Explanation: The selected probabilities are 0.9 and 0.7, and the output is the mean of their negative logarithms.
Input: y_true = [2], y_pred = [[0.1, 0.1, 0.8]]
Output: 0.223144
Input: y_true = [1, 0, 1], y_pred = [[0.2, 0.8], [0.6, 0.4], [0.49, 0.51]]
Output: 0.469105
np.arange(len(y_true)) provides the row indices for advanced indexing.
y_pred[row_indices, y_true] selects one correct-class probability per sample.
Use np.log() followed by np.mean() for the final reduction.
Sign in to take notes on this problem
Accepts: array
Accepts: array