def fields_to_batches(d, keys_to_ignore=[]):
    """
    The input is a dict whose items are batched tensors. The output is a list of dictionaries - one
    per entry in the batch - with the slices of the tensors for that entry. Here's an example.
    Input:
    d = {"a": [[1, 2], [3,4]], "b": [1, 2]}
    Output:
    res = [{"a": [1, 2], "b": 1}, {"a": [3, 4], "b": 2}].
    """
    keys = [key for key in d.keys() if key not in keys_to_ignore]

    # Make sure all input dicts have same length. If they don't, there's a problem.
    lengths = {k: len(d[k]) for k in keys}
    if len(set(lengths.values())) != 1:
        msg = f"fields have different lengths: {lengths}."
        # If there's a doc key, add it to specify where the error is.
        if "doc_key" in d:
            msg = f"For document {d['doc_key']}, " + msg
        raise ValueError(msg)

    length = list(lengths.values())[0]
    res = [{k: d[k][i] for k in keys} for i in range(length)]
    return res