コード例 #1
0
def test_wrong_input_shapes():
    m = GeometricMeanRelativeAbsoluteError()

    with pytest.raises(
            ValueError,
            match=r"Input data shapes should be the same, but given"):
        m.update((torch.rand(4, 1, 2), torch.rand(4, 1)))

    with pytest.raises(
            ValueError,
            match=r"Input data shapes should be the same, but given"):
        m.update((torch.rand(4, 1), torch.rand(4, 1, 2)))

    with pytest.raises(
            ValueError,
            match=r"Input data shapes should be the same, but given"):
        m.update((
            torch.rand(4, 1, 2),
            torch.rand(4, ),
        ))

    with pytest.raises(
            ValueError,
            match=r"Input data shapes should be the same, but given"):
        m.update((
            torch.rand(4, ),
            torch.rand(4, 1, 2),
        ))
コード例 #2
0
def test_integration():

    y_pred = torch.rand(size=(100,))
    y = torch.rand(size=(100,))

    batch_size = 10

    def update_fn(engine, batch):
        idx = (engine.state.iteration - 1) * batch_size
        y_true_batch = np_y[idx : idx + batch_size]
        y_pred_batch = np_y_pred[idx : idx + batch_size]
        return torch.from_numpy(y_pred_batch), torch.from_numpy(y_true_batch)

    engine = Engine(update_fn)

    m = GeometricMeanRelativeAbsoluteError()
    m.attach(engine, "gmrae")

    np_y = y.numpy().ravel()
    np_y_pred = y_pred.numpy().ravel()

    data = list(range(y_pred.shape[0] // batch_size))
    gmrae = engine.run(data, max_epochs=1).metrics["gmrae"]

    sum_errors = np.log(np.abs(np_y - np_y_pred) / np.abs(np_y - np_y.mean())).sum()
    np_len = len(y_pred)
    np_ans = np.exp(sum_errors / np_len)

    assert np_ans == pytest.approx(gmrae)
コード例 #3
0
def test_zero_sample():
    m = GeometricMeanRelativeAbsoluteError()
    with pytest.raises(
        NotComputableError,
        match=r"GeometricMeanRelativeAbsoluteError must have at least one example before it can be computed",
    ):
        m.compute()
コード例 #4
0
    def _test(n_epochs, metric_device):
        metric_device = torch.device(metric_device)
        n_iters = 80
        s = 16
        n_classes = 2

        offset = n_iters * s
        y_true = torch.rand(size=(offset * idist.get_world_size(),)).to(device)
        y_preds = torch.rand(size=(offset * idist.get_world_size(),)).to(device)

        def update(engine, i):
            return (
                y_preds[i * s + rank * offset : (i + 1) * s + rank * offset],
                y_true[i * s + rank * offset : (i + 1) * s + rank * offset],
            )

        engine = Engine(update)

        gmrae = GeometricMeanRelativeAbsoluteError(device=metric_device)
        gmrae.attach(engine, "gmrae")

        data = list(range(n_iters))
        engine.run(data=data, max_epochs=n_epochs)

        assert "gmrae" in engine.state.metrics

        res = engine.state.metrics["gmrae"]

        np_y = y_true.cpu().numpy()
        np_y_pred = y_preds.cpu().numpy()

        np_gmrae = np.exp(np.log(np.abs(np_y - np_y_pred) / np.abs(np_y - np_y.mean())).mean())

        assert pytest.approx(res, rel=1e-4) == np_gmrae
def test_wrong_input_shapes():
    m = GeometricMeanRelativeAbsoluteError()

    with pytest.raises(ValueError):
        m.update((torch.rand(4, 1, 2), torch.rand(4, 1)))

    with pytest.raises(ValueError):
        m.update((torch.rand(4, 1), torch.rand(4, 1, 2)))

    with pytest.raises(ValueError):
        m.update((torch.rand(4, 1, 2), torch.rand(4, )))

    with pytest.raises(ValueError):
        m.update((torch.rand(4, ), torch.rand(4, 1, 2)))
コード例 #6
0
def test_integration_geometric_mean_relative_absolute_error_with_output_transform(
):

    np.random.seed(1)
    size = 105
    np_y_pred = np.random.rand(size, 1)
    np_y = np.random.rand(size, 1)
    np.random.shuffle(np_y)

    np_y_sum = 0
    num_examples = 0
    num_sum_of_errors = 0
    np_gmrae = 0

    n_iters = 15
    batch_size = size // n_iters
    for i in range(n_iters + 1):
        idx = i * batch_size
        np_y_i = np_y[idx:idx + batch_size]
        np_y_pred_i = np_y_pred[idx:idx + batch_size]

        np_y_sum += np_y_i.sum()
        num_examples += np_y_i.shape[0]
        np_mean = np_y_sum / num_examples

        np_gmrae += np.log(
            np.abs(np_y_i - np_y_pred_i) / np.abs(np_y_i - np_mean)).sum()

    def update_fn(engine, batch):
        idx = (engine.state.iteration - 1) * batch_size
        y_true_batch = np_y[idx:idx + batch_size]
        y_pred_batch = np_y_pred[idx:idx + batch_size]
        return idx, torch.from_numpy(y_pred_batch), torch.from_numpy(
            y_true_batch)

    engine = Engine(update_fn)

    m = GeometricMeanRelativeAbsoluteError(
        output_transform=lambda x: (x[1], x[2]))
    m.attach(engine, "geometric_mean_relative_absolute_error")

    data = list(range(size // batch_size))
    gmrae = engine.run(
        data, max_epochs=1).metrics["geometric_mean_relative_absolute_error"]

    assert np.exp(np_gmrae / num_examples) == pytest.approx(m.compute())
コード例 #7
0
def test_geometric_mean_relative_absolute_error_2():

    np.random.seed(1)
    size = 105
    np_y_pred = np.random.rand(size, 1)
    np_y = np.random.rand(size, 1)
    np.random.shuffle(np_y)

    np_y_sum = 0
    num_examples = 0
    num_sum_of_errors = 0
    np_gmrae = 0

    m = GeometricMeanRelativeAbsoluteError()
    y_pred = torch.from_numpy(np_y_pred)
    y = torch.from_numpy(np_y)

    m.reset()
    n_iters = 15
    batch_size = size // n_iters
    for i in range(n_iters + 1):
        idx = i * batch_size
        np_y_i = np_y[idx:idx + batch_size]
        np_y_pred_i = np_y_pred[idx:idx + batch_size]

        np_y_sum += np_y_i.sum()
        num_examples += np_y_i.shape[0]
        np_mean = np_y_sum / num_examples

        np_gmrae += np.log(
            np.abs(np_y_i - np_y_pred_i) / np.abs(np_y_i - np_mean)).sum()
        m.update((y_pred[idx:idx + batch_size], y[idx:idx + batch_size]))

    assert np.exp(np_gmrae / num_examples) == pytest.approx(m.compute())
コード例 #8
0
    def _test(metric_device):
        metric_device = torch.device(metric_device)
        m = GeometricMeanRelativeAbsoluteError(device=metric_device)
        torch.manual_seed(10 + rank)

        y_pred = torch.rand(size=(100,), device=device)
        y = torch.rand(size=(100,), device=device)

        m.update((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()

        np_gmrae = np.exp(np.log(np.abs(np_y - np_y_pred) / np.abs(np_y - np_y.mean())).mean())

        assert m.compute() == pytest.approx(np_gmrae, rel=1e-4)
コード例 #9
0
def test_compute():
    size = 51
    np_y_pred = np.random.rand(size,)
    np_y = np.random.rand(size,)
    np_gmrae = np.exp(np.log(np.abs(np_y - np_y_pred) / np.abs(np_y - np_y.mean())).mean())

    m = GeometricMeanRelativeAbsoluteError()
    y_pred = torch.from_numpy(np_y_pred)
    y = torch.from_numpy(np_y)

    m.reset()
    m.update((y_pred, y))

    assert np_gmrae == pytest.approx(m.compute())