def tensor_to_im(tensor):
    """Reshape a tensor as a grayscale image stack.

    The function reshapes the tensor :attr:`x` of size
    :math:`N\\times K\\times H\\times W`
    to have shape :math:`(NK)\\times 1\\times H\\times W`.

    Args:
        tensor (:class:`torch.Tensor`): tensor to rearrange.

    Returns:
        :class:`torch.Tensor`: Reshaped tensor.
    """
    return tensor.reshape(-1, *tensor.shape[2:])[:, (None), :, :]
