Implementation:Sktime Pytorch forecasting NHiTS Sub Modules
| Knowledge Sources | |
|---|---|
| Domains | Time_Series, Forecasting, Deep_Learning |
| Last Updated | 2026-02-08 08:00 GMT |
Overview
NHiTS Sub Modules contains the internal neural architecture building blocks for the NHiTS model, including the NHiTSModule (referred to as NHiTS in code) and NHiTSBlock classes.
Description
This module implements the core computational components of the NHiTS architecture. NHiTSBlock represents a single block that applies pooling-based downsampling, an MLP, and an identity basis interpolation to produce backcast and forecast outputs at a specific temporal resolution. The NHiTSModule (class name NHiTS in sub_modules.py) orchestrates multiple blocks organized into stacks, applying residual learning where each block refines the residual of previous blocks. It also includes supporting components such as StaticFeaturesEncoder, IdentityBasis, and MLP.
Usage
These sub-modules are not typically instantiated directly by end users. They are created internally by the top-level NHiTS model class. Understanding these components is valuable when customizing the NHiTS architecture or debugging model behavior at the block level.
Code Reference
Source Location
- Repository: Sktime_Pytorch_forecasting
- File: pytorch_forecasting/models/nhits/sub_modules.py
- Lines: 1-453
NHiTSBlock Signature
class NHiTSBlock(nn.Module):
def __init__(
self,
context_length: int,
prediction_length: int,
output_size: int,
encoder_covariate_size: int,
decoder_covariate_size: int,
static_size: int,
static_hidden_size: int,
n_theta: int,
hidden_size: list[int],
pooling_sizes: int,
pooling_mode: str,
basis: nn.Module,
n_layers: int,
batch_normalization: bool,
dropout: float,
activation: str,
):
NHiTSModule (NHiTS) Signature
class NHiTS(nn.Module):
def __init__(
self,
context_length,
prediction_length,
output_size: int,
static_size,
encoder_covariate_size,
decoder_covariate_size,
static_hidden_size,
n_blocks: list,
n_layers: list,
hidden_size: list,
pooling_sizes: list,
downsample_frequencies: list,
pooling_mode,
interpolation_mode,
dropout,
activation,
initialization,
batch_normalization,
shared_weights,
naive_level: bool,
):
Import
from pytorch_forecasting.models.nhits.sub_modules import NHiTS as NHiTSModule
from pytorch_forecasting.models.nhits.sub_modules import NHiTSBlock
I/O Contract
NHiTSBlock Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| context_length | int | Yes | Length of the lookback window. |
| prediction_length | int | Yes | Length of the forecast horizon. |
| output_size | int | Yes | Number of output features per target. |
| encoder_covariate_size | int | Yes | Size of encoder time-varying covariates. |
| decoder_covariate_size | int | Yes | Size of decoder time-varying covariates. |
| static_size | int | Yes | Size of static covariates. |
| static_hidden_size | int | Yes | Hidden size for static variable encoding. |
| n_theta | int | Yes | Number of basis expansion coefficients. |
| hidden_size | list[int] | Yes | Widths of hidden MLP layers. |
| pooling_sizes | int | Yes | Pooling kernel size for input downsampling. |
| pooling_mode | str | Yes | One of 'max' or 'average'. |
| basis | nn.Module | Yes | Basis function module (e.g. IdentityBasis). |
| n_layers | int | Yes | Number of MLP layers. |
| batch_normalization | bool | Yes | Whether to apply batch normalization. |
| dropout | float | Yes | Dropout rate. |
| activation | str | Yes | Activation function name. |
NHiTSBlock Outputs
| Name | Type | Description |
|---|---|---|
| backcast | torch.Tensor | Backcast output of shape (batch, context_length, n_outputs). |
| forecast | torch.Tensor | Forecast output of shape (batch, prediction_length, sum(output_size)). |
NHiTSModule Outputs
| Name | Type | Description |
|---|---|---|
| forecast | torch.Tensor | Combined forecast from all blocks. |
| backcast | torch.Tensor | Residual backcast after all blocks. |
| block_forecasts | torch.Tensor | Stacked per-block forecasts of shape (batch, time, outputs, n_blocks). |
| block_backcasts | torch.Tensor | Stacked per-block backcasts of shape (batch, time, outputs, n_blocks). |
Usage Examples
# These modules are typically used internally by the NHiTS model.
# Direct usage example for NHiTSBlock:
from pytorch_forecasting.models.nhits.sub_modules import NHiTSBlock, IdentityBasis
basis = IdentityBasis(
backcast_size=96,
forecast_size=24,
interpolation_mode="linear",
)
block = NHiTSBlock(
context_length=96,
prediction_length=24,
output_size=[1],
encoder_covariate_size=0,
decoder_covariate_size=0,
static_size=0,
static_hidden_size=0,
n_theta=12,
hidden_size=[256, 256],
pooling_sizes=4,
pooling_mode="max",
basis=basis,
n_layers=2,
batch_normalization=False,
dropout=0.1,
activation="ReLU",
)
Related Pages
- Principle:Sktime_Pytorch_forecasting_NHiTS_Architecture
- Sktime_Pytorch_forecasting_NHiTS - Top-level NHiTS model that uses these sub-modules