Softmax Cross-Entropy Loss
Updated
Problem statement
You are given a batch of logits for a classification task. Each row holds one score per class. labels gives the index of the correct class for each row. Return the mean softmax cross-entropy loss over all rows.
For row i, the loss is log(sum(exp(logits[i][j]))) - logits[i][labels[i]], where the sum runs over every j in that row. Before taking the exponentials, subtract the largest logit in the row from every value in that row. This keeps the exponentials from growing too large, so the result stays 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.7531091265562451In the first row, the correct class has the largest logit, and its loss is about 0.407606. The second row has equal logits, so its loss is log(3). The mean of the two row losses is about 0.753109.
Example 2
logits = [[1000.0,999.0]]labels = [0]return = 0.31326168751822286Subtract 1000, the largest logit, from both values. The exponentials become 1 and e^-1, so nothing overflows. The correct class has the largest logit, so after the shift its value is zero. The loss is the log of the sum of the two exponentials, which is the output shown.
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- Your answer is accepted if it is within an absolute difference of
1e-9from the expected value
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
- Streaming Entropy, Part 2: Numerically Stable EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Random Pick with Weight Using Recorded DrawsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Maximize the Even-Odd Index Product GapInfosys · ONSITE INTERVIEWInfosys interview questions
- Squirrel Nut Collection Shortest PathAmazon · ONSITE INTERVIEW
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
- Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions
- Product Total With the Cheapest PromotionInstacart · ONSITE INTERVIEWInstacart interview questions
- Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions