braindecode.models.EEGTCNet#

class braindecode.models.EEGTCNet(n_chans=None, n_outputs=None, n_times=None, chs_info=None, input_window_seconds=None, sfreq=None, activation=<class 'torch.nn.modules.activation.ELU'>, depth_multiplier=2, filter_1=8, kern_length=64, drop_prob=0.5, depth=2, kernel_size=4, filters=12, max_norm_const=0.25, drop_prob_eeg=None, drop_prob_tcn=None, tcn_batch_norm=True)[source]#

EEGTCNet model from Ingolfsson et al (2020) [ingolfsson2020].

Convolution Recurrent

EEGTCNet Architecture

Combining EEGNet and TCN blocks.

Parameters:
  • n_chans (int) – Number of EEG channels.

  • n_outputs (int) – Number of outputs of the model. This is the number of classes in the case of classification.

  • n_times (int) – Number of time samples of the input window.

  • chs_info (list of dict) – Information about each individual EEG channel. This should be filled with info["chs"]. Refer to mne.Info for more details.

  • input_window_seconds (float) – Length of the input window in seconds.

  • sfreq (float) – Sampling frequency of the EEG recordings.

  • activation (type[Module]) – Activation function to use. Default is nn.ELU().

  • depth_multiplier (int) – Depth multiplier for the depthwise convolution. Default is 2.

  • filter_1 (int) – Number of temporal filters in the first convolutional layer. Default is 8.

  • kern_length (int) – Length of the temporal kernel in the first convolutional layer. Default is 64.

  • drop_prob (float) – Default dropout rate, used for both the EEGNet front-end and the TCN block unless overridden by drop_prob_eeg / drop_prob_tcn. Default is 0.5.

  • depth (int) – Number of residual blocks in the TCN. Default is 2.

  • kernel_size (int) – Size of the temporal convolutional kernel in the TCN. Default is 4.

  • filters (int) – Number of filters in the TCN convolutional layers. Default is 12.

  • max_norm_const (float) – Maximum L2-norm constraint imposed on weights of the last fully-connected layer. Defaults to 0.25.

  • drop_prob_eeg (float | None) – Dropout rate for the EEGNet front-end. If None (default), falls back to drop_prob. The source/paper uses 0.2 for BCI IV 2a.

  • drop_prob_tcn (float | None) – Dropout rate for the TCN block. If None (default), falls back to drop_prob. The source/paper uses 0.3 for BCI IV 2a.

  • tcn_batch_norm (bool) –

    If True (default), insert a BatchNorm1d after each TCN convolution and use a bias in the residual 1x1 downsample, matching the original Keras source. Set to False to recover the previous braindecode architecture (e.g. to load checkpoints trained before this change). Default is True.

    Added in version 1.6.1.

Raises:

ValueError – If some input signal-related parameters are not specified: and can not be inferred.

Notes

If some input signal-related parameters are not specified, there will be an attempt to infer them from the other parameters.

References

[ingolfsson2020]

Ingolfsson, T. M., Hersche, M., Wang, X., Kobayashi, N., Cavigelli, L., & Benini, L. (2020). EEG-TCNet: An accurate temporal convolutional network for embedded motor-imagery brain–machine interfaces. https://doi.org/10.48550/arXiv.2006.00622

Hugging Face Hub integration

When the optional huggingface_hub package is installed, all models automatically gain the ability to be pushed to and loaded from the Hugging Face Hub. Install with:

pip install braindecode[hub]

Pushing a model to the Hub:

from braindecode.models import EEGTCNet

# Train your model
model = EEGTCNet(n_chans=22, n_outputs=4, n_times=1000)
# ... training code ...

# Push to the Hub
model.push_to_hub(
    repo_id="username/my-eegtcnet-model",
    commit_message="Initial model upload",
)

Loading a model from the Hub:

from braindecode.models import EEGTCNet

# Load pretrained model
model = EEGTCNet.from_pretrained("username/my-eegtcnet-model")

# Load with a different number of outputs (head is rebuilt automatically)
model = EEGTCNet.from_pretrained("username/my-eegtcnet-model", n_outputs=4)

Extracting features and replacing the head:

import torch

x = torch.randn(1, model.n_chans, model.n_times)
# Extract encoder features (consistent dict across all models)
out = model(x, return_features=True)
features = out["features"]

# Replace the classification head
model.reset_head(n_outputs=10)

Saving and restoring full configuration:

import json

config = model.get_config()            # all __init__ params
with open("config.json", "w") as f:
    json.dump(config, f)

model2 = EEGTCNet.from_config(config)    # reconstruct (no weights)

All model parameters (both EEG-specific and model-specific such as dropout rates, activation functions, number of filters) are automatically saved to the Hub and restored when loading.

See Loading and Adapting Pretrained Foundation Models for a complete tutorial.

Methods

forward(x)[source]#

Forward pass of the EEGTCNet model.

Parameters:

x (Tensor) – Input tensor of shape (batch_size, n_chans, n_times).

Returns:

Output tensor of shape (batch_size, n_outputs).

Return type:

Tensor