import torch


def cos_objective_func(source, targets):
    """ Cosine loss function.

    Args:
        source: Torch tensors.
        targets: Torch tensors.

    Returns:
        Mean cosine value of each source wrt target.
    """
    # m here for matrix.
    object_m = torch.mm(source, torch.t(targets))
    source_norm = torch.norm(source, 2, 1, keepdim=True)
    targets_norm = torch.norm(targets, 2, 1, keepdim=True)
    norm_m = torch.mm(source_norm, torch.t(targets_norm))

    return (object_m/norm_m).mean()