Production Engineering: Fused, Numerically-Stable Loss
Advanced classical-mlclassificationproduction-engineeringnumerical-stability
Implement:
def bce_with_logits_loss(z: np.ndarray, y: np.ndarray) -> float:
"""
Numerically stable BCE computed directly from raw logits z,
without ever computing a separate sigmoid(z) probability array.
"""
Your function should:
- Never call
sigmoid() or compute exp(z) for positive z anywhere in the implementation — only exp(-|z|).
- Match plain
bce_loss(sigmoid(z), y) on ordinary, non-extreme inputs.
- Stay finite and correct for
z values large enough that sigmoid(z) itself would already round to exactly 0.0 or 1.0.