import torch


def calc_batch_loss_cross_ent_list(model, batch):
    """
    List-wise Cross-Entropy loss, described in "Baoxu Shi, Tim Weninger, ProjE: Embedding Projection for KnowledgeGraph Completion, AAAi 2017".

    :param model: A PathL model.
    :param batch: An iterable with node pairs.
    :return: List-wise cross entropy loss.
    """

    pairs, labs = batch

    # if len(one_trips) == 0:
    #     return False, None

    all_preds = model.predict_train(pairs)
    ones_mask_norm = torch.Tensor(labs / sum(labs))

    H = torch.exp(all_preds)/torch.exp(all_preds).sum()
    L = - (H * ones_mask_norm).sum()

    return True, L