Exemplo n.º 1
0
    def _test(metric_device):
        engine = Engine(update)

        m = MeanPairwiseDistance(device=metric_device)
        m.attach(engine, "mpwd")

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

        assert "mpwd" in engine.state.metrics
        res = engine.state.metrics["mpwd"]

        true_res = []
        for i in range(n_iters * idist.get_world_size()):
            true_res.append(
                torch.pairwise_distance(
                    y_true[i * s : (i + 1) * s, ...], y_preds[i * s : (i + 1) * s, ...], p=m._p, eps=m._eps
                )
                .cpu()
                .numpy()
            )
        true_res = np.array(true_res).ravel()
        true_res = true_res.mean()

        assert pytest.approx(res) == true_res
Exemplo n.º 2
0
def _test_distrib_itegration(device):
    import numpy as np
    import torch.distributed as dist

    from ignite.engine import Engine

    rank = dist.get_rank()
    torch.manual_seed(12)

    n_iters = 100
    s = 50
    offset = n_iters * s

    y_true = torch.rand(offset * dist.get_world_size(), 10).to(device)
    y_preds = torch.rand(offset * dist.get_world_size(), 10).to(device)

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

    engine = Engine(update)

    m = MeanPairwiseDistance(device=device)
    m.attach(engine, "mpwd")

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

    assert "mpwd" in engine.state.metrics
    res = engine.state.metrics["mpwd"]

    true_res = []
    for i in range(n_iters * dist.get_world_size()):
        true_res.append(
            torch.pairwise_distance(
                y_true[i * s : (i + 1) * s, ...],
                y_preds[i * s : (i + 1) * s, ...],
                p=m._p,
                eps=m._eps,
            )
            .cpu()
            .numpy()
        )
    true_res = np.array(true_res).ravel()
    true_res = true_res.mean()

    assert pytest.approx(res) == true_res