braindecode.modules.PatchTokenizer#

class braindecode.modules.PatchTokenizer(patch_size, n_times, emb_dim=None, learnable=False, on_non_divisible='pad', projection='conv', output_order='channel_patch')[source]#

Tokenize an EEG signal into non-overlapping temporal patches.

Transforms (batch, n_chans, n_times) into (batch, n_chans, n_patches, patch_dim) by splitting the time axis into non-overlapping patches of patch_size samples. This is the shared patch / “tokenization” step used by transformer EEG foundation models (e.g. LaBraM, CBraMod, EEG-DINO).

By default, as in the filter-bank models (FBCNet, FBMSNet), when n_times is not a multiple of patch_size the input is right zero-padded (a warning is emitted at construction). Set on_non_divisible="crop" to hard-crop the trailing samples instead, or "error" to reject non-divisible inputs. Padding/cropping is applied at forward time to the actual input length (not the construction-time n_times), so the module also accepts inputs of a different length.

Two modes:

  • non-learnable (learnable=False, default): a pure reshape, so patch_dim == patch_size and the raw samples of each patch are kept (the patch embedding, if any, lives in the model).

  • learnable (learnable=True): maps each patch to emb_dim features, so patch_dim == emb_dim. projection="conv" keeps the historical strided Conv1d behavior; projection="linear" first forms raw patches and then applies one Linear(patch_size, emb_dim) to each patch.

Parameters:
  • patch_size (int) – Number of time samples per patch.

  • n_times (int) – Number of time samples of the input, used to set up the right-padding when n_times is not a multiple of patch_size.

  • emb_dim (int, optional) – Output features per patch in learnable mode. Defaults to patch_size. Ignored when learnable=False.

  • learnable (bool, default=False) – Whether the tokenizer is a learned convolution or a fixed reshape.

  • on_non_divisible ({"pad", "crop", "error"}, default="pad") – How to handle a time dimension that is not divisible by patch_size. "pad" right-pads with zeros, "crop" drops the trailing samples, and "error" raises a ValueError.

  • projection ({"conv", "linear"}, default="conv") – Learnable projection type. Only used when learnable=True.

  • output_order ({"channel_patch", "patch_channel"}, default="channel_patch") – Axis order after the batch dimension. "channel_patch" returns (batch, n_chans, n_patches, patch_dim) and preserves the historical output. "patch_channel" returns (batch, n_patches, n_chans, patch_dim).

Examples

>>> import torch
>>> from braindecode.modules import PatchTokenizer
>>> tokenizer = PatchTokenizer(patch_size=200, n_times=1000)
>>> tokenizer(torch.randn(2, 19, 1000)).shape
torch.Size([2, 19, 5, 200])

Methods

forward(x)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.