Implementation:Sktime Pytorch forecasting SeriesDecomposition
| Knowledge Sources | |
|---|---|
| Domains | Time_Series, Forecasting, Deep_Learning |
| Last Updated | 2026-02-08 08:00 GMT |
Overview
A series decomposition layer that separates time series data into trend and seasonal components using moving average filtering.
Description
SeriesDecomposition is an nn.Module that implements the series decomposition block originally introduced in the Autoformer architecture. It decomposes an input time series into two additive components: a trend component extracted by a MovingAvg filter, and a seasonal (residual) component obtained by subtracting the trend from the original signal.
The decomposition is parameterized by a single kernel_size value that controls the window of the moving average filter used for trend extraction. Larger kernel sizes produce smoother trend components and more detailed seasonal components.
Usage
Use this layer as a preprocessing component within transformer-based forecasting models (such as Autoformer, FEDformer, or DLinear) that benefit from explicit trend-seasonal decomposition. It is typically applied at multiple points within the encoder and decoder to progressively refine the decomposition.
Code Reference
Source Location
- Repository: Sktime_Pytorch_forecasting
- File: pytorch_forecasting/layers/_decomposition/_series_decomp.py
- Lines: 1-43
Signature
class SeriesDecomposition(nn.Module):
def __init__(self, kernel_size)
def forward(self, x)
Import
from pytorch_forecasting.layers._decomposition._series_decomp import SeriesDecomposition
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| kernel_size | int | Yes | Size of the moving average kernel used for trend extraction |
| x | torch.Tensor | Yes | Input time series tensor of shape (batch_size, seq_len, features) |
Outputs
| Name | Type | Description |
|---|---|---|
| seasonal | torch.Tensor | Seasonal component of shape (batch_size, seq_len, features), returned as the first element of the tuple |
| trend | torch.Tensor | Trend component of shape (batch_size, seq_len, features), returned as the second element of the tuple |
Usage Examples
import torch
from pytorch_forecasting.layers._decomposition._series_decomp import SeriesDecomposition
# Create a decomposition layer with kernel size 25
decomp = SeriesDecomposition(kernel_size=25)
# Decompose a batch of time series
x = torch.randn(32, 96, 7) # (batch, seq_len, features)
seasonal, trend = decomp(x)
# Both components have the same shape as input
print(seasonal.shape) # torch.Size([32, 96, 7])
print(trend.shape) # torch.Size([32, 96, 7])
# Verify additive decomposition: seasonal + trend == x
reconstructed = seasonal + trend
assert torch.allclose(reconstructed, x, atol=1e-6)