Skip to content

[ENH] Add v2 interface support for N-BEATS model#2322

Open
harshsomankar123-tech wants to merge 4 commits into
sktime:mainfrom
harshsomankar123-tech:add-nbeats-v2
Open

[ENH] Add v2 interface support for N-BEATS model#2322
harshsomankar123-tech wants to merge 4 commits into
sktime:mainfrom
harshsomankar123-tech:add-nbeats-v2

Conversation

@harshsomankar123-tech

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #2034.

What does this implement/fix? Explain your changes.

This PR adds v2 interface support for the N-BEATS model, following the v2 patterns recently established by DLinear, SAMformer, and TFT in the codebase.

Specifically, this implements:

  • Model Core (pytorch_forecasting/models/nbeats/_nbeats_v2.py): * Implemented NBeats inheriting from the experimental BaseModel v2.
    • Added dynamic parameter metadata extraction for lookback (context_length) and horizon (prediction_length).
    • Implemented the forward pass to return prediction, backcast, trend, seasonality, and generic forecasts.
    • Squeezed target/prediction shapes where necessary to maintain strict compatibility with 2D loss metrics (e.g., MAE, QuantileLoss, MASE) and v2 training loops.
    • Integrated an optional backcast loss scaling hook when backcast_loss_ratio > 0.
    • Preserved model interpretation functionality (plot_interpretation) and scaling transformation (transform_output).
  • Package Wrapper & Registration (pytorch_forecasting/models/nbeats/_nbeats_pkg_v2.py & __init__.py): * Created package container NBeats_pkg_v2 inheriting from Base_pkg to represent the high-level workflow wrapper.
    • Registered model parameters, metadata, and testing configurations (get_test_train_params).
    • Updated imports and exports for the new v2 components.

What should a reviewer concentrate their feedback on?

  • The implementation of the target/prediction shape squeezing to ensure robust compatibility with 2D loss metrics.
  • The integration strategy with the new BaseModel (v2) and Base_pkg classes.
  • The optional backcast loss scaling hook logic.
  • Verification that the new get_test_train_params adequately covers required testing configurations for the package wrapper.

Did you add any tests for the change?

Yes. Robust test coverage was added in tests/test_models/test_nbeats_v2.py.

The new tests cover initialization, forward pass configurations, quantile loss output shapes, step functions (training, validation, test), packaging/fitting pipelines, and interpretation plotting (including matplotlib soft-dependency checking).

Verification: All new v2 tests (7 passes, 1 skipped due to matplotlib missing) and legacy v1 tests (2 passes, 1 skipped) execute successfully without regressions.

Any other comments?

N/A

PR checklist

  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG].
  • 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 Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.52336% with 16 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@4d8d97c). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pytorch_forecasting/models/nbeats/_nbeats_v2.py 91.62% 16 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2322   +/-   ##
=======================================
  Coverage        ?   87.41%           
=======================================
  Files           ?      169           
  Lines           ?     9965           
  Branches        ?        0           
=======================================
  Hits            ?     8711           
  Misses          ?     1254           
  Partials        ?        0           
Flag Coverage Δ
cpu 87.41% <92.52%> (?)
pytest 87.41% <92.52%> (?)

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 v2 interface support for N-BEATS

1 participant