Page 5 of 8~112 min topic

Loss functions

Debug shape mismatch between y_true and y_pred in the regression loss workbench

Page 5 reproduces and repairs the characteristic failure of the regression loss workbench: shape mismatch between y_true and y_pred, or silent NaN loss.

~14 min this pageDebugging

1Learn the idea

Read

Reproduce before you repair

Do not start with a speculative fix for the regression loss workbench. Force the failure on purpose, save the before output, then change one cause at a time. Retries are allowed only for transient conditions—not for bad input that will fail forever on loss-functions.

Read

Force the failure

import math
def mse(y,p):
 if len(y)!=len(p) or not y: raise ValueError('aligned non-empty arrays required')
 if not all(math.isfinite(x) for x in y+p): raise ValueError('finite values required')
 return sum((a-b)**2 for a,b in zip(y,p))/len(y)
try: mse([1],[float('nan')])
except ValueError as e: print(e)

Expected evidence: **2 for a,b in zip(y,p))/len(y) try: mse((1),(float('nan'))) except ValueError as e: print(e)


Expected output: **. If you cannot reproduce on demand, you do not yet control the failure mode for `loss-functions`.

Read

Repair with a reviewable diff

After repair, rerun the exact reproduction command. Keep the failing fixture as a regression seed for the observability page. For the regression loss workbench, remember the claim you are restoring: both losses are computed from identical residuals and the outlier effect is visible.

Read

Lab notebook: reproduce on command

Store a one-command reproduction for: shape mismatch between y_true and y_pred, or silent NaN loss. The command should use y_true/y_pred arrays including one large outlier or a minimal mutant of it. Paste the failing output into notes/failure-before.txt (or your shell scrollback as copied text). After the fix, paste notes/failure-after.txt and keep both.

Retries belong only on transient faults. If the failure is bad input, a bad allowlist, or a logic bug in the regression loss workbench, retrying will amplify cost without repairing trust around compare MSE vs MAE on the same residual set to see which outlier hurts more.

Read

Worked judgment

Classify the failure as prevent, detect, contain, or recover—using this lab’s language, not a generic poster. For loss-functions, the first fix should usually be detect+prevent at the boundary, because shape mismatch between y_true and y_pred, or silent NaN loss is cheaper to stop early than to explain in production prose.

Read

Why this stage matters for the regression loss workbench

At the debugging stage for loss-functions, the job is narrower than finishing a product demo. You are creating one progressive evidence piece about y_true/y_pred arrays including one large outlier that later pages inherit without redefining success. Keep that fixture small enough to inspect by hand, keep outputs copy-pasteable as text, and refuse to narrate this baseline as if it were a production SLA: mean prediction residual before any model.

For this page specifically, success looks like before/after evidence for the characteristic failure while still centering the user decision to compare MSE vs MAE on the same residual set to see which outlier hurts more. If you cannot point to a file, command, or assertion that proves that for the regression loss workbench, stay on this page instead of advancing.

Glossary: loss function · Glossary: gradient descent

Previous · Next

Go deeper

Before you start

Why this matters

Describe the smallest fixture that triggers shape mismatch between y_true and y_pred. Predict the first visible symptom (exception, wrong label, silent empty success). You will compare that prediction with the reproduction below.

Check your understanding

Page assessment

Answer from memory. Completion is saved from this evidence, not from opening the next page.

1. Can you reproduce the failure with a one-command fixture?
2. Did you avoid retrying non-transient bad input?
3. Is before/after evidence saved as text (not only a screenshot)?
4. Does the repair restore the metric path toward: MSE and MAE printed?

All responses are required.