def chunk(tensor, sizes, dim=0):
    """Splits the tensor according to chunks of sizes along dim.

        Arguments:
            tensor (Tensor): tensor to split.
            sizes (list): sizes of chunks
            dim (int): dimension along which to split the tensor.
    """
    if dim < 0:
        dim += tensor.dim()

    if tensor.size(dim) != sum(sizes):
        raise ValueError(
            "Chunk sizes ({}) do not match tensor size ({}) in dim {}".format(
                sum(sizes), tensor.size(dim), dim))

    nsizes = len(sizes)
    sizes = [0] + sizes
    return [tensor.narrow(dim, sizes[i], sizes[i+1]) for i in range(nsizes)]