def predict_trials(module, dataset, return_targets=True, batch_size=1, num_workers=0):
    """Create trialwise predictions and optionally also return trialwise
    labels from cropped dataset given module.

    Parameters
    ----------
    module: torch.nn.Module
        A pytorch model implementing forward.
    dataset: braindecode.datasets.BaseConcatDataset
        A braindecode dataset to be predicted.
    return_targets: bool
        If True, additionally returns the trial targets.
    batch_size: int
        The batch size used to iterate the dataset.
    num_workers: int
        Number of workers used in DataLoader to iterate the dataset.

    Returns
    -------
        trial_predictions: np.ndarray
            3-dimensional array (n_trials x n_classes x n_predictions), where
            the number of predictions depend on the chosen window size and the
            receptive field of the network.
        trial_labels: np.ndarray
            2-dimensional array (n_trials x n_targets) where the number of
            targets depends on the decoding paradigm and can be either a single
            value, multiple values, or a sequence.
    """
    # Ensure the model is in evaluation mode
    module.eval()
    # we have a cropped dataset if there exists at least one trial with more
    # than one compute window
    more_than_one_window = sum(dataset.get_metadata()["i_window_in_trial"] != 0) > 0
    if not more_than_one_window:
        warnings.warn(
            "This function was designed to predict trials from "
            "cropped datasets, which typically have multiple compute "
            "windows per trial. The given dataset has exactly one "
            "window per trial."
        )
    loader = DataLoader(
        dataset=dataset,
        batch_size=batch_size,
        shuffle=False,
        num_workers=num_workers,
    )
    device = next(module.parameters()).device
    all_preds, all_ys, all_inds = [], [], []
    with torch.no_grad():
        for X, y, ind in loader:
            X = X.to(device)
            preds = module(X)
            all_preds.extend(preds.cpu().numpy().astype(np.float32))
            all_ys.extend(y.cpu().numpy().astype(np.float32))
            all_inds.extend(ind)
    preds_per_trial = trial_preds_from_window_preds(
        preds=all_preds,
        i_window_in_trials=torch.cat(all_inds[0::3]),
        i_stop_in_trials=torch.cat(all_inds[2::3]),
    )
    preds_per_trial = np.array(preds_per_trial)
    if return_targets:
        if all_ys[0].shape == ():
            all_ys = np.array(all_ys)
            ys_per_trial = all_ys[
                np.diff(torch.cat(all_inds[0::3]), prepend=[np.inf]) != 1
            ]
        else:
            ys_per_trial = trial_preds_from_window_preds(
                preds=all_ys,
                i_window_in_trials=torch.cat(all_inds[0::3]),
                i_stop_in_trials=torch.cat(all_inds[2::3]),
            )
            ys_per_trial = np.array(ys_per_trial)
        return preds_per_trial, ys_per_trial
    return preds_per_trial
Create trialwise predictions and optionally also return trialwise
    labels from cropped dataset given module.

    Parameters
    ----------
    module: torch.nn.Module
        A pytorch model implementing forward.
    dataset: braindecode.datasets.BaseConcatDataset
        A braindecode dataset to be predicted.
    return_targets: bool
        If True, additionally returns the trial targets.
    batch_size: int
        The batch size used to iterate the dataset.
    num_workers: int
        Number of workers used in DataLoader to iterate the dataset.

    Returns
    -------
        trial_predictions: np.ndarray
            3-dimensional array (n_trials x n_classes x n_predictions), where
            the number of predictions depend on the chosen window size and the
            receptive field of the network.
        trial_labels: np.ndarray
            2-dimensional array (n_trials x n_targets) where the number of
            targets depends on the decoding paradigm and can be either a single
            value, multiple values, or a sequence.
    
braindecode/training/scoring.py