braindecode.visualization.amplitude_gradients_per_trial#

braindecode.visualization.amplitude_gradients_per_trial(model, dataset, batch_size)[source]#

Concatenated amplitude_gradients() over every trial in a dataset.

Parameters:
Returns:

Gradients of the sum of each minibatch’s outputs, in dataset order. For a model that processes trials independently in evaluation mode, each entry is the gradient of that trial’s output.

Return type:

numpy.ndarray of shape (n_outputs, n_trials, n_chans, n_freqs)

Notes

This function replaces the dataset-level compute_amplitude_gradients from Braindecode 1.4. That function seeded the random generators before loading the data. To reproduce the old default, call set_random_seeds(20240205, cuda=next(model.parameters()).is_cuda) from braindecode.util before computing the gradients.

The old function returned gradients of the minibatch mean. To recover its scaling from the current output, divide each slice by its actual batch length, including the last batch:

gradients = amplitude_gradients_per_trial(model, dataset, batch_size)
for start in range(0, gradients.shape[1], batch_size):
    batch = gradients[:, start : start + batch_size]
    batch /= batch.shape[1]

For gradients of a single batch mean, use amplitude_gradients().

Examples using braindecode.visualization.amplitude_gradients_per_trial#

Interpretability of EEG Decoders

Interpretability of EEG Decoders