[ENH] Add NLinear model implementation#2171
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2171 +/- ##
=======================================
Coverage ? 86.86%
=======================================
Files ? 168
Lines ? 9837
Branches ? 0
=======================================
Hits ? 8545
Misses ? 1292
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…tadata handling and remove unsupported features
|
TslibBaseModel initializes metadata as This change updates those accesses to consistently use self.metadata, ensuring the normalized metadata object is used throughout the class and preventing potential crashes when metadata is not provided |
|
|
||
| feature_indices = metadata.get("feature_indices", {}) | ||
| feature_indices = self.metadata.get("feature_indices", {}) | ||
| self.cont_indices = feature_indices.get("continuous", []) |
There was a problem hiding this comment.
I think these changes are out of scope for this PR?
There was a problem hiding this comment.
I explained it in above comment 👆🏻
the change does not affect the existing behavior of the codebase. It only replaces direct access to the raw metadata argument with self.metadata, which is already normalized earlier as metadata or {}
I added it because the class was still reading from the raw argument in a few places, which could raise an attributeerror if metadata=None and .get() is called
If you prefer keeping this PR strictly scoped to the NLinear implementation, I can remove the change and open a separate PR for it just lmk
| return TslibDataModule | ||
|
|
||
| @classmethod | ||
| def get_test_dataset_from(cls, **kwargs): |
There was a problem hiding this comment.
why are you adding this method?
There was a problem hiding this comment.
where is this method coming from?
There was a problem hiding this comment.
the default test datasets include covariates and categorical features, while NLinear v2 enforces a strict target-history-only input contract
using the default dataset would therefore trigger the validation errors for unsupported inputs
this helper creates a dataset containing only time_idx, group identifiers, nd the target column so that the package tests run with inputs that match the model's constraints
I avoided modifying the shared datamodule and kept the change scoped to the model package instead
| """Return testing parameter settings for the trainer.""" | ||
| params = [ | ||
| {}, | ||
| dict(datamodule_cfg=dict(context_length=12, prediction_length=3)), |
There was a problem hiding this comment.
I think there should be some fixtures making changes to the model params itself?
There was a problem hiding this comment.
this follows the original LTSF-NLinear formulation from the paper. It is just a single linear projection from context_length to prediction_length after the normalization step
phoeenniixx
left a comment
There was a problem hiding this comment.
I have a doubt: are you sure there are no model specific params (like n_layers or something) here?
|
Ok, I looked at the implementation, it seems like a basic linear model? |
Yes sir the current implementation follows the original paper which is just a single linear projection from So there are currently no model-specific hyperparameters like n_layers |
Reference Issues/PRs
#2157
Reference implementation based on the LTSF-Linear paper:
https://github.com/cure-lab/LTSF-Linear
https://github.com/cure-lab/LTSF-Linear/blob/main/models/NLinear.py
⸻
This PR adds an implementation of the NLinear model for the PyTorch Forecasting v2 estimator API.
Main changes:
• Added NLinear model implementation using TslibBaseModel
• Implemented support for:
• point forecasting
• quantile forecasting via QuantileLoss
• optional per-channel linear layers (individual=True)
• Ensured compatibility with TslibDataModule and v2 estimator workflows
Reference paper:
https://arxiv.org/abs/2205.13504
Added a new test module:
tests/test_models/test_nlinear_v2.py
The tests cover:
• model initialization
• forward pass execution
• correct output tensor shapes
• behavior for both point forecasting and quantile forecasting
• compatibility with the v2 estimator pipeline
This implementation closely follows the structure used by existing v2 models (e.g., DLinear) to maintain consistency with the repository architecture.
Happy to adjust structure or implementation details if there are preferred patterns for new models.