def multi_unsqueeze(tensor, times, prepend=True):
    """
    Unsqueeze a tensor multiple times towards direction.
    Useful for broadcasting operations.
    :param tensor: The tensor to unsqueeze
    :param times: The number of singular dimensions to add.
    :param prepend: whether to prepend unsqueezes or append them.
    :return: The unsqueezed tensor, which is a view and not a copy.
    """
    old_shape = list(tensor.shape)
    extra_dims = [1]*times
    if prepend:
        return tensor.view(extra_dims + old_shape)
    else:
        return tensor.view(old_shape, extra_dims)