Softmax Cross-Entropy Loss
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[]) → doubleExamples
Example 1
logits = [[1.0,2.0,3.0],[1.0,1.0,1.0]]labels = [2,0]return = 0.7531091265562451The 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.31326168751822286Subtracting 1000 yields exponentials of 1 and e^-1, avoiding overflow.
Constraints
1 <= logits.length = labels.length <= 2002 <= logits[i].length <= 100, and every row has the same length-1000 <= logits[i][j] <= 1000, and every logit is finite0 <= labels[i] < logits[i].length- Results use absolute tolerance
1e-9