def apc(x):
    """Perform average product correct, used for contact prediction.
    
    Args:
        x: np array.
    Returns:
        apc np array

"""
    a1 = x.sum(-1, keepdims=True)
    a2 = x.sum(-2, keepdims=True)
    a12 = x.sum((-1, -2), keepdims=True)

    avg = a1 * a2
    avg.div_(a12)  # in-place to reduce memory
    normalized = x - avg
    return normalized