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 fittedEEGClassifierorEEGRegressor. It runs in eval mode without gradients; the mode of every submodule is restored on exhaustion, error orclose().dataset (torch.utils.data.Dataset) – Chronological cropped windows with targets and window indices. Items with channel positions raise a
ValueError; callset_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_fnandgeneratorare accepted, and they replace the arguments of the same name. Other keys, such asshuffle,sampler,drop_lastorcollate_fn, andbatch_size=Noneraise aValueError, 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 orclose()shuts down the loader workers and restores the flags. Only the forward runs undertorch.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, andlist()keeps them all.A
breakdoes not close the iterator. Wrap an early exit incontextlib.closing(), as in the example ofiter_trial_prediction_chunks(). The tutorial section Predict whole trials runs the three trial functions on a trained model.