Skip to content

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

Open
harshsomankar123-tech wants to merge 2 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 2 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 68.63636% with 69 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (main@7bcf66c). Learn more about missing BASE report.

Files with missing lines Patch % Lines
pytorch_forecasting/models/nbeats/_nbeats_v2.py 65.15% 69 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2322   +/-   ##
=======================================
  Coverage        ?   87.13%           
=======================================
  Files           ?      177           
  Lines           ?    10385           
  Branches        ?        0           
=======================================
  Hits            ?     9049           
  Misses          ?     1336           
  Partials        ?        0           
Flag Coverage Δ
cpu 87.13% <68.63%> (?)
pytest 87.13% <68.63%> (?)

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.

@harshsomankar123-tech

Copy link
Copy Markdown
Contributor Author

@phoeenniixx @fkiraly Please take a look

Comment thread Dockerfile

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have this docker file?


_tags = {
"info:name": "NBeats",
"authors": ["jdb78"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add your name as well.

from pytorch_forecasting.models.base._base_model_v2 import BaseModel


class NBeats(BaseModel):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name it NBeats_v2 to create a distinction between v1 and v2?

@phoeenniixx phoeenniixx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the docstrings

@phoeenniixx phoeenniixx added enhancement New feature or request module:models ptf-v2 Related to `pytorch-forecasting` v2 labels Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request module:models ptf-v2 Related to `pytorch-forecasting` v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH] Add v2 interface support for N-BEATS

3 participants