braindecode.training.iter_trial_predictions#

braindecode.training.iter_trial_predictions(module, dataset, return_targets=True, batch_size=1, num_workers=0, *, loader_kwargs=None)[source]#

Yield cropped predictions, or (prediction, target) pairs, trial by trial.

Parameters:
  • module (torch.nn.Module) – Model implementing forward, for example the module_ of a fitted EEGClassifier or EEGRegressor. It runs in eval mode without gradients; the mode of every submodule is restored on exhaustion, error or close().

  • dataset (torch.utils.data.Dataset) – Chronological cropped windows with targets and window indices. Items with channel positions raise a ValueError; call set_return_ch_pos(False) on the dataset first.

  • return_targets (bool) – If True, yield (prediction, target) pairs.

  • batch_size (int) – DataLoader batch size.

  • num_workers (int) – DataLoader workers.

  • loader_kwargs (dict | None) – DataLoader overrides. Only batch_size, num_workers, pin_memory, persistent_workers, prefetch_factor, worker_init_fn and generator are accepted, and they replace the arguments of the same name. Other keys, such as shuffle, sampler, drop_last or collate_fn, and batch_size=None raise a ValueError, because the reconstruction needs batches of windows in recording order.

Yields:

prediction (np.ndarray or tuple of np.ndarray) – Float32 prediction of one trial, (n_outputs, n_predictions), or a pair with its float32 target: the first window’s target for scalar and vector targets, the overlap-reconstructed target for time targets (two or more dimensions per window).

Notes

The iterator is lazy. On the first next(), it records the training flag of every submodule and puts the module in eval mode. Exhaustion, an error or close() shuts down the loader workers and restores the flags. Only the forward runs under torch.no_grad; the code that reads the items keeps its own gradient mode. Do not train the module while the iterator is suspended.

Memory holds one trial, the loader batches and one window of lookahead, not the completed trials. It still grows with the trial length; use iter_trial_prediction_chunks() to bound a long trial. The bound holds only if each item is dropped after use, and list() keeps them all.

A break does not close the iterator. Wrap an early exit in contextlib.closing(), as in the example of iter_trial_prediction_chunks(). The tutorial section Predict whole trials runs the three trial functions on a trained model.

Examples using braindecode.training.iter_trial_predictions#

Cropped Decoding on BCIC IV 2a Dataset

Cropped Decoding on BCIC IV 2a Dataset