FastPrepSoftmax Cross-Entropy Loss

Softmax Cross-Entropy Loss

Microsoft logoMicrosoft● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given a batch of classification logits and the correct class index for each row in labels, return the mean softmax cross-entropy loss.

For row i, its loss is log(sum(exp(logits[i][j]))) - logits[i][labels[i]]. Compute it with the maximum logit subtracted from every value in that row so the exponentials remain numerically stable.

Function

softmaxCrossEntropy(logits: double[][], labels: int[]) → double

Examples

Example 1

logits = [[1.0,2.0,3.0],[1.0,1.0,1.0]]labels = [2,0]return = 0.7531091265562451

The first row contributes about 0.407606 and the uniform second row contributes log(3); their mean is about 0.753109.

Example 2

logits = [[1000.0,999.0]]labels = [0]return = 0.31326168751822286

Subtracting 1000 yields exponentials of 1 and e^-1, avoiding overflow.

Constraints

  • 1 <= logits.length = labels.length <= 200
  • 2 <= logits[i].length <= 100, and every row has the same length
  • -1000 <= logits[i][j] <= 1000, and every logit is finite
  • 0 <= labels[i] < logits[i].length
  • Results use absolute tolerance 1e-9

More Microsoft problems

See Microsoft hiring insights
public double softmaxCrossEntropy(double[][] logits, int[] labels) {
  // Write your code here.
}
logits[[1.0,2.0,3.0],[1.0,1.0,1.0]]
labels[2,0]
expected0.7531091265562451
Checking account…