Numerically Stable Row Softmax

Quick Overview

Implement a row-wise softmax in NumPy that stays finite for logits as large as 1000 or as small as -1000. It tests overflow and underflow handling, broadcasting along the right axis, and returning a new float64 array without changing the input.

Numerically Stable Row Softmax

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `softmax_rows(Z: np.ndarray) -> np.ndarray` for a 2-D float logit array of shape `(n_samples, n_classes)`. Return a new `float64` array of the same shape where each row is `exp(z_j - max(row)) / sum_k exp(z_k - max(row))`. Requirements: - Subtract the per-row max (`Z.max(axis=1, keepdims=True)`) before exponentiating. - Every row sums to `1.0` with no `NaN` or `inf`. - Do not modify `Z` in place. For example: ```python Z = np.array([[1000., 1001., 1002.], [-1000., -1001., -1002.]]) out = softmax_rows(Z) # out[0] -> [0.09003057, 0.24472847, 0.66524096] # out.sum(axis=1) -> [1., 1.] and np.isfinite(out).all() == True ``` ```hint Watch the input array Think about which NumPy operations allocate a new array and which write into an existing one, and what `np.asarray` returns when the input already has the dtype you ask for. ``` ### Constraints & Assumptions - `Z` is a 2-D float array of shape `(n_samples, n_classes)`. Logits can be large in magnitude, as in the example (around 1000 or -1000). - The output is graded with `assert_allclose(rtol=1e-5)` against `scipy.special.softmax(Z, axis=1)`, plus `np.isfinite(out).all()` and a row-sum check. ### What a Strong Answer Covers - What goes wrong with the naive formula for large positive and large negative logits, and why the max shift fixes both cases. - The correct axis and broadcasting for both the max and the sum, so each row is normalized on its own. - An out-of-place computation that returns a new `float64` array and leaves `Z` unchanged. - Time and space complexity, and edge cases such as equal logits, a single class, or non-finite entries. ### Follow-up Questions - Why does subtracting a per-row constant leave the result unchanged? - How would you extend this to compute log-softmax stably? - Some models mask invalid classes by setting their logits to `-inf`. Does your function still work, and when does it break?

Overview: Implement a row-wise softmax in NumPy that stays finite for logits as large as 1000 or as small as -1000. It tests overflow and underflow handling, broadcasting along the right axis, and returning a new float64 array without changing the input.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement softmax_rows(Z: np.ndarray) -> np.ndarray for a 2-D float logit array of shape (n_samples, n_classes). Return a new float64 array of the same shape where each row is exp(z_j - max(row)) / sum_k exp(z_k - max(row)).

Requirements:

  • Subtract the per-row max ( Z.max(axis=1, keepdims=True) ) before exponentiating.
  • Every row sums to 1.0 with no NaN or inf .
  • Do not modify Z in place.

For example:

Z = np.array([[1000., 1001., 1002.],
              [-1000., -1001., -1002.]])
out = softmax_rows(Z)
# out[0] -> [0.09003057, 0.24472847, 0.66524096]
# out.sum(axis=1) -> [1., 1.]  and np.isfinite(out).all() == True

Constraints & Assumptions

  • Z is a 2-D float array of shape (n_samples, n_classes) . Logits can be large in magnitude, as in the example (around 1000 or -1000).
  • The output is graded with assert_allclose(rtol=1e-5) against scipy.special.softmax(Z, axis=1) , plus np.isfinite(out).all() and a row-sum check.

What a Strong Answer Covers Guidance

  • What goes wrong with the naive formula for large positive and large negative logits, and why the max shift fixes both cases.
  • The correct axis and broadcasting for both the max and the sum, so each row is normalized on its own.
  • An out-of-place computation that returns a new float64 array and leaves Z unchanged.
  • Time and space complexity, and edge cases such as equal logits, a single class, or non-finite entries.

Follow-up Questions Guidance

  • Why does subtracting a per-row constant leave the result unchanged?
  • How would you extend this to compute log-softmax stably?
  • Some models mask invalid classes by setting their logits to -inf . Does your function still work, and when does it break?