PIXELBANKv9.1.0
Menu

Softmax Cross-Entropy Loss

You are given a vector of predicted class probabilities and the index of the true class. Your task is to compute the cross-entropy loss, which measures how well the predicted distribution matches the true label.

The Cross-Entropy Loss for classification is defined as:

L=βˆ’log⁑(py)L = -\log(p_y)

Where:

  • pp is the vector of predicted probabilities (must sum to 1)
  • yy is the index of the true class
  • pyp_y is the predicted probability for the true class

This loss function:

  1. Returns 0 when the model is perfectly confident in the correct class (py=1p_y = 1)
  2. Approaches infinity as confidence in the correct class approaches 0
  3. Heavily penalizes confident but wrong predictions

Round output to 4 decimal places.

Example:

Input:
probs = [0.7, 0.2, 0.1]
true_class = 0
Output:
0.3567
Reasoning:
  1. The true class is index 0
  2. The predicted probability for class 0 is p_0 = 0.7
  3. Cross-entropy loss = -log(0.7) = -(-0.3567) = 0.3567
  4. A probability of 0.7 yields relatively low loss since the model is fairly confident in the correct answer

Constraints:

  • probs is a list of probabilities that sum to 1
  • All probabilities are positive (> 0)
  • true_class is a valid index (0 to len(probs)-1)
  • Return the loss rounded to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.
Softmax Cross-Entropy Loss - Medium | PixelBank