Skip to content

[ENH] Add support for nn losses to ptf-v2 (Followup on #2073)#2331

Draft
Faakhir30 wants to merge 4 commits into
sktime:mainfrom
Faakhir30:nidhi_nn_losses
Draft

[ENH] Add support for nn losses to ptf-v2 (Followup on #2073)#2331
Faakhir30 wants to merge 4 commits into
sktime:mainfrom
Faakhir30:nidhi_nn_losses

Conversation

@Faakhir30

Copy link
Copy Markdown
Member

Reference Issues/PRs

Fixes #1970

What does this implement/fix? Explain your changes.

Followups on existing PR #2073 by @Nidhicodes

By @Nidhicodes:
-Introduces NNLossAdapter, a lightweight wrapper that adapts nn.Module losses to the ptf-v2 loss/metric API.
-Enables users to pass native PyTorch losses directly to models (e.g. DLinear(loss=nn.MSELoss())) without manual wrapping.
-Ensures compatibility with BaseModel.predict() by implementing to_prediction and to_quantiles.
The adapter:
-Handles (target, weight) inputs by applying weights internally while respecting the loss’s original reduction.
-Supports multi-target predictions by splitting [B, T, N] tensors and summing losses across targets.
-Enforces point-prediction-only usage (H=1) for losses that are not horizon-aware, with clear error messages otherwise.

Further updates:

  • store in self._loss instead of self.loss
  • WIP

What should a reviewer concentrate their feedback on?

Did you add any tests for the change?

Any other comments?

PR checklist

  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG]. [BUG] - bugfix, [MNT] - CI, test framework, [ENH] - adding or improving code, [DOC] - writing or improving documentation or docstrings.
  • Added/modified tests
  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.
    To run hooks independent of commit, execute pre-commit run --all-files

@codecov

codecov Bot commented Jul 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.00699% with 10 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@c4f5aac). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pytorch_forecasting/metrics/nn_loss_adapter.py 84.48% 9 Missing ⚠️
pytorch_forecasting/tests/test_nn_loss_adapter.py 98.76% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2331   +/-   ##
=======================================
  Coverage        ?   87.22%           
=======================================
  Files           ?      169           
  Lines           ?     9895           
  Branches        ?        0           
=======================================
  Hits            ?     8631           
  Misses          ?     1264           
  Partials        ?        0           
Flag Coverage Δ
cpu 87.22% <93.00%> (?)
pytest 87.22% <93.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[ENH] Add support for nn losses to ptf-v2

3 participants