braindecode.modules.VectorQuantizer#

class braindecode.modules.VectorQuantizer(dim, codebook_size, codebook_dim=None, decay=0.99, epsilon=1e-06, threshold_ema_dead_code=2, rotation_trick=True, kmeans_init=True, kmeans_iters=10)[source]#

Single-layer vector quantisation with optional rotation-trick STE.

Parameters:
  • dim (int) – Input/output feature dimensionality.

  • codebook_size (int) – Number of codebook entries.

  • codebook_dim (int | None) – Internal codebook dimensionality. If different from dim, learned projection layers are added. Defaults to dim.

  • decay (float) – EMA decay for the underlying EMACodebook.

  • epsilon (float) – Numerical stability constant for codebook EMA updates.

  • threshold_ema_dead_code (int) – Dead-code replacement threshold (see EMACodebook).

  • rotation_trick (bool) – When True the rotation-trick STE (Fifty et al., 2024) is used instead of the standard straight-through estimator.

  • kmeans_init (bool) – Whether to initialize the codebook from the first input batch.

  • kmeans_iters (int) – Number of K-means iterations used for first-batch initialization.

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.

Return type:

tuple[Tensor, Tensor, Tensor]