Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Sktime Pytorch forecasting NHiTS

From Leeroopedia


Knowledge Sources
Domains Time_Series, Forecasting, Deep_Learning
Last Updated 2026-02-08 08:00 GMT

Overview

NHiTS (Neural Hierarchical Interpolation for Time Series) is a multi-rate sampling architecture designed for accurate long-horizon time series forecasting with covariate support.

Description

NHiTS extends BaseModelWithCovariates and implements a hierarchical interpolation approach that decomposes forecasting into multiple frequency components using stacks of blocks with different pooling and downsampling rates. The model has been shown to increase accuracy by approximately 25% compared to N-BEATS, while also supporting static and time-varying covariates. Each stack processes input at a different temporal resolution, and the final forecast is composed by interpolating and combining block-level predictions.

Usage

Use NHiTS when you need long-horizon univariate or multivariate time series forecasting, particularly when the data exhibits multi-scale temporal patterns. It is well-suited for tasks where N-BEATS would be applicable but higher accuracy or covariate support is needed. The preferred way to instantiate the model is via the from_dataset class method.

Code Reference

Source Location

Signature

class NHiTS(BaseModelWithCovariates):
    def __init__(
        self,
        output_size: int | list[int] = 1,
        static_categoricals: list[str] | None = None,
        static_reals: list[str] | None = None,
        time_varying_categoricals_encoder: list[str] | None = None,
        time_varying_categoricals_decoder: list[str] | None = None,
        categorical_groups: dict[str, list[str]] | None = None,
        time_varying_reals_encoder: list[str] | None = None,
        time_varying_reals_decoder: list[str] | None = None,
        embedding_sizes: dict[str, tuple[int, int]] | None = None,
        embedding_paddings: list[str] | None = None,
        embedding_labels: list[str] | None = None,
        x_reals: list[str] | None = None,
        x_categoricals: list[str] | None = None,
        context_length: int = 1,
        prediction_length: int = 1,
        static_hidden_size: int | None = None,
        naive_level: bool = True,
        shared_weights: bool = True,
        activation: str = "ReLU",
        initialization: str = "lecun_normal",
        n_blocks: list[str] | None = None,
        n_layers: int | list[int] = 2,
        hidden_size: int = 512,
        pooling_sizes: list[int] | None = None,
        downsample_frequencies: list[int] | None = None,
        pooling_mode: str = "max",
        interpolation_mode: str = "linear",
        batch_normalization: bool = False,
        dropout: float = 0.0,
        learning_rate: float = 1e-2,
        log_interval: int = -1,
        log_gradient_flow: bool = False,
        log_val_interval: int = None,
        weight_decay: float = 1e-3,
        loss: MultiHorizonMetric = None,
        reduce_on_plateau_patience: int = 1000,
        backcast_loss_ratio: float = 0.0,
        logging_metrics: nn.ModuleList = None,
        **kwargs,
    ):

from_dataset Signature

@classmethod
def from_dataset(cls, dataset: TimeSeriesDataSet, **kwargs):

Import

from pytorch_forecasting.models.nhits import NHiTS

I/O Contract

Inputs

Name Type Required Description
output_size list[int] No Number of outputs per target (e.g. quantiles). Defaults to 1.
context_length int No Lookback period length. Defaults to 1.
prediction_length int No Forecast horizon length. Defaults to 1.
hidden_size int No Size of hidden layers, range 8-1024. Defaults to 512.
static_hidden_size None No Hidden size for static variables. Defaults to hidden_size.
n_blocks None No Number of blocks per stack. Defaults to [1, 1, 1].
n_layers list[int] No Layers per block or per stack. Defaults to 2.
pooling_sizes None No Pooling sizes for input per stack. Defaults to heuristic.
downsample_frequencies None No Downsample multiplier per stack. Defaults to heuristic.
pooling_mode str No One of 'max' or 'average'. Defaults to "max".
interpolation_mode str No One of 'linear', 'nearest', 'cubic-x'. Defaults to "linear".
activation str No Activation function name. Defaults to "ReLU".
dropout float No Dropout rate. Defaults to 0.0.
loss MultiHorizonMetric No Loss function. Defaults to MASE().
backcast_loss_ratio float No Weight of backcast loss vs forecast. Defaults to 0.0.
learning_rate float No Learning rate. Defaults to 1e-2.
shared_weights bool No Whether to share block weights in each stack. Defaults to True.
naive_level bool No Add naive last-observation forecast. Defaults to True.

Outputs

Name Type Description
prediction torch.Tensor Transformed forecast of shape (n_samples, prediction_length, output_size).
backcast torch.Tensor Backcast reconstruction of shape (n_samples, context_length, 1).
block_forecasts tuple[torch.Tensor] Per-block forecast decompositions for interpretation.
block_backcasts tuple[torch.Tensor] Per-block backcast decompositions for interpretation.

Usage Examples

from pytorch_forecasting import TimeSeriesDataSet
from pytorch_forecasting.models.nhits import NHiTS

# Create model from dataset (preferred approach)
model = NHiTS.from_dataset(
    dataset,
    hidden_size=256,
    n_blocks=[1, 1, 1],
    n_layers=2,
    learning_rate=1e-3,
)

# Direct instantiation
model = NHiTS(
    context_length=96,
    prediction_length=24,
    hidden_size=512,
    n_blocks=[1, 1, 1],
    pooling_mode="max",
    interpolation_mode="linear",
    activation="ReLU",
    dropout=0.1,
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment