Implement a numerically stable softmax in NumPy
Interview insights are lockedSee how often this is asked and which companies asked it with Buncho Pro.Implement softmax(x, axis=-1) that works on a batch of logits with shape (batch, classes).
- Why does the naive
np.exp(x) / np.exp(x).sum()fail forx = [1000, 1001]? - Make it stable and vectorised.
- How would you add a temperature parameter, and how do you compute log-softmax stably?