Overview
Sub-modules implementing the dense encoder-decoder building blocks for the Time-series Dense Encoder (TiDE) model.
Description
This module provides two core PyTorch nn.Module classes used within the TiDE architecture. _ResidualBlock implements a residual connection with a two-layer dense network, optional dropout, and optional layer normalization. _TideModule composes multiple residual blocks into a full TiDE encoder-decoder pipeline that flattens past observations, future covariates, and static covariates into a dense representation, encodes and decodes through stacked residual blocks, and applies a temporal decoder with a lookback skip connection.
Usage
These sub-modules are internal building blocks of the TiDE forecasting model. Use them when constructing or extending the TiDE architecture for time series forecasting with mixed covariates (past, future, and static).
Code Reference
Source Location
Signature: _ResidualBlock
class _ResidualBlock(nn.Module):
def __init__(
self,
input_dim: int,
output_dim: int,
hidden_size: int,
dropout: float,
use_layer_norm: bool,
):
Signature: _TideModule
class _TideModule(nn.Module):
def __init__(
self,
output_dim: int,
future_cov_dim: int,
static_cov_dim: int,
output_chunk_length: int,
input_chunk_length: int,
num_encoder_layers: int,
num_decoder_layers: int,
decoder_output_dim: int,
hidden_size: int,
temporal_decoder_hidden: int,
temporal_width_future: int,
use_layer_norm: bool,
dropout: float,
temporal_hidden_size_future: int,
):
Forward: _TideModule
def forward(
self, x_in: tuple[torch.Tensor, torch.Tensor | None, torch.Tensor | None]
) -> torch.Tensor:
Import
from pytorch_forecasting.models.tide.sub_modules import _ResidualBlock, _TideModule
I/O Contract
_ResidualBlock
Inputs
| Name |
Type |
Required |
Description
|
| input_dim |
int |
Yes |
Dimensionality of the input features
|
| output_dim |
int |
Yes |
Dimensionality of the output features
|
| hidden_size |
int |
Yes |
Size of the hidden layer in the dense block
|
| dropout |
float |
Yes |
Dropout rate applied after the dense layers
|
| use_layer_norm |
bool |
Yes |
Whether to apply layer normalization to the output
|
Outputs
| Name |
Type |
Description
|
| x |
torch.Tensor |
Output tensor of shape (batch_size, output_dim) with residual connection applied
|
_TideModule
Inputs
| Name |
Type |
Required |
Description
|
| output_dim |
int |
Yes |
Number of output features in the target
|
| future_cov_dim |
int |
Yes |
Number of future covariates available
|
| static_cov_dim |
int |
Yes |
Number of static covariates
|
| output_chunk_length |
int |
Yes |
Length of the output/prediction chunk
|
| input_chunk_length |
int |
Yes |
Length of the input/lookback chunk
|
| num_encoder_layers |
int |
Yes |
Number of stacked ResidualBlocks in the encoder
|
| num_decoder_layers |
int |
Yes |
Number of stacked ResidualBlocks in the decoder
|
| decoder_output_dim |
int |
Yes |
Dimensionality of the decoder output per time step
|
| hidden_size |
int |
Yes |
Hidden layer size within encoder/decoder ResidualBlocks
|
| temporal_decoder_hidden |
int |
Yes |
Hidden layer size in the temporal decoder
|
| temporal_width_future |
int |
Yes |
Embedding dimension for future covariates projection
|
| use_layer_norm |
bool |
Yes |
Whether to apply layer normalization in ResidualBlocks
|
| dropout |
float |
Yes |
Dropout rate
|
| temporal_hidden_size_future |
int |
Yes |
Hidden size for the future covariates projection ResidualBlock
|
Forward Inputs
| Name |
Type |
Required |
Description
|
| x_in |
tuple[Tensor, Tensor or None, Tensor or None] |
Yes |
Tuple of (x_past, x_future, x_static) where x_past has shape (batch_size, input_chunk_length, input_dim), x_future has shape (batch_size, output_chunk_length, future_cov_dim), x_static has shape (batch_size, static_cov_dim)
|
Outputs
| Name |
Type |
Description
|
| y |
torch.Tensor |
Output tensor of shape (batch_size, output_chunk_length, output_dim)
|
Usage Examples
import torch
from pytorch_forecasting.models.tide.sub_modules import _ResidualBlock, _TideModule
# Create a ResidualBlock
block = _ResidualBlock(
input_dim=64,
output_dim=32,
hidden_size=128,
dropout=0.1,
use_layer_norm=True,
)
x = torch.randn(16, 64)
out = block(x) # shape: (16, 32)
# Create a TiDE module
tide = _TideModule(
output_dim=1,
future_cov_dim=3,
static_cov_dim=2,
output_chunk_length=24,
input_chunk_length=96,
num_encoder_layers=2,
num_decoder_layers=2,
decoder_output_dim=16,
hidden_size=128,
temporal_decoder_hidden=64,
temporal_width_future=4,
use_layer_norm=True,
dropout=0.1,
temporal_hidden_size_future=64,
)
x_past = torch.randn(16, 96, 4) # batch, input_len, features
x_future = torch.randn(16, 24, 3) # batch, output_len, future_cov_dim
x_static = torch.randn(16, 2) # batch, static_cov_dim
y = tide((x_past, x_future, x_static)) # shape: (16, 24, 1)
Related Pages