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:
model (torch.nn.Module)
dataset (torch.utils.data.Dataset) – Yields
(x, ...)tuples; only the first element is used.batch_size (int)
- 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_gradientsfrom Braindecode 1.4. That function seeded the random generators before loading the data. To reproduce the old default, callset_random_seeds(20240205, cuda=next(model.parameters()).is_cuda)frombraindecode.utilbefore 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().