Derive and Implement the Gradient of Softmax Cross-Entropy
Last updated: November 29, 2026
Quick Overview
Derive the gradient of the softmax cross-entropy loss with respect to the logits, implement it in NumPy for a batch, and verify it numerically.
Meta
November 29, 20260
0
0 solved
Derive the gradient of the softmax cross-entropy loss with respect to the logits, implement it in NumPy for a batch, and verify it numerically.
Meta MLE screens sometimes include a short from-scratch exercise to confirm that a candidate understands backpropagation rather than only framework calls. The interviewer checks the derivation, numerical stability, and the habit of gradient checking.
What the Interviewer Expects
- Derive dL/dz = softmax(z) minus one-hot(y)
- Use the log-sum-exp trick for stability
- Average over the batch and scale the gradient accordingly
- Verify with a finite-difference gradient check
- Explain why the fused op is preferred over separate softmax and log
Key Topics to Cover
How to Approach This
- Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
- Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
- Feature engineering is often more impactful than model selection.
- Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
- Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
- How does the gradient change with label smoothing?
- Why is the gradient bounded, and why does that matter for training?
- How would you compute this for a 128k-token vocabulary without running out of memory?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Explore ML Interview PrepSample Answer
Clarify
Input: logits and integer labels . Output: the mean loss over the batch and with the same shape as ....
Approach
For one example, and . Differentiate directly: $\frac{\partial L}{\partial z_j} = -\mathbb{1}[j = y] + \frac{e^{z_j}}{\sum_...