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 EnEmbedding

From Leeroopedia


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

Overview

EnEmbedding is an encoder embedding module that transforms raw time series input into patch-based representations with positional encoding for downstream transformer-style models.

Description

The EnEmbedding class handles endogenous feature embeddings for time series data. It splits the input sequence into fixed-length patches using an unfold operation, projects each patch into the model dimension via a linear layer, adds sinusoidal positional encodings, and appends a learnable global token per variable. This design enables patch-level processing of multivariate time series within architectures such as TimeXer.

Usage

Use EnEmbedding when building encoder-based time series models that require patching of input sequences combined with positional encoding. It is designed for multivariate time series where each variable is independently patched and then enriched with a shared global token before being fed into attention-based encoder layers.

Code Reference

Source Location

Signature

class EnEmbedding(nn.Module):
    def __init__(self, n_vars, d_model, patch_len, dropout):
        ...

    def forward(self, x):
        ...

Import

from pytorch_forecasting.layers import EnEmbedding

I/O Contract

Inputs

__init__ Parameters

Name Type Required Description
n_vars int Yes Number of input features (variables) in the time series.
d_model int Yes Dimension of the model embedding space.
patch_len int Yes Length of each patch that the input sequence is split into.
dropout float Yes Dropout rate applied after embedding.

forward Parameters

Name Type Required Description
x torch.Tensor Yes Input tensor of shape (batch_size, seq_len, n_vars). The sequence length must be divisible by patch_len.

Outputs

Name Type Description
x torch.Tensor Embedded output tensor of shape (batch_size * n_vars, num_patches + 1, d_model), where the extra patch position corresponds to the global token.
n_vars int Number of variables in the input, passed through for downstream use.

Usage Examples

import torch
from pytorch_forecasting.layers import EnEmbedding

# Configuration
n_vars = 7       # 7 input features
d_model = 64     # embedding dimension
patch_len = 16   # each patch covers 16 time steps
dropout = 0.1

# Create the embedding layer
embedding = EnEmbedding(n_vars=n_vars, d_model=d_model, patch_len=patch_len, dropout=dropout)

# Input: batch of 32 samples, 96 time steps, 7 variables
x = torch.randn(32, 96, n_vars)

# Forward pass
embedded, num_vars = embedding(x)
# embedded shape: (32 * 7, 96 // 16 + 1, 64) = (224, 7, 64)
print(embedded.shape, num_vars)

Related Pages

Page Connections

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