TensorTonic
Problems
Implement Cross-Entropy Loss
Loss Functions
Medium

Compute the mean multiclass cross-entropy loss from correct class labels and predicted class probabilities. For sample iii, select the probability assigned to its correct class:

Li=−log⁡(pi,yi)L_i = -\log(p_{i,y_i})Li​=−log(pi,yi​​)

Average the sample losses:

L=−1N∑i=1Nlog⁡(pi,yi)L = -\frac{1}{N} \sum_{i=1}^{N} \log(p_{i,y_i})L=−N1​i=1∑N​log(pi,yi​​)

Here, NNN is the number of samples, yiy_iyi​ is the correct class index for sample iii, and pi,yip_{i,y_i}pi,yi​​ is the predicted probability of that class. Use the natural logarithm and return the mean loss as a Python float.

Loading visualization...

Examples

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

Hint 1

np.arange(len(y_true)) provides the row indices for advanced indexing.

Hint 2

y_pred[row_indices, y_true] selects one correct-class probability per sample.

Hint 3

Use np.log() followed by np.mean() for the final reduction.

Requirements

  • y_true contains one valid class index for each row of y_pred
  • y_pred contains probabilities rather than logits
  • Select all correct-class probabilities without a Python loop over samples
  • Return the mean loss as a Python float

Constraints

  • y_pred has shape (N,K)(N, K)(N,K) and y_true has length NNN
  • 1≤N≤100,0001 \leq N \leq 100{,}0001≤N≤100,000 and 2≤K≤1002 \leq K \leq 1002≤K≤100
  • Every probability is positive and each probability row sums to 1
  • Use NumPy only
Try Similar Problems
Implement KL Divergence in Python (NumPy)MediumImplement Focal LossMediumBinary Log Loss per Sample in PythonEasyImplement Hinge Loss (Binary SVM)EasyImplement Numerically Stable SoftmaxMediumLogistic Regression Training LoopMediumImplement Dice LossMediumImplement Contrastive Loss (Siamese)MediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium
Scratchpad

Accepts: array

Accepts: array

You must run your code first.
Implement Cross-Entropy Loss

Implement Cross-Entropy Loss

Loss Functions
Medium

Compute the mean multiclass cross-entropy loss from correct class labels and predicted class probabilities. For sample iii, select the probability assigned to its correct class:

Li=−log⁡(pi,yi)L_i = -\log(p_{i,y_i})Li​=−log(pi,yi​​)

Average the sample losses:

L=−1N∑i=1Nlog⁡(pi,yi)L = -\frac{1}{N} \sum_{i=1}^{N} \log(p_{i,y_i})L=−N1​i=1∑N​log(pi,yi​​)

Here, NNN is the number of samples, yiy_iyi​ is the correct class index for sample iii, and pi,yip_{i,y_i}pi,yi​​ is the predicted probability of that class. Use the natural logarithm and return the mean loss as a Python float.

Loading visualization...

Examples

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

Hint 1

np.arange(len(y_true)) provides the row indices for advanced indexing.

Hint 2

y_pred[row_indices, y_true] selects one correct-class probability per sample.

Hint 3

Use np.log() followed by np.mean() for the final reduction.

Requirements

  • y_true contains one valid class index for each row of y_pred
  • y_pred contains probabilities rather than logits
  • Select all correct-class probabilities without a Python loop over samples
  • Return the mean loss as a Python float

Constraints

  • y_pred has shape (N,K)(N, K)(N,K) and y_true has length NNN
  • 1≤N≤100,0001 \leq N \leq 100{,}0001≤N≤100,000 and 2≤K≤1002 \leq K \leq 1002≤K≤100
  • Every probability is positive and each probability row sums to 1
  • Use NumPy only
Try Similar Problems
Implement KL Divergence in Python (NumPy)MediumImplement Focal LossMediumBinary Log Loss per Sample in PythonEasyImplement Hinge Loss (Binary SVM)EasyImplement Numerically Stable SoftmaxMediumLogistic Regression Training LoopMediumImplement Dice LossMediumImplement Contrastive Loss (Siamese)MediumHigher-Order Derivatives & Loss CurvatureMediumLoss FunctionsMediumBradley-Terry Reward Model LossMediumREINFORCE with BaselineMedium
Scratchpad

Accepts: array

Accepts: array

You must run your code first.
Problems
Loading...
1 / 1