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¶
- Patching. Each channel is cut into non-overlapping patches of
patch_lengthsamples (60 in the paper, so 15 patches for a window of 900 samples).patch_stridegives overlapping patches;Nonemeans non-overlapping. - 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. - Output. The tokenizer returns a
TokenizerOutputwith the patches, the channel embeddings and, if enabled, the time embeddings. The next modules read it (see Contextualizer). - Time embedding (optional).
time_embedding_typeadds 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 |
required |
channel_embedding_dim
|
int
|
Channels of the channel embedding. |
required |
patch_stride
|
int | None
|
Step between patch starts (defaults to |
None
|
time_embedding_type
|
TimeEmbeddingType | None
|
|
None
|
num_time_frequencies
|
int
|
Number of Fourier frequency bands of the time embedding. |
0
|
time_resolution
|
int
|
Fourier resolution (highest frequency |
1
|
channel_embedding_init
|
ndarray | Tensor | str | None
|
Optional |
None
|
trainable_channel_embedding
|
bool
|
Whether the channel embedding is trained. |
True
|
time_embedding_dim
property
¶
time_embedding_dim: int
2 * num_time_frequencies + 1 with a time embedding, else 0.
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
|
|
required |
channel_indices
|
Tensor | None
|
|
None
|
timesteps
|
Tensor | None
|
|
None
|
Returns:
| Type | Description |
|---|---|
TokenizerOutput
|
|
TokenizerOutput
dataclass
¶
Patches and position embeddings (batch axes flattened into one).
Attributes:
| Name | Type | Description |
|---|---|---|
patches |
Tensor
|
|
channel_embeddings |
Tensor
|
|
time_embeddings |
Tensor | None
|
|
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
|
|
required |
num_frequencies
|
int
|
Number of frequency bands |
required |
max_resolution
|
int
|
Resolution that sets the highest (Nyquist) frequency. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
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.