import torch


def em_by_begin_end_index(pred_begin, pred_end, begin, end):
    """
    Calculate exact match score given the token index tensors of
    prediction boundary and true answer boundary.
    """
    batch_num = pred_begin.size(0)
    exact_correct_num = torch.sum(
        (pred_begin == begin) * (pred_end == end))
    em = exact_correct_num.item() / batch_num
    return em