Compute a sample-weighted binary log loss from raw logits without overflow

Quick Overview

Implement a sample-weighted binary log loss that works directly on raw logits and stays finite for very large logit values. It tests the logit-space form of cross-entropy, numerically stable softplus evaluation, and the difference between a weighted mean and a weighted sum.

Compute a sample-weighted binary log loss from raw logits without overflow

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `weighted_stable_log_loss(y_true, z, sample_weight, normalize=True)`. The inputs are 1-D arrays of shape `(n,)`: 0/1 labels `y_true`, raw logits `z`, and positive weights `sample_weight` that in general do not sum to n. Return the sample-weighted log loss computed stably from the logits, so that a large `|z|` cannot overflow. The function must: - Use the per-sample loss `logaddexp(0, z_i) - y_i * z_i`. Never form the probability or an explicit sigmoid. - With `normalize=True`, return `sum(w*l) / sum(w)`. The denominator is the sum of the weights, not n. - With `normalize=False`, return `sum(w*l)`. - Return a Python `float`. For example: ```python y_true = [1, 0, 1, 0] z = [2.0, -1.5, 0.3, -4.0] sample_weight = [1, 2, 3, 4] weighted_stable_log_loss(y_true, z, sample_weight, normalize=True) # 0.22654200120852983 weighted_stable_log_loss(y_true, z, sample_weight, normalize=False) # 2.265420012085298 ``` ```hint Where the formula comes from Write `log(sigmoid(z))` and `log(1 - sigmoid(z))` in terms of `log(1 + e^z)` and substitute them into the usual cross-entropy. Most of the terms cancel. ``` ```hint Test the extremes Evaluate your expression at z = 1000 and z = -1000 for both labels before trusting it. Any intermediate `exp(z)` on a large positive z is a sign of trouble. ``` ### Constraints & Assumptions - `y_true`, `z` and `sample_weight` are 1-D with the same length n. They may arrive as Python lists. - `y_true` holds only 0 and 1. `z` can be any finite float, including values far outside the range where `exp` fits in float64. - Every weight is positive, so `sum(w) > 0`. ### What a Strong Answer Covers - A derivation showing that the logit-space expression equals binary cross-entropy. - Why the computation stays finite and accurate for very large positive and negative logits. - The two reductions, with the correct denominator for the normalized one. - Reproducing the example values. - Input handling: list inputs, dtypes, mismatched shapes and the return type. ### Follow-up Questions - Why is `logaddexp(0, z)` stable where `log(1 + exp(z))` is not? - How would you extend this to a vectorized batch or multi-class setting? - How would you compute the gradient of this loss with respect to `z` without losing stability?

Overview: Implement a sample-weighted binary log loss that works directly on raw logits and stays finite for very large logit values. It tests the logit-space form of cross-entropy, numerically stable softplus evaluation, and the difference between a weighted mean and a weighted sum.

Nov 24, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement weighted_stable_log_loss(y_true, z, sample_weight, normalize=True). The inputs are 1-D arrays of shape (n,): 0/1 labels y_true, raw logits z, and positive weights sample_weight that in general do not sum to n. Return the sample-weighted log loss computed stably from the logits, so that a large |z| cannot overflow.

The function must:

  • Use the per-sample loss logaddexp(0, z_i) - y_i * z_i . Never form the probability or an explicit sigmoid.
  • With normalize=True , return sum(w*l) / sum(w) . The denominator is the sum of the weights, not n.
  • With normalize=False , return sum(w*l) .
  • Return a Python float .

For example:

y_true = [1, 0, 1, 0]
z = [2.0, -1.5, 0.3, -4.0]
sample_weight = [1, 2, 3, 4]

weighted_stable_log_loss(y_true, z, sample_weight, normalize=True)   # 0.22654200120852983
weighted_stable_log_loss(y_true, z, sample_weight, normalize=False)  # 2.265420012085298

Constraints & Assumptions

  • y_true , z and sample_weight are 1-D with the same length n. They may arrive as Python lists.
  • y_true holds only 0 and 1. z can be any finite float, including values far outside the range where exp fits in float64.
  • Every weight is positive, so sum(w) > 0 .

What a Strong Answer Covers Guidance

  • A derivation showing that the logit-space expression equals binary cross-entropy.
  • Why the computation stays finite and accurate for very large positive and negative logits.
  • The two reductions, with the correct denominator for the normalized one.
  • Reproducing the example values.
  • Input handling: list inputs, dtypes, mismatched shapes and the return type.

Follow-up Questions Guidance

  • Why is logaddexp(0, z) stable where log(1 + exp(z)) is not?
  • How would you extend this to a vectorized batch or multi-class setting?
  • How would you compute the gradient of this loss with respect to z without losing stability?