import torch


def cross_entropy(predictions, targets):
    """Home-made implementation of the cross-entropy.

    The mean or sum reduction is applied outside.

    Args:
        predictions (torch.Tensor): The predictions.
        targets (torch.Tensor): The targets.
        reduction (str): The type of reduction: `mean` or `sum`.

    Returns:
        (float): The loss for all items in the mini-batch.
    """
    # Apply the log softmax.
    logsoftmax = torch.log_softmax(predictions, dim=1)

    # Compute categorical cross-entropy, summing across classes.
    loss = - (targets * logsoftmax).sum(dim=1)

    return loss