def test_no_update(): ck = CohenKappa() with pytest.raises( NotComputableError, match=r"EpochMetric must have at least one example before it can be computed" ): ck.compute()
def _test(y_pred, y, n_iters, metric_device): metric_device = torch.device(metric_device) ck = CohenKappa(device=metric_device) torch.manual_seed(10 + rank) ck.reset() ck.update((y_pred, y)) if n_iters > 1: batch_size = y.shape[0] // n_iters + 1 for i in range(n_iters): idx = i * batch_size ck.update( (y_pred[idx:idx + batch_size], y[idx:idx + batch_size])) # gather y_pred, y y_pred = idist.all_gather(y_pred) y = idist.all_gather(y) np_y = y.cpu().numpy() np_y_pred = y_pred.cpu().numpy() res = ck.compute() assert isinstance(res, float) assert cohen_kappa_score(np_y, np_y_pred) == pytest.approx(res)
def _test(metric_device): metric_device = torch.device(metric_device) ck_metric = CohenKappa(device=metric_device) torch.manual_seed(10 + rank) y_pred = torch.randint(0, 2, size=(100, 1), device=device) y = torch.randint(0, 2, size=(100, 1), device=device) ck_metric.update((y_pred, y)) # gather y_pred, y y_pred = idist.all_gather(y_pred) y = idist.all_gather(y) np_y_pred = y_pred.cpu().numpy() np_y = y.cpu().numpy() np_ck = cohen_kappa_score(np_y, np_y_pred) res = ck_metric.compute() assert res == pytest.approx(np_ck)
def test_multilabel_inputs(): ck = CohenKappa() with pytest.raises(ValueError, match=r"multilabel-indicator is not supported"): ck.reset() ck.update((torch.randint(0, 2, size=(10, 4)).long(), torch.randint(0, 2, size=(10, 4)).long())) ck.compute() with pytest.raises(ValueError, match=r"multilabel-indicator is not supported"): ck.reset() ck.update((torch.randint(0, 2, size=(10, 6)).long(), torch.randint(0, 2, size=(10, 6)).long())) ck.compute() with pytest.raises(ValueError, match=r"multilabel-indicator is not supported"): ck.reset() ck.update((torch.randint(0, 2, size=(10, 8)).long(), torch.randint(0, 2, size=(10, 8)).long())) ck.compute()