|
17 | 17 |
|
18 | 18 |
|
19 | 19 | @pytest.mark.skipif(nogpu, reason="No GPU available") |
20 | | -def test_gpu_sinkhorn(): |
| 20 | +def test_gpu_dist(): |
21 | 21 |
|
22 | 22 | rng = np.random.RandomState(0) |
23 | 23 |
|
24 | | - def describe_res(r): |
25 | | - print("min:{:.3E}, max::{:.3E}, mean::{:.3E}, std::{:.3E}".format( |
26 | | - np.min(r), np.max(r), np.mean(r), np.std(r))) |
27 | | - |
28 | 24 | for n_samples in [50, 100, 500, 1000]: |
29 | 25 | print(n_samples) |
30 | 26 | a = rng.rand(n_samples // 4, 100) |
31 | 27 | b = rng.rand(n_samples, 100) |
32 | | - time1 = time.time() |
33 | | - transport = ot.da.OTDA_sinkhorn() |
34 | | - transport.fit(a, b) |
35 | | - G1 = transport.G |
36 | | - time2 = time.time() |
37 | | - transport = ot.gpu.da.OTDA_sinkhorn() |
38 | | - transport.fit(a, b) |
39 | | - G2 = transport.G |
40 | | - time3 = time.time() |
41 | | - print("Normal sinkhorn, time: {:6.2f} sec ".format(time2 - time1)) |
42 | | - describe_res(G1) |
43 | | - print(" GPU sinkhorn, time: {:6.2f} sec ".format(time3 - time2)) |
44 | | - describe_res(G2) |
45 | | - |
46 | | - np.testing.assert_allclose(G1, G2, rtol=1e-5, atol=1e-5) |
| 28 | + |
| 29 | + M = ot.dist(a.copy(), b.copy()) |
| 30 | + M2 = ot.gpu.dist(a.copy(), b.copy()) |
| 31 | + |
| 32 | + np.testing.assert_allclose(M, M2, rtol=1e-10) |
| 33 | + |
| 34 | + M2 = ot.gpu.dist(a.copy(), b.copy(), to_numpy=False) |
47 | 35 |
|
48 | 36 |
|
49 | 37 | @pytest.mark.skipif(nogpu, reason="No GPU available") |
50 | | -def test_gpu_sinkhorn_lpl1(): |
| 38 | +def test_gpu_sinkhorn(): |
51 | 39 |
|
52 | 40 | rng = np.random.RandomState(0) |
53 | 41 |
|
54 | | - def describe_res(r): |
55 | | - print("min:{:.3E}, max:{:.3E}, mean:{:.3E}, std:{:.3E}" |
56 | | - .format(np.min(r), np.max(r), np.mean(r), np.std(r))) |
| 42 | + for n_samples in [50, 100, 500, 1000]: |
| 43 | + a = rng.rand(n_samples // 4, 100) |
| 44 | + b = rng.rand(n_samples, 100) |
| 45 | + |
| 46 | + wa = ot.unif(n_samples // 4) |
| 47 | + wb = ot.unif(n_samples) |
| 48 | + |
| 49 | + M = ot.dist(a.copy(), b.copy()) |
| 50 | + M2 = ot.gpu.dist(a.copy(), b.copy(), to_numpy=False) |
| 51 | + |
| 52 | + reg = 1 |
| 53 | + |
| 54 | + G = ot.sinkhorn(wa, wb, M, reg) |
| 55 | + G1 = ot.gpu.sinkhorn(wa, wb, M, reg) |
| 56 | + |
| 57 | + np.testing.assert_allclose(G1, G, rtol=1e-10) |
| 58 | + |
| 59 | + G2 = ot.gpu.sinkhorn(wa, wb, M2, reg, to_numpy=False) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.skipif(nogpu, reason="No GPU available") |
| 63 | +def test_gpu_sinkhorn_lpl1(): |
| 64 | + |
| 65 | + rng = np.random.RandomState(0) |
57 | 66 |
|
58 | 67 | for n_samples in [50, 100, 500]: |
59 | 68 | print(n_samples) |
60 | 69 | a = rng.rand(n_samples // 4, 100) |
61 | 70 | labels_a = np.random.randint(10, size=(n_samples // 4)) |
62 | 71 | b = rng.rand(n_samples, 100) |
63 | | - time1 = time.time() |
64 | | - transport = ot.da.OTDA_lpl1() |
65 | | - transport.fit(a, labels_a, b) |
66 | | - G1 = transport.G |
67 | | - time2 = time.time() |
68 | | - transport = ot.gpu.da.OTDA_lpl1() |
69 | | - transport.fit(a, labels_a, b) |
70 | | - G2 = transport.G |
71 | | - time3 = time.time() |
72 | | - print("Normal sinkhorn lpl1, time: {:6.2f} sec ".format( |
73 | | - time2 - time1)) |
74 | | - describe_res(G1) |
75 | | - print(" GPU sinkhorn lpl1, time: {:6.2f} sec ".format( |
76 | | - time3 - time2)) |
77 | | - describe_res(G2) |
78 | | - |
79 | | - np.testing.assert_allclose(G1, G2, rtol=1e-3, atol=1e-3) |
| 72 | + |
| 73 | + wa = ot.unif(n_samples // 4) |
| 74 | + wb = ot.unif(n_samples) |
| 75 | + |
| 76 | + M = ot.dist(a.copy(), b.copy()) |
| 77 | + M2 = ot.gpu.dist(a.copy(), b.copy(), to_numpy=False) |
| 78 | + |
| 79 | + reg = 1 |
| 80 | + |
| 81 | + G = ot.da.sinkhorn_lpl1_mm(wa, labels_a, wb, M, reg) |
| 82 | + G1 = ot.gpu.da.sinkhorn_lpl1_mm(wa, labels_a, wb, M, reg) |
| 83 | + |
| 84 | + np.testing.assert_allclose(G1, G, rtol=1e-10) |
| 85 | + |
| 86 | + G2 = ot.gpu.da.sinkhorn_lpl1_mm(wa, labels_a, wb, M2, reg, to_numpy=False) |
0 commit comments