Implementation:Sktime Pytorch forecasting TimeXer V2
| Knowledge Sources | |
|---|---|
| Domains | Time_Series, Forecasting, Deep_Learning |
| Last Updated | 2026-02-08 08:00 GMT |
Overview
TimeXerV2 is the experimental v2 implementation of the TimeXer model that extends TslibBaseModel for the redesigned pytorch-forecasting architecture.
Description
This class implements the TimeXer architecture on top of TslibBaseModel, which is the v2 base class for tslib-derived models. It separates endogenous information (from target history) and exogenous information (from all continuous covariates), applies patch-level embeddings for endogenous data and variate-level inverted embeddings for exogenous data, then processes them through a dual-attention encoder. The model obtains context_length, prediction_length, target_dim, cont_dim, and features mode from the TslibBaseModel metadata system rather than from a TimeSeriesDataSet, making it compatible with the new TslibDataModule data pipeline.
Usage
Use TimeXerV2 when working with the v2 pytorch-forecasting data pipeline (TslibDataModule and TimeSeries). It is experimental and intended for testing the redesigned architecture. It accepts a metadata dictionary from the data module that describes feature dimensions and forecast horizons.
Code Reference
Source Location
- Repository: Sktime_Pytorch_forecasting
- File: pytorch_forecasting/models/timexer/_timexer_v2.py
- Lines: 1-322
Signature
class TimeXer(TslibBaseModel):
def __init__(
self,
loss: nn.Module,
enc_in: int = None,
hidden_size: int = 512,
n_heads: int = 8,
e_layers: int = 2,
d_ff: int = 2048,
dropout: float = 0.1,
patch_length: int = 4,
factor: int = 5,
activation: str = "relu",
use_efficient_attention: bool = False,
logging_metrics: list[nn.Module] | None = None,
optimizer: Optimizer | str | None = "adam",
optimizer_params: dict | None = None,
lr_scheduler: str | None = None,
lr_scheduler_params: dict | None = None,
metadata: dict | None = None,
**kwargs: Any,
):
forward
def forward(self, x: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]:
Import
from pytorch_forecasting.models.timexer._timexer_v2 import TimeXer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| loss | nn.Module | Yes | Loss function for training (e.g., MAE, QuantileLoss) |
| enc_in | int | No | Number of input features for encoder; defaults to cont_dim from metadata |
| hidden_size | int | No | Dimension of model embeddings and hidden representations (default 512) |
| n_heads | int | No | Number of attention heads (default 8) |
| e_layers | int | No | Number of encoder layers (default 2) |
| d_ff | int | No | Dimension of feedforward network (default 2048) |
| dropout | float | No | Dropout rate (default 0.1) |
| patch_length | int | No | Length of each non-overlapping patch for endogenous tokenization (default 4) |
| factor | int | No | Factor for attention mechanism (default 5) |
| activation | str | No | Activation function: 'relu' or 'gelu' (default 'relu') |
| use_efficient_attention | bool | No | Use PyTorch native optimized SDPA (default False) |
| logging_metrics | list[nn.Module] or None | No | Metrics to log during training |
| optimizer | Optimizer or str or None | No | Optimizer to use (default 'adam') |
| optimizer_params | dict or None | No | Optimizer parameters |
| lr_scheduler | str or None | No | Learning rate scheduler name |
| lr_scheduler_params | dict or None | No | LR scheduler parameters |
| metadata | dict or None | No | Metadata from TslibDataModule describing features, dimensions, and horizons |
Forward Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x["history_cont"] | torch.Tensor | Yes | Continuous features for the historical context window |
| x["history_target"] | torch.Tensor | Yes | Target values for the historical context window |
| x["history_time_idx"] | torch.Tensor | No | Time indices for the historical window |
| x["target_scale"] | torch.Tensor | No | Scaling factors for target inverse transformation |
Outputs
| Name | Type | Description |
|---|---|---|
| prediction | dict[str, torch.Tensor] | Dictionary with key 'prediction' containing tensor of shape (batch_size, prediction_length, n_vars) |
Usage Examples
from pytorch_forecasting.models.timexer._timexer_v2 import TimeXer
from pytorch_forecasting.metrics import MAE
# Instantiate with metadata from TslibDataModule
model = TimeXer(
loss=MAE(),
hidden_size=512,
n_heads=8,
e_layers=2,
d_ff=2048,
dropout=0.1,
patch_length=4,
metadata=datamodule.metadata,
)
# Forward pass
output = model(batch_x)
predictions = output["prediction"]