Compute per-sample clipped log loss for confidently wrong predictions

Quick Overview

Implement a per-sample binary log-loss vector in NumPy with clipping, for inputs that include maximally wrong predictions. It tests numerical stability of logarithms, float64 rounding near 1, and how a single confident error can dominate an averaged loss.

Compute per-sample clipped log loss for confidently wrong predictions

Role: Data Scientist

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `confident_wrong_losses(y_true, y_pred, eps=1e-15)` so that it returns the per-sample clipped binary log-loss vector. `y_true` is a 0/1 array of shape `(n,)`, and `y_pred` holds $P(y=1)$ with shape `(n,)`. The inputs include maximally wrong pairs `(y=1, p=0.0)` and `(y=0, p=1.0)`. Requirements: - Clip `y_pred` to `[eps, 1 - eps]` using the `eps` argument before taking any log. - The per-sample loss is `-(y*log(p) + (1-y)*log(1-p))`. Do not reduce the vector. - Return a `float64` array of shape `(n,)` with all entries finite. - Do not call `sklearn.metrics.log_loss` (its internal epsilon differs). Example: ```python y_true = [1, 0, 1, 0] y_pred = [0.0, 1.0, 0.5, 0.25] # eps left at its default confident_wrong_losses(y_true, y_pred) # [34.53877639491068, 34.53957599234088, 0.6931471805599453, 0.2876820724517809] ``` ```hint Zero times a log Think about what `0 * log(p)` evaluates to in NumPy when `p` is exactly 0, even though the label weight is zero. ``` ```hint The two caps differ The first two expected values are close but not equal. Before trying to explain or adjust that, compute `1 - (1 - eps)` in float64. ``` ### What a Strong Answer Covers - Clipping with the passed `eps` before either log, and why both terms need it - An element-wise computation that returns the unreduced float64 vector of shape `(n,)` - Why the two maximal-error losses differ slightly - How large the capped loss is next to a typical loss, and what one such sample does to a batch mean - How `eps` sets the maximum per-sample penalty, and the values of `eps` where clipping stops working ### Follow-up Questions - Why is the per-sample log-loss vector more useful here than the reduced mean? - How does the choice of `eps` affect the maximum per-sample cost? - What happens if `eps` is set to something like `1e-20`? - How would you compute the loss from logits so that no clipping is needed?

Overview: Implement a per-sample binary log-loss vector in NumPy with clipping, for inputs that include maximally wrong predictions. It tests numerical stability of logarithms, float64 rounding near 1, and how a single confident error can dominate an averaged loss.

Nov 25, 2026
mediumData ScientistOtherMachine Learning
0
0

Implement confident_wrong_losses(y_true, y_pred, eps=1e-15) so that it returns the per-sample clipped binary log-loss vector. y_true is a 0/1 array of shape (n,), and y_pred holds P(y=1)P(y=1) with shape (n,). The inputs include maximally wrong pairs (y=1, p=0.0) and (y=0, p=1.0).

Requirements:

  • Clip y_pred to [eps, 1 - eps] using the eps argument before taking any log.
  • The per-sample loss is -(y*log(p) + (1-y)*log(1-p)) . Do not reduce the vector.
  • Return a float64 array of shape (n,) with all entries finite.
  • Do not call sklearn.metrics.log_loss (its internal epsilon differs).

Example:

y_true = [1, 0, 1, 0]
y_pred = [0.0, 1.0, 0.5, 0.25]  # eps left at its default
confident_wrong_losses(y_true, y_pred)
# [34.53877639491068, 34.53957599234088, 0.6931471805599453, 0.2876820724517809]

What a Strong Answer Covers Guidance

  • Clipping with the passed eps before either log, and why both terms need it
  • An element-wise computation that returns the unreduced float64 vector of shape (n,)
  • Why the two maximal-error losses differ slightly
  • How large the capped loss is next to a typical loss, and what one such sample does to a batch mean
  • How eps sets the maximum per-sample penalty, and the values of eps where clipping stops working

Follow-up Questions Guidance

  • Why is the per-sample log-loss vector more useful here than the reduced mean?
  • How does the choice of eps affect the maximum per-sample cost?
  • What happens if eps is set to something like 1e-20 ?
  • How would you compute the loss from logits so that no clipping is needed?