braindecode.models package¶
Some predefined network architectures for EEG decoding.
Submodules¶
braindecode.models.base module¶
-
class
braindecode.models.base.
BaseModel
[source]¶ Bases:
object
-
parameters
()[source]¶ Return parameters of underlying torch model.
Returns: parameters Return type: list of torch tensors
-
compile
(loss, optimizer, extra_monitors=None, cropped=False, iterator_seed=0)[source]¶ Setup training for this model.
Parameters: - loss (function (predictions, targets) -> torch scalar)
- optimizer (torch.optim.Optimizer or string) – Either supply an optimizer or the name of the class (e.g. ‘adam’)
- extra_monitors (List of Braindecode monitors, optional) – In case you want to monitor additional values except for loss, misclass and runtime.
- cropped (bool) – Whether to perform cropped decoding, see cropped decoding tutorial.
- iterator_seed (int) – Seed to seed the iterator random generator.
-
fit
(train_X, train_y, epochs, batch_size, input_time_length=None, validation_data=None, model_constraint=None, remember_best_column=None, scheduler=None, log_0_epoch=True)[source]¶ Fit the model using the given training data.
Will set epochs_df variable with a pandas dataframe to the history of the training process.
Parameters: - train_X (ndarray) – Training input data
- train_y (1darray) – Training labels
- epochs (int) – Number of epochs to train
- batch_size (int)
- input_time_length (int, optional) – Super crop size, what temporal size is pushed forward through the network, see cropped decoding tuturial.
- validation_data ((ndarray, 1darray), optional) – X and y for validation set if wanted
- model_constraint (object, optional) – You can supply
MaxNormDefaultConstraint
if wanted. - remember_best_column (string, optional) – In case you want to do an early stopping/reset parameters to some “best” epoch, define here the monitored value whose minimum determines the best epoch.
- scheduler (‘cosine’ or None, optional) – Whether to use cosine annealing (
CosineAnnealing
). - log_0_epoch (bool) – Whether to compute the metrics once before training as well.
Returns: Underlying braindecode
Experiment
Return type: exp
-
evaluate
(X, y)[source]¶ Evaluate, i.e., compute metrics on given inputs and targets.
Parameters: - X (ndarray) – Input data.
- y (1darray) – Targets.
Returns: result – Dictionary with result metrics.
Return type: dict
-
predict_classes
(X, threshold_for_binary_case=None, individual_crops=False)[source]¶ Predict the labels for given input data.
Parameters: - X (ndarray) – Input data.
- threshold_for_binary_case (float, optional) – In case of a model with single output, the threshold for assigning, label 0 or 1, e.g. 0.5.
Returns: pred_labels – Predicted labels per trial, optionally for each crop within trial.
Return type: 1darray or list of 1darrays
-
predict_outs
(X, individual_crops=False)[source]¶ Predict raw outputs of the network for given input.
Parameters: - X (ndarray) – Input data.
- threshold_for_binary_case (float, optional) – In case of a model with single output, the threshold for assigning, label 0 or 1, e.g. 0.5.
- individual_crops (bool)
Returns: outs_per_trial – Network outputs for each trial, optionally for each crop within trial.
Return type: 2darray or list of 2darrays
-
braindecode.models.deep4 module¶
-
class
braindecode.models.deep4.
Deep4Net
(in_chans, n_classes, input_time_length, final_conv_length, n_filters_time=25, n_filters_spat=25, filter_time_length=10, pool_time_length=3, pool_time_stride=3, n_filters_2=50, filter_length_2=10, n_filters_3=100, filter_length_3=10, n_filters_4=200, filter_length_4=10, first_nonlin=<function elu>, first_pool_mode='max', first_pool_nonlin=<function identity>, later_nonlin=<function elu>, later_pool_mode='max', later_pool_nonlin=<function identity>, drop_prob=0.5, double_time_convs=False, split_first_layer=True, batch_norm=True, batch_norm_alpha=0.1, stride_before_pool=False)[source]¶ Bases:
braindecode.models.base.BaseModel
Deep ConvNet model from [1].
References
[1] Schirrmeister, R. T., Springenberg, J. T., Fiederer, L. D. J., Glasstetter, M., Eggensperger, K., Tangermann, M., Hutter, F. & Ball, T. (2017). Deep learning with convolutional neural networks for EEG decoding and visualization. Human Brain Mapping , Aug. 2017. Online: http://dx.doi.org/10.1002/hbm.23730
braindecode.models.eegnet module¶
-
class
braindecode.models.eegnet.
Conv2dWithConstraint
(*args, max_norm=1, **kwargs)[source]¶ Bases:
torch.nn.modules.conv.Conv2d
-
class
braindecode.models.eegnet.
EEGNetv4
(in_chans, n_classes, final_conv_length='auto', input_time_length=None, pool_mode='mean', F1=8, D=2, F2=16, kernel_length=64, third_kernel_size=(8, 4), drop_prob=0.25)[source]¶ Bases:
braindecode.models.base.BaseModel
EEGNet v4 model from [EEGNet4].
Notes
This implementation is not guaranteed to be correct, has not been checked by original authors, only reimplemented from the paper description.
References
[EEGNet4] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon, S. M., Hung, C. P., & Lance, B. J. (2018). EEGNet: A Compact Convolutional Network for EEG-based Brain-Computer Interfaces. arXiv preprint arXiv:1611.08024.
-
class
braindecode.models.eegnet.
EEGNet
(in_chans, n_classes, final_conv_length='auto', input_time_length=None, pool_mode='max', second_kernel_size=(2, 32), third_kernel_size=(8, 4), drop_prob=0.25)[source]¶ Bases:
object
EEGNet model from [EEGNet].
Notes
This implementation is not guaranteed to be correct, has not been checked by original authors, only reimplemented from the paper description.
References
[EEGNet] Lawhern, V. J., Solon, A. J., Waytowich, N. R., Gordon, S. M., Hung, C. P., & Lance, B. J. (2016). EEGNet: A Compact Convolutional Network for EEG-based Brain-Computer Interfaces. arXiv preprint arXiv:1611.08024.
braindecode.models.hybrid module¶
-
class
braindecode.models.hybrid.
HybridNet
(in_chans, n_classes, input_time_length)[source]¶ Bases:
braindecode.models.base.BaseModel
Wrapper for HybridNetModule
-
class
braindecode.models.hybrid.
HybridNetModule
(in_chans, n_classes, input_time_length)[source]¶ Bases:
torch.nn.modules.module.Module
Hybrid ConvNet model from [3].
Very hardcoded at the moment.
References
[3] Schirrmeister, R. T., Springenberg, J. T., Fiederer, L. D. J., Glasstetter, M., Eggensperger, K., Tangermann, M., Hutter, F. & Ball, T. (2017). Deep learning with convolutional neural networks for EEG decoding and visualization. Human Brain Mapping , Aug. 2017. Online: http://dx.doi.org/10.1002/hbm.23730
braindecode.models.shallow_fbcsp module¶
-
class
braindecode.models.shallow_fbcsp.
ShallowFBCSPNet
(in_chans, n_classes, input_time_length=None, n_filters_time=40, filter_time_length=25, n_filters_spat=40, pool_time_length=75, pool_time_stride=15, final_conv_length=30, conv_nonlin=<function square>, pool_mode='mean', pool_nonlin=<function safe_log>, split_first_layer=True, batch_norm=True, batch_norm_alpha=0.1, drop_prob=0.5)[source]¶ Bases:
braindecode.models.base.BaseModel
Shallow ConvNet model from [2].
References
[2] Schirrmeister, R. T., Springenberg, J. T., Fiederer, L. D. J., Glasstetter, M., Eggensperger, K., Tangermann, M., Hutter, F. & Ball, T. (2017). Deep learning with convolutional neural networks for EEG decoding and visualization. Human Brain Mapping , Aug. 2017. Online: http://dx.doi.org/10.1002/hbm.23730
braindecode.models.util module¶
-
braindecode.models.util.
to_dense_prediction_model
(model, axis=(2, 3))[source]¶ Transform a sequential model with strides to a model that outputs dense predictions by removing the strides and instead inserting dilations. Modifies model in-place.
Parameters: - model
- axis (int or (int,int)) – Axis to transform (in terms of intermediate output axes) can either be 2, 3, or (2,3).
Notes
Does not yet work correctly for average pooling. Prior to version 0.1.7, there had been a bug that could move strides backwards one layer.