Chapter 3 · Diagnose signal and ship

Loss functions by task, data, and deployment constraints

Choose and debug training objectives as optimization surrogates for the real task, including imbalance, outliers, uncertainty, ranking, dense prediction, and generation.

65–90 min8 key conceptsReviewed 26 Aug 2026
01 · Production proposition

Training loss falls steadily, yet rare-event precision, segmentation quality, and forecast decisions regress after deployment.

This lesson isolates loss functions by task, data, and deployment constraints as one decision inside that system. The people affected are product users and operators; the learning data must carry event time, availability time, ownership, and version; and the operating envelope is stable gradients, representative batch composition, and compute that fits the training window.

Decision

Choose a differentiable objective whose inductive bias and gradient behavior fit the target, noise, imbalance, and output contract.

Metric

Training stability and held-out decision metrics, calibration/uncertainty, slice behavior, and resource use—not loss value alone.

Failure consequence

Training loss falls steadily, yet rare-event precision, segmentation quality, and forecast decisions regress after deployment. An unsafe release must degrade to a named baseline or the last known-good version.

02 · Intuition & prerequisites

Build the mental model before the machinery.

The core move is to treat loss functions by task, data, and deployment constraints as a contract between data, a computation, and an action. The training contract records target encoding, logits/probability expectations, reduction, weights, masking, numerical stability, gradient-scale diagnostics, and the separate evaluation metrics that gate release. The implementation becomes easier to debug once you can state which inputs exist, which state is learned, what output means, and what must remain invariant after serialization.

01

empirical risk

Define it in a hand-checkable form and name the prediction-time inputs.

02

surrogate objectives

Connect it to the production metric and identify what it cannot guarantee.

03

proper losses

Stress it with a slice, a temporal boundary, and a failure-safe alternative.

04

robust regression

Stress it with a slice, a temporal boundary, and a failure-safe alternative.

05

class weighting

Stress it with a slice, a temporal boundary, and a failure-safe alternative.

06

focal loss

Stress it with a slice, a temporal boundary, and a failure-safe alternative.

07

contrastive learning

Stress it with a slice, a temporal boundary, and a failure-safe alternative.

08

multi-objective training

Stress it with a slice, a temporal boundary, and a failure-safe alternative.

Bring forward

Lessons 1, 2, 3, 4, 5, 6 in this course.

03 · Formal treatment

Name every symbol. Check every shape.

Regularized empirical risk is the central invariant for this lesson. The formula is useful only when its inputs match the production cutoff and its output maps to an action.

Formal treatment
R^(θ)=1niL ⁣(yi,fθ(xi))+λΩ(θ)\hat{R}(\theta) = \frac{1}{n}\sum_i L\!\left(y_i, f_\theta(x_i)\right) + \lambda\Omega(\theta)

Regularized empirical risk

Symbol, shape or unit contract
SymbolMeaning / shape / unit
θtrainable parameters
Lper-example surrogate loss
f_θ(x_i)model output with declared shape and semantics
Ωregularizer
λregularization strength
Open derivation and numerical substitution

Start from the production quantity being optimized, substitute the observed values with their declared units, then isolate the model-controlled term. Preserve shape annotations at each step so broadcasting or aggregation cannot silently change the result.

  1. Write the named inputs: θ, L, f_θ(x_i), Ω, λ.
  2. Substitute one small, hand-checkable batch before vectorizing.
  3. Calculate an independent reference value and compare within a declared tolerance.
# equation → code contract
inputs = validate_shapes_and_units(batch)
value = compute_c09(inputs)
assert is_finite(value)
04 · Selection field guide

Choose from the task contract, not a favorite default.

The loss is a trainable surrogate, not automatically the product metric.

Declare target and output shapes, logits versus probabilities, reduction, weights, masks, numerical stability, and gradient behavior before comparing objectives.

Task-to-loss selection map
TaskStart withChange whenCommon failure
Binary classificationOne raw logit + BCE with logitsClass weighting or focal loss after measuring imbalance, noise, and calibrationDouble sigmoid; weights interpreted as calibrated probability
Exclusive multiclassC raw logits + cross-entropyLabel smoothing for justified regularization; cost weights for declared policySoftmax before cross-entropy; missing rare-class slices
MultilabelC independent logits + per-label BCE with logitsAsymmetric/focal variants with label-specific evidenceSoftmax across labels that may co-occur
RegressionMSE for conditional mean; MAE for medianHuber for bounded outlier influence; quantile/pinball for asymmetric targetsChoosing by validation loss without matching action cost
Counts / positive targetsPoisson, Gamma, or Tweedie deviance when assumptions fitDistributional NLL with learned uncertaintyIgnoring support, dispersion, zero inflation, or link semantics
Forecast intervalsPinball loss per quantile or a distributional likelihoodCRPS or interval objectives for a full predictive distributionCrossing quantiles and uncalibrated intervals
RankingPointwise, pairwise logistic/hinge/BPR, or listwise softmaxChoose from slate structure, feedback bias, and serving policyAssuming lower surrogate loss guarantees NDCG or utility
Retrieval / embeddingsInfoNCE, contrastive, or triplet objectiveHard-negative mining with leakage and false-negative controlsBatch composition silently defines the task
SegmentationCE/BCE plus Dice, Tversky, or focal term when justifiedBalance component gradient scales and validate small classesOptimizing background pixels or summing incomparable scales
DetectionClassification/objectness plus box-regression or IoU-family lossFocal classification for dense easy-negative imbalanceUnversioned assignment/matching rules
Language modelsMasked token cross-entropy from raw logitsPreference/distillation objectives as separately evaluated stagesPadding in the loss, wrong causal mask, or train/eval tokenizer drift
DistillationTemperature-scaled KL plus optional hard-target lossTune temperature and term weights under task metricsComparing unscaled gradients or inheriting teacher errors
05 · Three views of the idea

Calculate it small. Shape it realistically. Break it on purpose.

HAND-CALCULATED TOY

A result you can reproduce on paper

For residuals [0.2, −0.5, 0.8, 10], compare MSE, MAE, and Huber loss; then explain how their gradients treat the outlier.

  1. Write every input and unit.
  2. Substitute values into the regularized empirical risk equation above.
  3. Compare the result to one simple baseline and explain the direction of the difference.
PRODUCTION-SHAPED

The same reasoning under real constraints

Select BCE-with-logits for calibrated binary risk, focal or sampling changes for dense imbalance, quantile loss for asymmetric forecast intervals, Dice-plus-CE for small masks, and contrastive objectives for retrieval—then validate each with decision metrics.

The production record includes the data snapshot, transformation state, artifact identity, cutoff, score, decision, and the version of the policy that consumed it.

FAILURE / COUNTEREXAMPLE

The attractive result you should reject

The team uses MSE for every target, applies softmax before a numerically stable logits loss, weights rare examples twice, and assumes a lower training loss guarantees a better product decision.

Diagnostic: replay the smallest failing slice from immutable inputs, then compare each boundary rather than retuning the model.

06 · Deterministic lab

Change one assumption and make the tradeoff visible.

This lab runs predefined TypeScript only. It never executes learner code. Use the slider, numeric input, reset, live text, or table—the computation is the same.

Exact computation

Robust-loss influence lab

Increase one residual and compare how MSE, MAE, and Huber objectives react.

Primary25.233 MSE
Secondary2.491 Huber
DiagnosisMSE gradient dominated

Assumption: Residuals [0.2, −0.5, 0.8, r]; MSE is mean squared error and Huber δ=1.

Open nonvisual data table
ItemComputed stateInterpretation
MSE25.23250outlier gradient ∝ 10.0
MAE2.87500bounded gradient magnitude
Huber δ=12.49125linear outlier tail
07 · Production implications

Trace the complete operating path.

  1. 01

    Validate and version empirical risk.

  2. 02

    Compute loss functions by task, data, and deployment constraints from prediction-time-safe inputs.

  3. 03

    Persist model, feature, and configuration identities together.

  4. 04

    Serve or materialize behind explicit stable gradients, representative batch composition, and compute that fits the training window.

  5. 05

    Join telemetry to mature outcomes and retain a rollback path.

Observability

Join service health, input quality, prediction distributions, slice behavior, and mature outcomes by exact version.

Cost

Measure storage, preprocessing, compute, queueing, and human review under a representative arrival pattern.

Failure modes

The team uses MSE for every target, applies softmax before a numerically stable logits loss, weights rare examples twice, and assumes a lower training loss guarantees a better product decision. Add a detector, owner, mitigation, and stop condition for this class of failure.

Alternatives

Compare a rule, a simpler statistical baseline, and a different system boundary before adding model complexity.

07 · Check understanding

Explain the contract, not just the vocabulary.

Browser-graded checkpointPass ≥ 80%
01Is the training loss automatically the production metric?
02Why prefer Huber over MSE in some regressions?
03What common logits-loss bug harms stability?
08 · Apply in production

Loss-selection benchmark

Compare plausible objectives for one imbalanced classifier and one regression, ranking, or segmentation task under a fixed evaluation contract.

  • Target/output shapes and logits/probability contract
  • At least three objective ablations
  • Gradient-scale, masking, and numerical-stability checks
  • Independent decision metrics and calibration analysis
  • Failure-slice recommendation with rollback criteria
Production self-review0/100

Staged hints

Failure diagnosis

  • If offline numbers look impossible, audit prediction-time availability before model code.
  • If quality is sound but the contract fails, measure the exact serialized and served path.
  • If one slice regresses, preserve that slice as a permanent release gate.

Record an honest attempt to unlock the reference solution.

09 · Sources & next depth

Read primary material with a purpose.

10 · Production resolution

Return to the opening failure.

The training contract records target encoding, logits/probability expectations, reduction, weights, masking, numerical stability, gradient-scale diagnostics, and the separate evaluation metrics that gate release.

For this lesson, the release evidence is a hand-checked formal result, deterministic simulation output, a ≥80% checkpoint, the production rubric, and a named fallback. The course resolves when the system can produce a small network built from forward pass through backpropagation and optimization.

Course production assignmentNetwork from first principles