import torch


def unbatchifier(x, callback, **nargs):
    """
    Transform a mini-batch of data x of shape (n, ..) to n single samples of shape (1, ...)
    to be processed by the callback fct with nargs as optional named arguments
    :param x: mini-batch of data
    :param callback: processing function taking single sample tensors
    :param nargs: optional arguments for callback
    :return: mini-batch of processed items callback(x)
    """
    return torch.stack([callback(i.unsqueeze(0), **nargs)[0] for i in x], dim=0)