Skip to content

Tokenizer

The tokenizer turns a raw multichannel recording into patch embeddings, one per channel and patch. It is the only place where the input samples enter the model, and where the model learns what each channel is.

What it does

  1. Patching. Each channel is cut into non-overlapping patches of patch_length samples (60 in the paper, so 15 patches for a window of 900 samples). patch_stride gives overlapping patches; None means non-overlapping.
  2. Channel embedding. Each channel has a vector of size channel_embedding_dim (64 in the paper), which tells the model which node a patch comes from. In the paper it is learned. The fMRI experiments start it from a spectral embedding of the cortical mesh (channel_embedding_init), which training then refines.
  3. Output. The tokenizer returns a TokenizerOutput with the patches, the channel embeddings and, if enabled, the time embeddings. The next modules read it (see Contextualizer).
  4. Time embedding (optional). time_embedding_type adds a Fourier embedding of the time. The paper's pretraining uses none: the time order is inferred from the causal attention. The option exists for the time-encoding ablation, with "patch" (the patch index) or "timestep" (the absolute timestep of the patch start).

Options that matter

Option Default Experiments Effect
patch_length 16 60 Samples per patch.
patch_stride None None Hop between patches; None means non-overlapping.
num_channels 1024 59412 (fMRI) Size of the channel embedding table: the largest channel index you may use.
channel_embedding_dim 64 64 Size of each channel embedding (D_p).
trainable_channel_embedding True yes Whether the channel embeddings are learned.
time_embedding_type None None None, "patch" or "timestep".

Reference

timber.modules.tokenizer

Patch tokenizer: cuts multichannel time series into patches and builds their position embeddings.

A sample is C channels x T timepoints. It is cut into P patches of patch_length samples (stride patch_stride). Each patch gets a channel embedding (per channel, trainable, optionally initialised from a given array, e.g. a geometric embedding of the sensors) and optionally a Fourier time embedding.

PatchTokenizer

Bases: Module

Patchify multichannel time series and build their position embeddings.

Parameters:

Name Type Description Default
patch_length int

Samples per patch.

required
num_channels int

Number of channels with a channel embedding (channel indices are in [0, num_channels)).

required
channel_embedding_dim int

Channels of the channel embedding.

required
patch_stride int | None

Step between patch starts (defaults to patch_length: no overlap).

None
time_embedding_type TimeEmbeddingType | None

None (no time embedding), "patch" (Fourier features of the patch index within the window) or "timestep" (Fourier features of the absolute timestep of the patch start, out of time_resolution timesteps).

None
num_time_frequencies int

Number of Fourier frequency bands of the time embedding.

0
time_resolution int

Fourier resolution (highest frequency time_resolution / 2); with "timestep" it is also the number of timesteps covered.

1
channel_embedding_init ndarray | Tensor | str | None

Optional (num_channels, channel_embedding_dim) array (or a path to a .npy file) to initialise the channel embedding.

None
trainable_channel_embedding bool

Whether the channel embedding is trained.

True
channel_embedding_dim property
channel_embedding_dim: int

Size of the channel embedding.

time_embedding_dim property
time_embedding_dim: int

2 * num_time_frequencies + 1 with a time embedding, else 0.

position_dim property
position_dim: int

Channels of TokenizerOutput.position_embeddings.

forecast_offset property
forecast_offset: int

Sample offset of the forecasting targets (see forecast_offset).

forward
forward(
    signal: Tensor,
    channel_indices: Tensor | None = None,
    timesteps: Tensor | None = None,
) -> TokenizerOutput

Cut the signal into patches and embed their positions.

Parameters:

Name Type Description Default
signal Tensor

(*batch, channel, time). Trailing timepoints that do not fill a whole patch are dropped.

required
channel_indices Tensor | None

(*batch, channel) or (channel,) indices into the channel embedding. None means channels 0 .. channel - 1.

None
timesteps Tensor | None

(*batch, time) absolute timestep of every sample; required for time_embedding_type="timestep".

None

Returns:

Type Description
TokenizerOutput

TokenizerOutput with the batch axes flattened.

TokenizerOutput dataclass

Patches and position embeddings (batch axes flattened into one).

Attributes:

Name Type Description
patches Tensor

(batch, channel, patch, patch_length) raw signal patches.

channel_embeddings Tensor

(batch, channel, channel_dim).

time_embeddings Tensor | None

(batch or 1, patch, time_dim) or None without time embedding.

num_channels property
num_channels: int

Number of channels of the batch.

num_patches property
num_patches: int

Number of patches per channel.

position_embeddings
position_embeddings() -> torch.Tensor

Channel and time embeddings of every patch, concatenated.

Returns (batch, channel, patch, channel_dim + time_dim).

fourier_features

fourier_features(
    positions: Tensor,
    num_frequencies: int,
    max_resolution: int,
) -> torch.Tensor

Fourier features of 1-D positions in [-1, 1].

Frequencies are linspace(1, max_resolution / 2, num_frequencies). The channel order is [position, sin(pi f_1 x) ... sin(pi f_K x), cos(pi f_1 x) ... cos(pi f_K x)] (same as the Perceiver Fourier encoding).

Parameters:

Name Type Description Default
positions Tensor

(num_positions,) float positions.

required
num_frequencies int

Number of frequency bands K.

required
max_resolution int

Resolution that sets the highest (Nyquist) frequency.

required

Returns:

Type Description
Tensor

(num_positions, 2 * num_frequencies + 1).

linear_positions

linear_positions(
    num_positions: int, device: device | str | None = None
) -> torch.Tensor

num_positions positions evenly spaced in [-1, 1].

num_patches

num_patches(
    num_timepoints: int,
    patch_length: int,
    patch_stride: int,
) -> int

Number of whole patches in a window of num_timepoints samples.

forecast_offset

forecast_offset(
    patch_length: int, patch_stride: int
) -> int

Extra patch shift so that a forecast target does not overlap its query patch.

0 for non-overlapping patches (patch_stride >= patch_length); otherwise the number of following patches that still overlap the query patch.