def evaluate(model, loader, device):
    """
    Evaluate a prediction model with a given data loader.
    """
    model.eval()
    acc_sum, count = 0, 0
    for x, y in loader:
        x = x.to(device)
        y = y.to(device)
        y_pred = model(x)
        acc_sum += (y_pred.argmax(1) == y).float().mean().item() * x.size(0)
        count += x.size(0)
    return acc_sum / count