import torch


def log_sum_exp(vec):
    """
    log sum exp function.

    Parameters
    ----------
    vec : ``torch.FloatTensor``, required.
        input vector, of shape(ins_num, from_tag_size, to_tag_size)

    Returns
    -------
    sum: ``torch.FloatTensor``.
        log sum exp results, tensor of shape (ins_num, to_tag_size)    
    """
    max_score, _ = torch.max(vec, 1)

    return max_score + torch.log(torch.sum(torch.exp(vec - max_score.unsqueeze(1).expand_as(vec)), 1))