import torch


def f1_score_binary(y_true, y_pred):
    """
    Binary f1. Same results as sklearn f1 binary.

    Args:
        y_true: [bs*x*y], binary
        y_pred: [bs*x*y], binary

    Returns:
        f1
    """
    intersect = torch.sum(y_true * y_pred)  # works because all multiplied by 0 gets 0
    denominator = torch.sum(y_true) + torch.sum(y_pred)  # works because all multiplied by 0 gets 0
    f1 = (2 * intersect.float()) / (denominator.float() + 1e-6)
    return f1