import torch


def compute_ent(confidences: torch.Tensor, reduction="mean", eps=1e-12) -> torch.Tensor:
    """
    Args:
        confidences (Tensor): a tensor of shape [N, K] of predicted confidences.
        reduction (str): specifies the reduction to apply to the output.
            - none: no reduction will be applied,
            - mean: the sum of the output will be divided by
                    the number of elements in the output.
        eps (float): small value to avoid evaluation of log(0).
    
    Returns:
        ent (Tensor): entropies for given confidences.
            - a tensor of shape [N,] when reduction is "none",
            - a tensor of shape [,] when reduction is "mean".
    """
    assert reduction in [
        "none", "mean",
    ], f"Unknown reduction = \"{reduction}\""

    ent = (confidences * torch.log(eps + confidences)).sum(1).neg() # [N,]

    if reduction == "mean":
        ent = ent.mean() # [,]

    return ent