diff options
Diffstat (limited to 'test/test_gpu.py')
-rw-r--r-- | test/test_gpu.py | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/test/test_gpu.py b/test/test_gpu.py index 24797f2..98f59f7 100644 --- a/test/test_gpu.py +++ b/test/test_gpu.py @@ -1,5 +1,6 @@ -import ot + import numpy as np +import ot import time import pytest @@ -13,16 +14,16 @@ except ImportError: @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn(): - np.random.seed(0) + rng = np.random.RandomState(0) - def describeRes(r): + def describe_res(r): print("min:{:.3E}, max::{:.3E}, mean::{:.3E}, std::{:.3E}".format( np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [50, 100, 500, 1000]: - print(n) - a = np.random.rand(n // 4, 100) - b = np.random.rand(n, 100) + for n_samples in [50, 100, 500, 1000]: + print(n_samples) + a = rng.rand(n_samples // 4, 100) + b = rng.rand(n_samples, 100) time1 = time.time() transport = ot.da.OTDA_sinkhorn() transport.fit(a, b) @@ -33,26 +34,27 @@ def test_gpu_sinkhorn(): G2 = transport.G time3 = time.time() print("Normal sinkhorn, time: {:6.2f} sec ".format(time2 - time1)) - describeRes(G1) + describe_res(G1) print(" GPU sinkhorn, time: {:6.2f} sec ".format(time3 - time2)) - describeRes(G2) + describe_res(G2) - assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) + np.testing.assert_allclose(G1, G2, rtol=1e-5, atol=1e-5) @pytest.mark.skipif(nogpu, reason="No GPU available") def test_gpu_sinkhorn_lpl1(): - np.random.seed(0) - def describeRes(r): + rng = np.random.RandomState(0) + + def describe_res(r): print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" .format(np.min(r), np.max(r), np.mean(r), np.std(r))) - for n in [50, 100, 500]: - print(n) - a = np.random.rand(n // 4, 100) - labels_a = np.random.randint(10, size=(n // 4)) - b = np.random.rand(n, 100) + for n_samples in [50, 100, 500]: + print(n_samples) + a = rng.rand(n_samples // 4, 100) + labels_a = np.random.randint(10, size=(n_samples // 4)) + b = rng.rand(n_samples, 100) time1 = time.time() transport = ot.da.OTDA_lpl1() transport.fit(a, labels_a, b) @@ -64,9 +66,9 @@ def test_gpu_sinkhorn_lpl1(): time3 = time.time() print("Normal sinkhorn lpl1, time: {:6.2f} sec ".format( time2 - time1)) - describeRes(G1) + describe_res(G1) print(" GPU sinkhorn lpl1, time: {:6.2f} sec ".format( time3 - time2)) - describeRes(G2) + describe_res(G2) - assert np.allclose(G1, G2, rtol=1e-5, atol=1e-5) + np.testing.assert_allclose(G1, G2, rtol=1e-5, atol=1e-5) |