summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKilian Fatras <kilianfatras@dhcp-206-12-53-99.eduroam.wireless.ubc.ca>2018-06-15 18:53:54 -0700
committerKilian Fatras <kilianfatras@dhcp-206-12-53-99.eduroam.wireless.ubc.ca>2018-06-15 18:53:54 -0700
commitc8eda449b2c6b39e9d57d1b5b2c39e43f2925892 (patch)
tree19e241788b920c8c5c181ca6562553a2e3bd7468 /test
parent90efa5a8b189214d1aeb81920b2bb04ce0c261ca (diff)
add problems solved in doc
Diffstat (limited to 'test')
-rw-r--r--test/test_stochastic.py115
1 files changed, 115 insertions, 0 deletions
diff --git a/test/test_stochastic.py b/test/test_stochastic.py
new file mode 100644
index 0000000..829f781
--- /dev/null
+++ b/test/test_stochastic.py
@@ -0,0 +1,115 @@
+"""
+==========================
+Stochastic test
+==========================
+
+This example is designed to test the stochatic optimization algorithms module
+for descrete and semicontinous measures from the POT library.
+
+"""
+
+# Author: Kilian Fatras <kilian.fatras@gmail.com>
+#
+# License: MIT License
+
+import numpy as np
+import ot
+
+#############################################################################
+#
+# TEST SAG algorithm
+# ---------------------------------------------
+# 2 identical discrete measures u defined on the same space with a
+# regularization term, a learning rate and a number of iteration
+
+
+def test_stochastic_sag():
+ # test sag
+ n = 15
+ reg = 1
+ numItermax = 300000
+ rng = np.random.RandomState(0)
+
+ x = rng.randn(n, 2)
+ u = ot.utils.unif(n)
+
+ M = ot.dist(x, x)
+
+ G = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "sag",
+ numItermax=numItermax)
+
+ # check constratints
+ np.testing.assert_allclose(
+ u, G.sum(1), atol=1e-04) # cf convergence sag
+ np.testing.assert_allclose(
+ u, G.sum(0), atol=1e-04) # cf convergence sag
+
+
+#############################################################################
+#
+# TEST ASGD algorithm
+# ---------------------------------------------
+# 2 identical discrete measures u defined on the same space with a
+# regularization term, a learning rate and a number of iteration
+
+def test_stochastic_asgd():
+ # test asgd
+ n = 15
+ reg = 1
+ numItermax = 300000
+ lr = 1
+ rng = np.random.RandomState(0)
+
+ x = rng.randn(n, 2)
+ u = ot.utils.unif(n)
+
+ M = ot.dist(x, x)
+
+ G = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "asgd",
+ numItermax=numItermax,
+ lr=lr)
+
+ # check constratints
+ np.testing.assert_allclose(
+ u, G.sum(1), atol=1e-03) # cf convergence asgd
+ np.testing.assert_allclose(
+ u, G.sum(0), atol=1e-03) # cf convergence asgd
+
+
+#############################################################################
+#
+# TEST Convergence SAG and ASGD toward Sinkhorn's solution
+# --------------------------------------------------------
+# 2 identical discrete measures u defined on the same space with a
+# regularization term, a learning rate and a number of iteration
+
+
+def test_sag_asgd_sinkhorn():
+ # test all algorithms
+ n = 15
+ reg = 1
+ nb_iter = 300000
+ lr = 1
+ rng = np.random.RandomState(0)
+
+ x = rng.randn(n, 2)
+ u = ot.utils.unif(n)
+ zero = np.zeros(n)
+ M = ot.dist(x, x)
+
+ G_asgd = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "asgd",
+ numItermax=nb_iter,
+ lr=1)
+ G_sag = ot.stochastic.transportation_matrix_entropic(u, u, M, reg, "sag",
+ numItermax=nb_iter)
+ G_sinkhorn = ot.sinkhorn(u, u, M, reg)
+
+ # check constratints
+ np.testing.assert_allclose(
+ zero, (G_sag - G_sinkhorn).sum(1), atol=1e-03) # cf convergence sag
+ np.testing.assert_allclose(
+ zero, (G_sag - G_sinkhorn).sum(0), atol=1e-03) # cf convergence sag
+ np.testing.assert_allclose(
+ zero, (G_asgd - G_sinkhorn).sum(1), atol=1e-03) # cf convergence asgd
+ np.testing.assert_allclose(
+ zero, (G_asgd - G_sinkhorn).sum(0), atol=1e-03) # cf convergence asgd