braindecode.models.GCNsNet#
- class braindecode.models.GCNsNet(n_outputs=None, n_chans=None, chs_info=None, n_times=None, input_window_seconds=None, sfreq=None, adjacency=None, n_features=(16, 32, 64, 128, 256, 512), cheb_orders=(2, 2, 2, 2, 2, 2), pool_sizes=(2, 2, 2, 2, 2, 2), activation=<class 'torch.nn.modules.activation.Softplus'>, drop_prob=0.0)[source]#
GCNs-Net from Hou et al. (2022) [hou2022gcns].
Graph Neural Network Channel
Every time sample of a
(n_chans, n_times)window is classified as a graph signal over the electrodes, and the logits are averaged over time. The graph is the absolute Pearson correlation between channels, \(A = |P| - I\), coarsened with Graclus so that each pooling layer merges pairs of nodes. Each layer is a Chebyshev graph convolution on the rescaled normalized Laplacian \(L = I - D^{-1/2} A D^{-1/2}\), followed by a per-node bias, batch normalization, the activation and graph max pooling; a linear layer gives the logits.- Parameters:
n_outputs (
int|None) – Number of outputs of the model. This is the number of classes in the case of classification.chs_info (
list[dict] |None) – Information about each individual EEG channel. This should be filled withinfo["chs"]. Refer tomne.Infofor more details.n_times (
int|None) – Number of time samples of the input window.input_window_seconds (
float|None) – Length of the input window in seconds.sfreq (
float|None) – Sampling frequency of the EEG recordings.adjacency (
Tensor|None) – Adjacency matrix of shape (n_chans, n_chans). If not provided, it is computed from the array passed toEEGClassifier.fit/EEGRegressor.fit, otherwise from the first input passed toforward.n_features (
tuple[int,...]) – Number of filters of each graph convolution.cheb_orders (
tuple[int,...]) – Order \(K\) of the Chebyshev polynomial of each layer.pool_sizes (
tuple[int,...]) – Graph pooling size of each layer, 1 (no pooling) or a power of 2.drop_prob (
float) – Dropout probability before the final layer.
- 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
[hou2022gcns]Hou, Y., Jia, S., Lun, X., Hao, Z., Shi, Y., Li, Y., Zeng, R., & Lv, J. (2022). GCNs-Net: A Graph Convolutional Neural Network Approach for Decoding Time-resolved EEG Motor Imagery Signals. IEEE Transactions on Neural Networks and Learning Systems. https://ieeexplore.ieee.org/abstract/document/9889159
Hugging Face Hub integration
When the optional
huggingface_hubpackage 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 GCNsNet # Train your model model = GCNsNet(n_chans=22, n_outputs=4, n_times=1000) # ... training code ... # Push to the Hub model.push_to_hub( repo_id="username/my-gcnsnet-model", commit_message="Initial model upload", )
Loading a model from the Hub:
from braindecode.models import GCNsNet # Load pretrained model model = GCNsNet.from_pretrained("username/my-gcnsnet-model") # Load with a different number of outputs (head is rebuilt automatically) model = GCNsNet.from_pretrained("username/my-gcnsnet-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 = GCNsNet.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]#
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
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.- Parameters:
x (torch.Tensor) – Input tensor of shape (batch_size, n_chans, n_times).
- Returns:
Output tensor of shape (batch_size, n_outputs).
- Return type: