Skip to content

Commit 75e7802

Browse files
committed
update tests
1 parent f45f7a6 commit 75e7802

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

ot/gpu/bregman.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ def sinkhorn_knopp(a, b, M, reg, numItermax=1000, stopThr=1e-9,
9090
9191
"""
9292

93-
a = cp.asarray(a, dtype=np.float64)
94-
b = cp.asarray(b, dtype=np.float64)
95-
M = cp.asarray(M, dtype=np.float64)
93+
a = cp.asarray(a)
94+
b = cp.asarray(b)
95+
M = cp.asarray(M)
9696

9797
if len(a) == 0:
98-
a = np.ones((M.shape[0],), dtype=np.float64) / M.shape[0]
98+
a = np.ones((M.shape[0],)) / M.shape[0]
9999
if len(b) == 0:
100-
b = np.ones((M.shape[1],), dtype=np.float64) / M.shape[1]
100+
b = np.ones((M.shape[1],)) / M.shape[1]
101101

102102
# init data
103103
Nini = len(a)

test/test_gpu.py

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,63 +17,70 @@
1717

1818

1919
@pytest.mark.skipif(nogpu, reason="No GPU available")
20-
def test_gpu_sinkhorn():
20+
def test_gpu_dist():
2121

2222
rng = np.random.RandomState(0)
2323

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-
2824
for n_samples in [50, 100, 500, 1000]:
2925
print(n_samples)
3026
a = rng.rand(n_samples // 4, 100)
3127
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)
4735

4836

4937
@pytest.mark.skipif(nogpu, reason="No GPU available")
50-
def test_gpu_sinkhorn_lpl1():
38+
def test_gpu_sinkhorn():
5139

5240
rng = np.random.RandomState(0)
5341

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)
5766

5867
for n_samples in [50, 100, 500]:
5968
print(n_samples)
6069
a = rng.rand(n_samples // 4, 100)
6170
labels_a = np.random.randint(10, size=(n_samples // 4))
6271
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

Comments
 (0)