The interview problem

Practice prompt

A classifier returns NaNs on large logits. Repair softmax computation, calculate a tiny example and explain the gradient of softmax cross-entropy.

Original practice, not a reported company interview. All scenarios and worked numbers are illustrative. Interview date: not applicable. Sources retrieved September 30, 2026.

1. Scope and assumptions

Assume finite logits and a normalized target distribution. Stable arithmetic prevents overflow but does not make predicted probabilities calibrated or fix invalid input values.

2. Worked approach

Subtract the maximum logit before exponentiation, then normalize. Adding or subtracting the same scalar from every logit leaves the mathematical probabilities unchanged because the common exponential factor cancels. Use a stable log-sum-exp expression for log probabilities rather than taking a log after a probability has underflowed.

For p_i=exp(z_i)/sum exp(z), the Jacobian entry is p_i times (indicator i=j minus p_j). Combining with cross-entropy and a target y summing to one gives gradient p−y. Apply the reduction convention explicitly: averaging a batch scales the summed gradient by batch size.

3. Calculate the example

For logits (1000,1000), naive exponentials overflow in common floating-point formats. Shifted logits (0,0) produce probabilities (0.5,0.5). The Jacobian is [[0.25,−0.25],[−0.25,0.25]]. With target (1,0), the cross-entropy gradient is (−0.5,0.5), and loss is ln 2≈0.6931 using natural logs. Each Jacobian row sums to zero, consistent with invariance to a shared logit shift.

4. Failure cases and trade-offs

Subtracting a maximum does not repair an input NaN or a vector of negative infinities. Applying softmax twice changes the probabilities and loss. Using an unstable separate log operation can still produce infinite loss when probabilities round to zero.

Fused log-softmax/cross-entropy implementations improve stability and avoid intermediate storage. Lower precision saves memory but needs careful accumulation and range handling. Temperature changes confidence and gradients, so it is a modeling choice rather than an overflow workaround.

5. Validate the result

Compare shifted and unshifted moderate-logit results, use finite differences for the Jacobian and test extreme finite logits. Verify gradient sum is zero and the loss reduction matches the training loop.

6. Interview follow-ups and self-review

Why is the Jacobian singular? What changes for soft targets instead of one-hot labels?

Reward shift invariance, correct Jacobian, stable log loss and clear limits of numerical repair.

Sources and related practice

Technical reference: docs.scipy.org — Official stable softmax reference; Jacobian walkthrough and numerical case are original.

Exact publication/update date not established in this review. Retrieved September 30, 2026. This reference supports the named technical concept; the proposed workflow and examples are original, not a documented vendor implementation or interview report.

Continue with Logistic regression, from score to decision; Gradient descent and training diagnostics.

Continue your preparation

Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.