metabci.brainda.algorithms.deep_learning.base module

class metabci.brainda.algorithms.deep_learning.base.AvgPool2dWithConv(kernel_size, stride, dilation=1, padding=0)[source]

Bases: Module

Compute average pooling using a convolution, to have the dilation parameter.

Parameters:
  • kernel_size ((int,int)) – Size of the pooling region.

  • stride ((int,int)) – Stride of the pooling operation.

  • dilation (int or (int,int)) – Dilation applied to the pooling filter.

  • padding (int or (int,int)) – Padding applied before the pooling operation.

forward(x)[source]

Defines 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.

training: bool
class metabci.brainda.algorithms.deep_learning.base.Ensure4d(*args, **kwargs)[source]

Bases: Module

forward(x)[source]

Defines 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.

training: bool
class metabci.brainda.algorithms.deep_learning.base.Expression(expression_fn)[source]

Bases: Module

Compute given expression on forward pass.

Parameters:

expression_fn (callable) – Should accept variable number of objects of type torch.autograd.Variable to compute its output.

forward(*x)[source]

Defines 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.

training: bool
class metabci.brainda.algorithms.deep_learning.base.MaxNormConstraintConv2d(*args, max_norm_value=1, norm_axis=2, **kwargs)[source]

Bases: Conv2d

bias: Tensor | None
dilation: Tuple[int, ...]
forward(input)[source]

Defines 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.

groups: int
in_channels: int
kernel_size: Tuple[int, ...]
out_channels: int
output_padding: Tuple[int, ...]
padding: str | Tuple[int, ...]
padding_mode: str
stride: Tuple[int, ...]
transposed: bool
weight: Tensor
class metabci.brainda.algorithms.deep_learning.base.MaxNormConstraintLinear(*args, max_norm_value=1, norm_axis=0, **kwargs)[source]

Bases: Linear

forward(input)[source]

Defines 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.

in_features: int
out_features: int
weight: Tensor
class metabci.brainda.algorithms.deep_learning.base.NeuralNetClassifierNoLog(module, *args, criterion=<class 'torch.nn.modules.loss.NLLLoss'>, train_split=<skorch.dataset.ValidSplit object>, classes=None, **kwargs)[source]

Bases: NeuralNetClassifier

fit(X, y, **fit_params)[source]

See NeuralNet.fit.

In contrast to NeuralNet.fit, y is non-optional to avoid mistakenly forgetting about y. However, y can be set to None in case it is derived dynamically from X.

get_loss(y_pred, y_true, *args, **kwargs)[source]

Return the loss for this batch.

Parameters:
  • y_pred (torch tensor) – Predicted target values

  • y_true (torch tensor) – True target values.

  • X (input data, compatible with skorch.dataset.Dataset) –

    By default, you should be able to pass:

    • numpy arrays

    • torch tensors

    • pandas DataFrame or Series

    • scipy sparse CSR matrices

    • a dictionary of the former three

    • a list/tuple of the former three

    • a Dataset

    If this doesn’t work with your data, you have to pass a Dataset that can deal with the data.

  • training (bool (default=False)) – Whether train mode should be used or not.

class metabci.brainda.algorithms.deep_learning.base.SkorchNet(module)[source]

Bases: object

metabci.brainda.algorithms.deep_learning.base.adaptive_batch_norm(model, use_global=False)[source]
metabci.brainda.algorithms.deep_learning.base.compute_out_size(input_size: int, kernel_size: int, stride: int = 1, padding: int = 0, dilation: int = 1)[source]
metabci.brainda.algorithms.deep_learning.base.compute_same_pad1d(input_size, kernel_size, stride=1, dilation=1)[source]
metabci.brainda.algorithms.deep_learning.base.compute_same_pad2d(input_size, kernel_size, stride=(1, 1), dilation=(1, 1))[source]
metabci.brainda.algorithms.deep_learning.base.identity(x)[source]
metabci.brainda.algorithms.deep_learning.base.np_to_th(X, requires_grad=False, dtype=None, pin_memory=False, **tensor_kwargs)[source]

Convenience function to transform numpy array to torch.Tensor.

Converts X to ndarray using asarray if necessary.

Parameters:
  • X (ndarray or list or number) – Input arrays

  • requires_grad (bool) – passed on to Variable constructor

  • dtype (numpy dtype, optional) –

  • var_kwargs – passed on to Variable constructor

Returns:

var

Return type:

torch.Tensor

metabci.brainda.algorithms.deep_learning.base.squeeze_final_output(x)[source]
Removes empty dimension at end and potentially removes empty time

dimension. It does not just use squeeze as we never want to remove first dimension.

Returns:

x – squeezed tensor

Return type:

torch.Tensor

metabci.brainda.algorithms.deep_learning.base.transpose_time_to_spat(x)[source]

Swap time and spatial dimensions.

Returns:

x – tensor in which last and first dimensions are swapped

Return type:

torch.Tensor