From b13feb07eaff4d971b749663652e5f8811c1992c Mon Sep 17 00:00:00 2001 From: Kilian Fatras Date: Tue, 28 Aug 2018 18:18:37 -0700 Subject: added gaussian test --- test/test_stochastic.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/test/test_stochastic.py b/test/test_stochastic.py index f315c88..88ad666 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -137,8 +137,8 @@ def test_stochastic_dual_sgd(): # test sgd n = 10 reg = 1 - numItermax = 300000 - batch_size = 8 + numItermax = 15000 + batch_size = 10 rng = np.random.RandomState(0) x = rng.randn(n, 2) @@ -151,9 +151,9 @@ def test_stochastic_dual_sgd(): # check constratints np.testing.assert_allclose( - u, G.sum(1), atol=1e-02) # cf convergence sgd + u, G.sum(1), atol=1e-04) # cf convergence sgd np.testing.assert_allclose( - u, G.sum(0), atol=1e-02) # cf convergence sgd + u, G.sum(0), atol=1e-04) # cf convergence sgd ############################################################################# @@ -168,10 +168,11 @@ def test_dual_sgd_sinkhorn(): # test all dual algorithms n = 10 reg = 1 - nb_iter = 300000 - batch_size = 8 + nb_iter = 150000 + batch_size = 10 rng = np.random.RandomState(0) +# Test uniform x = rng.randn(n, 2) u = ot.utils.unif(n) zero = np.zeros(n) @@ -184,8 +185,36 @@ def test_dual_sgd_sinkhorn(): # check constratints np.testing.assert_allclose( - zero, (G_sgd - G_sinkhorn).sum(1), atol=1e-02) # cf convergence sgd + zero, (G_sgd - G_sinkhorn).sum(1), atol=1e-04) # cf convergence sgd + np.testing.assert_allclose( + zero, (G_sgd - G_sinkhorn).sum(0), atol=1e-04) # cf convergence sgd + np.testing.assert_allclose( + G_sgd, G_sinkhorn, atol=1e-04) # cf convergence sgd + +# Test gaussian + n = 30 + n_source = n + n_target = n + reg = 1 + numItermax = 150000 + batch_size = 30 + + a = ot.datasets.get_1D_gauss(n_source, m=15, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n_target, m=15, s=5) + X_source = np.arange(n_source,dtype=np.float64) + Y_target = np.arange(n_target,dtype=np.float64) + M = ot.dist(X_source.reshape((n_source, 1)), Y_target.reshape((n_target, 1))) + M /= M.max() + + G_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg, batch_size, + numItermax=nb_iter) + + G_sinkhorn = ot.sinkhorn(a, b, M, reg) + + # check constratints + np.testing.assert_allclose( + zero, (G_sgd - G_sinkhorn).sum(1), atol=1e-04) # cf convergence sgd np.testing.assert_allclose( - zero, (G_sgd - G_sinkhorn).sum(0), atol=1e-02) # cf convergence sgd + zero, (G_sgd - G_sinkhorn).sum(0), atol=1e-04) # cf convergence sgd np.testing.assert_allclose( - G_sgd, G_sinkhorn, atol=1e-02) # cf convergence sgd + G_sgd, G_sinkhorn, atol=1e-04) # cf convergence sgd -- cgit v1.2.3 From cd193f78d392143ea9421da0f7e55ca8b75b8a0d Mon Sep 17 00:00:00 2001 From: Kilian Fatras Date: Tue, 28 Aug 2018 18:25:40 -0700 Subject: updated README and fixed pep8 --- test/test_stochastic.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/test/test_stochastic.py b/test/test_stochastic.py index 88ad666..4bbe230 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -193,17 +193,14 @@ def test_dual_sgd_sinkhorn(): # Test gaussian n = 30 - n_source = n - n_target = n reg = 1 - numItermax = 150000 batch_size = 30 - a = ot.datasets.get_1D_gauss(n_source, m=15, s=5) # m= mean, s= std - b = ot.datasets.get_1D_gauss(n_target, m=15, s=5) - X_source = np.arange(n_source,dtype=np.float64) - Y_target = np.arange(n_target,dtype=np.float64) - M = ot.dist(X_source.reshape((n_source, 1)), Y_target.reshape((n_target, 1))) + a = ot.datasets.get_1D_gauss(n, m=15, s=5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n, m=15, s=5) + X_source = np.arange(n, dtype=np.float64) + Y_target = np.arange(n, dtype=np.float64) + M = ot.dist(X_source.reshape((n, 1)), Y_target.reshape((n, 1))) M /= M.max() G_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg, batch_size, -- cgit v1.2.3 From 37e3b29595223399ebe4710ac2bb061004814118 Mon Sep 17 00:00:00 2001 From: Kilian Fatras Date: Tue, 28 Aug 2018 18:51:28 -0700 Subject: fixed argument functions --- ot/stochastic.py | 8 ++++---- test/test_stochastic.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/ot/stochastic.py b/ot/stochastic.py index 0788f61..e33f6a0 100644 --- a/ot/stochastic.py +++ b/ot/stochastic.py @@ -435,7 +435,7 @@ def solve_semi_dual_entropic(a, b, M, reg, method, numItermax=10000, lr=None, ############################################################################## -def batch_grad_dual(M, reg, a, b, alpha, beta, batch_size, batch_alpha, +def batch_grad_dual(a, b, M, reg, alpha, beta, batch_size, batch_alpha, batch_beta): ''' Computes the partial gradient of F_\W_varepsilon @@ -528,7 +528,7 @@ def batch_grad_dual(M, reg, a, b, alpha, beta, batch_size, batch_alpha, return grad_alpha, grad_beta -def sgd_entropic_regularization(M, reg, a, b, batch_size, numItermax, lr): +def sgd_entropic_regularization(a, b, M, reg, batch_size, numItermax, lr): ''' Compute the sgd algorithm to solve the regularized discrete measures optimal transport dual problem @@ -612,7 +612,7 @@ def sgd_entropic_regularization(M, reg, a, b, batch_size, numItermax, lr): k = np.sqrt(cur_iter / 100 + 1) batch_alpha = np.random.choice(n_source, batch_size, replace=False) batch_beta = np.random.choice(n_target, batch_size, replace=False) - update_alpha, update_beta = batch_grad_dual(M, reg, a, b, cur_alpha, + update_alpha, update_beta = batch_grad_dual(a, b, M, reg, cur_alpha, cur_beta, batch_size, batch_alpha, batch_beta) cur_alpha += (lr / k) * update_alpha @@ -698,7 +698,7 @@ def solve_dual_entropic(a, b, M, reg, batch_size, numItermax=10000, lr=1, arXiv preprint arxiv:1711.02283. ''' - opt_alpha, opt_beta = sgd_entropic_regularization(M, reg, a, b, batch_size, + opt_alpha, opt_beta = sgd_entropic_regularization(a, b, M, reg, batch_size, numItermax, lr) pi = (np.exp((opt_alpha[:, None] + opt_beta[None, :] - M[:, :]) / reg) * a[:, None] * b[None, :]) diff --git a/test/test_stochastic.py b/test/test_stochastic.py index 4bbe230..f1d4825 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -196,8 +196,8 @@ def test_dual_sgd_sinkhorn(): reg = 1 batch_size = 30 - a = ot.datasets.get_1D_gauss(n, m=15, s=5) # m= mean, s= std - b = ot.datasets.get_1D_gauss(n, m=15, s=5) + a = ot.datasets.get_1D_gauss(n, 15, 5) # m= mean, s= std + b = ot.datasets.get_1D_gauss(n, 15, 5) X_source = np.arange(n, dtype=np.float64) Y_target = np.arange(n, dtype=np.float64) M = ot.dist(X_source.reshape((n, 1)), Y_target.reshape((n, 1))) -- cgit v1.2.3 From e5087799c1cf93a76579b88bda930cfcce36b084 Mon Sep 17 00:00:00 2001 From: Kilian Fatras Date: Tue, 28 Aug 2018 23:22:05 -0700 Subject: fixed test gauss --- test/test_stochastic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/test/test_stochastic.py b/test/test_stochastic.py index f1d4825..10e62c1 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -196,8 +196,8 @@ def test_dual_sgd_sinkhorn(): reg = 1 batch_size = 30 - a = ot.datasets.get_1D_gauss(n, 15, 5) # m= mean, s= std - b = ot.datasets.get_1D_gauss(n, 15, 5) + a = ot.datasets.make_1D_gauss(n, 15, 5) # m= mean, s= std + b = ot.datasets.make_1D_gauss(n, 15, 5) X_source = np.arange(n, dtype=np.float64) Y_target = np.arange(n, dtype=np.float64) M = ot.dist(X_source.reshape((n, 1)), Y_target.reshape((n, 1))) -- cgit v1.2.3 From b2b5ffc529a7a3dbc51408cd2df59617a7e49a9a Mon Sep 17 00:00:00 2001 From: Kilian Fatras Date: Tue, 28 Aug 2018 23:32:55 -0700 Subject: fixed test error --- test/test_stochastic.py | 1 + 1 file changed, 1 insertion(+) (limited to 'test/test_stochastic.py') diff --git a/test/test_stochastic.py b/test/test_stochastic.py index 10e62c1..74228a3 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -195,6 +195,7 @@ def test_dual_sgd_sinkhorn(): n = 30 reg = 1 batch_size = 30 + zero = np.zeros(n) a = ot.datasets.make_1D_gauss(n, 15, 5) # m= mean, s= std b = ot.datasets.make_1D_gauss(n, 15, 5) -- cgit v1.2.3 From fd6371cc557ba73c4f5d1142fa8de8d956a850f0 Mon Sep 17 00:00:00 2001 From: Kilian Fatras Date: Wed, 29 Aug 2018 14:06:58 -0700 Subject: replaced marginal tests --- ot/lp/__init__.py | 2 -- ot/stochastic.py | 36 +++++++++++++++++++----------------- test/test_stochastic.py | 27 ++++++++++++--------------- 3 files changed, 31 insertions(+), 34 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/ot/lp/__init__.py b/ot/lp/__init__.py index 4c0d170..5dda82a 100644 --- a/ot/lp/__init__.py +++ b/ot/lp/__init__.py @@ -18,8 +18,6 @@ from .emd_wrap import emd_c, check_result from ..utils import parmap from .cvx import barycenter -__all__=['emd', 'emd2', 'barycenter', 'cvx'] - def emd(a, b, M, numItermax=100000, log=False): """Solves the Earth Movers distance problem and returns the OT matrix diff --git a/ot/stochastic.py b/ot/stochastic.py index e33f6a0..a369ba8 100644 --- a/ot/stochastic.py +++ b/ot/stochastic.py @@ -450,24 +450,29 @@ def batch_grad_dual(a, b, M, reg, alpha, beta, batch_size, batch_alpha, \forall j in batch_alpha, grad_beta_j = beta_j * batch_size/len(alpha) - - sum_{j in batch_alpha} exp((alpha_i + beta_j - M_{i,j})/reg) + sum_{i in batch_alpha} exp((alpha_i + beta_j - M_{i,j})/reg) * a_i * b_j where : - M is the (ns,nt) metric cost matrix - alpha, beta are dual variables in R^ixR^J - reg is the regularization term - - batch_alpha and batch_beta are list of index + - batch_alpha and batch_beta are lists of index + - a and b are source and target weights (sum to 1) + The algorithm used for solving the dual problem is the SGD algorithm as proposed in [19]_ [alg.1] Parameters ---------- - - reg : float number, - Regularization term > 0 + a : np.ndarray(ns,), + source measure + b : np.ndarray(nt,), + target measure M : np.ndarray(ns, nt), cost matrix + reg : float number, + Regularization term > 0 alpha : np.ndarray(ns,) dual variable beta : np.ndarray(nt,) @@ -516,8 +521,8 @@ def batch_grad_dual(a, b, M, reg, alpha, beta, batch_size, batch_alpha, ''' G = - (np.exp((alpha[batch_alpha, None] + beta[None, batch_beta] - - M[batch_alpha, :][:, batch_beta]) / reg) * a[batch_alpha, None] * - b[None, batch_beta]) + M[batch_alpha, :][:, batch_beta]) / reg) * + a[batch_alpha, None] * b[None, batch_beta]) grad_beta = np.zeros(np.shape(M)[1]) grad_alpha = np.zeros(np.shape(M)[0]) grad_beta[batch_beta] = (b[batch_beta] * len(batch_alpha) / np.shape(M)[0] + @@ -548,23 +553,20 @@ def sgd_entropic_regularization(a, b, M, reg, batch_size, numItermax, lr): Parameters ---------- - + a : np.ndarray(ns,), + source measure + b : np.ndarray(nt,), + target measure M : np.ndarray(ns, nt), cost matrix reg : float number, Regularization term > 0 - alpha : np.ndarray(ns,) - dual variable - beta : np.ndarray(nt,) - dual variable batch_size : int number size of the batch numItermax : int number number of iteration lr : float number learning rate - alternate : bool, optional - alternating algorithm Returns ------- @@ -591,8 +593,8 @@ def sgd_entropic_regularization(a, b, M, reg, batch_size, numItermax, lr): >>> Y_target = rng.randn(n_target, 2) >>> M = ot.dist(X_source, Y_target) >>> sgd_dual_pi, log = stochastic.solve_dual_entropic(a, b, M, reg, - batch_size, - numItermax, lr, log) + batch_size, + numItermax, lr, log) >>> print(log['alpha'], log['beta']) >>> print(sgd_dual_pi) @@ -609,7 +611,7 @@ def sgd_entropic_regularization(a, b, M, reg, batch_size, numItermax, lr): cur_alpha = np.zeros(n_source) cur_beta = np.zeros(n_target) for cur_iter in range(numItermax): - k = np.sqrt(cur_iter / 100 + 1) + k = np.sqrt(cur_iter + 1) batch_alpha = np.random.choice(n_source, batch_size, replace=False) batch_beta = np.random.choice(n_target, batch_size, replace=False) update_alpha, update_beta = batch_grad_dual(a, b, M, reg, cur_alpha, diff --git a/test/test_stochastic.py b/test/test_stochastic.py index 74228a3..0128317 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -97,7 +97,6 @@ def test_sag_asgd_sinkhorn(): x = rng.randn(n, 2) u = ot.utils.unif(n) - zero = np.zeros(n) M = ot.dist(x, x) G_asgd = ot.stochastic.solve_semi_dual_entropic(u, u, M, reg, "asgd", @@ -108,13 +107,13 @@ def test_sag_asgd_sinkhorn(): # check constratints np.testing.assert_allclose( - zero, (G_sag - G_sinkhorn).sum(1), atol=1e-03) # cf convergence sag + G_sag.sum(1), G_sinkhorn.sum(1), atol=1e-03) np.testing.assert_allclose( - zero, (G_sag - G_sinkhorn).sum(0), atol=1e-03) # cf convergence sag + G_sag.sum(0), G_sinkhorn.sum(0), atol=1e-03) np.testing.assert_allclose( - zero, (G_asgd - G_sinkhorn).sum(1), atol=1e-03) # cf convergence asgd + G_asgd.sum(1), G_sinkhorn.sum(1), atol=1e-03) np.testing.assert_allclose( - zero, (G_asgd - G_sinkhorn).sum(0), atol=1e-03) # cf convergence asgd + G_asgd.sum(0), G_sinkhorn.sum(0), atol=1e-03) np.testing.assert_allclose( G_sag, G_sinkhorn, atol=1e-03) # cf convergence sag np.testing.assert_allclose( @@ -151,9 +150,9 @@ def test_stochastic_dual_sgd(): # check constratints np.testing.assert_allclose( - u, G.sum(1), atol=1e-04) # cf convergence sgd + u, G.sum(1), atol=1e-03) # cf convergence sgd np.testing.assert_allclose( - u, G.sum(0), atol=1e-04) # cf convergence sgd + u, G.sum(0), atol=1e-03) # cf convergence sgd ############################################################################# @@ -175,7 +174,6 @@ def test_dual_sgd_sinkhorn(): # Test uniform x = rng.randn(n, 2) u = ot.utils.unif(n) - zero = np.zeros(n) M = ot.dist(x, x) G_sgd = ot.stochastic.solve_dual_entropic(u, u, M, reg, batch_size, @@ -185,17 +183,16 @@ def test_dual_sgd_sinkhorn(): # check constratints np.testing.assert_allclose( - zero, (G_sgd - G_sinkhorn).sum(1), atol=1e-04) # cf convergence sgd + G_sgd.sum(1), G_sinkhorn.sum(1), atol=1e-03) np.testing.assert_allclose( - zero, (G_sgd - G_sinkhorn).sum(0), atol=1e-04) # cf convergence sgd + G_sgd.sum(0), G_sinkhorn.sum(0), atol=1e-03) np.testing.assert_allclose( - G_sgd, G_sinkhorn, atol=1e-04) # cf convergence sgd + G_sgd, G_sinkhorn, atol=1e-03) # cf convergence sgd # Test gaussian n = 30 reg = 1 batch_size = 30 - zero = np.zeros(n) a = ot.datasets.make_1D_gauss(n, 15, 5) # m= mean, s= std b = ot.datasets.make_1D_gauss(n, 15, 5) @@ -211,8 +208,8 @@ def test_dual_sgd_sinkhorn(): # check constratints np.testing.assert_allclose( - zero, (G_sgd - G_sinkhorn).sum(1), atol=1e-04) # cf convergence sgd + G_sgd.sum(1), G_sinkhorn.sum(1), atol=1e-03) np.testing.assert_allclose( - zero, (G_sgd - G_sinkhorn).sum(0), atol=1e-04) # cf convergence sgd + G_sgd.sum(0), G_sinkhorn.sum(0), atol=1e-03) np.testing.assert_allclose( - G_sgd, G_sinkhorn, atol=1e-04) # cf convergence sgd + G_sgd, G_sinkhorn, atol=1e-03) # cf convergence sgd -- cgit v1.2.3 From 7786e8502ea010a52918861014ab0e7a934a7d45 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Fri, 14 Sep 2018 08:58:56 +0200 Subject: correct path function in example --- test/test_stochastic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/test/test_stochastic.py b/test/test_stochastic.py index 0128317..62cf82a 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -92,7 +92,7 @@ def test_sag_asgd_sinkhorn(): # test all algorithms n = 15 reg = 1 - nb_iter = 300000 + nb_iter = 30000 rng = np.random.RandomState(0) x = rng.randn(n, 2) @@ -167,7 +167,7 @@ def test_dual_sgd_sinkhorn(): # test all dual algorithms n = 10 reg = 1 - nb_iter = 150000 + nb_iter = 15000 batch_size = 10 rng = np.random.RandomState(0) -- cgit v1.2.3 From ccbe274fd9554492bb88ddaf530c2800a8dc3418 Mon Sep 17 00:00:00 2001 From: Rémi Flamary Date: Fri, 14 Sep 2018 09:03:07 +0200 Subject: speedup test stochastic --- test/test_stochastic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/test_stochastic.py') diff --git a/test/test_stochastic.py b/test/test_stochastic.py index 62cf82a..f0f3fc8 100644 --- a/test/test_stochastic.py +++ b/test/test_stochastic.py @@ -32,7 +32,7 @@ def test_stochastic_sag(): # test sag n = 15 reg = 1 - numItermax = 300000 + numItermax = 30000 rng = np.random.RandomState(0) x = rng.randn(n, 2) @@ -62,7 +62,7 @@ def test_stochastic_asgd(): # test asgd n = 15 reg = 1 - numItermax = 300000 + numItermax = 100000 rng = np.random.RandomState(0) x = rng.randn(n, 2) @@ -92,7 +92,7 @@ def test_sag_asgd_sinkhorn(): # test all algorithms n = 15 reg = 1 - nb_iter = 30000 + nb_iter = 100000 rng = np.random.RandomState(0) x = rng.randn(n, 2) -- cgit v1.2.3