Skip to content

fix(training): pass (y_true, y_pred) to fit metrics and detach them to host - #456

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/metrics-wiring
Open

fix(training): pass (y_true, y_pred) to fit metrics and detach them to host#456
ChrisW09 wants to merge 1 commit into
mainfrom
fix/metrics-wiring

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #443

Problems

Two defects at the same two call sites (lightning_module.py:420, :476):

  1. Arguments swapped. Metrics were called as metric_fn(preds, labels), but DeepTabMetric.__call__ is documented as (y_true, y_pred). Confirmed with a probe on an LSS model: y_true received (8, 2) — the two Normal parameters — and y_pred received (8, 1), the targets. Asymmetric metrics logged wrong numbers (R2Score 0.708 correct vs 0.523 swapped). SklearnBaseLSS.evaluate() calls the same objects the correct way round, so val_crps during fit and crps from evaluate() disagreed for the same model and data.
  2. Built-in metrics crash. Every metric in deeptab.metrics is numpy-based, but the call sites passed live torch tensors: RuntimeError: Can't call numpy() on Tensor that requires grad for train_metrics even on CPU, and TypeError: can't convert mps:0 device type tensor to numpy for val_metrics on the default accelerator. So the usage documented in deeptab/metrics/__init__.py, deeptab/metrics/base.py and docs/api/metrics/index.rst could not run at all.

Fix

Call metric_fn(targets, preds) in the documented order, with .detach().cpu() applied to both before handing them to the numpy-based metrics. needs_raw handling is unchanged.

Tests

New tests/test_fit_metrics_wiring.py: a probe metric asserts the targets arrive as y_true and the distribution parameters as y_pred, plus MeanAbsoluteError used as both a train and a val metric. Verified 2 of the 3 fail on main (the third fails on a GPU/MPS runner). tests/test_metrics.py, test_observability.py and test_lss_base.py pass unchanged.

🤖 Generated with Claude Code

…o host

Two defects at the same two call sites made fit(train_metrics=/val_metrics=)
unusable and silently wrong:

- Metrics were invoked as metric_fn(preds, labels), but DeepTabMetric is
  defined and documented as __call__(y_true, y_pred). Every asymmetric
  metric therefore logged a wrong number (R2Score 0.708 correct vs 0.523
  swapped), and SklearnBaseLSS.evaluate() -- which calls the same objects
  the right way round -- disagreed with the val_* metric logged during fit
  for the same model and data.
- The shipped metrics are numpy-based, but the call sites handed them live
  torch tensors: train_metrics raised 'Can't call numpy() on Tensor that
  requires grad' even on CPU, and val_metrics raised 'can't convert mps:0
  device type tensor to numpy' on the default accelerator. The documented
  fit(..., val_metrics={'mae': MeanAbsoluteError()}) usage could not run.

Fixes #443

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] fit(train_metrics=/val_metrics=) is wired wrong: arguments are swapped and built-in metrics crash

1 participant