MCPcopy Create free account
hub / github.com/PythonOT/POT / test_conditional_gradient_itermax

Function test_conditional_gradient_itermax

test/test_optim.py:46–87  ·  view source on GitHub ↗
(nx)

Source from the content-addressed store, hash-verified

44
45
46def test_conditional_gradient_itermax(nx):
47 n = 100 # nb samples
48
49 mu_s = np.array([0, 0])
50 cov_s = np.array([[1, 0], [0, 1]])
51
52 mu_t = np.array([4, 4])
53 cov_t = np.array([[1, -0.8], [-0.8, 1]])
54
55 xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s)
56 xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t)
57
58 a, b = np.ones((n,)) / n, np.ones((n,)) / n
59
60 # loss matrix
61 M = ot.dist(xs, xt)
62 M /= M.max()
63
64 def f(G):
65 return 0.5 * np.sum(G**2)
66
67 def df(G):
68 return G
69
70 def fb(G):
71 return 0.5 * nx.sum(G**2)
72
73 ab, bb, Mb = nx.from_numpy(a, b, M)
74
75 reg = 1e-1
76
77 G, log = ot.optim.cg(
78 a, b, M, reg, f, df, numItermaxEmd=10000, verbose=True, log=True
79 )
80 Gb, log = ot.optim.cg(
81 ab, bb, Mb, reg, fb, df, numItermaxEmd=10000, verbose=True, log=True
82 )
83 Gb = nx.to_numpy(Gb)
84
85 np.testing.assert_allclose(Gb, G)
86 np.testing.assert_allclose(a, Gb.sum(1))
87 np.testing.assert_allclose(b, Gb.sum(0))
88
89
90def test_generalized_conditional_gradient(nx):

Callers

nothing calls this directly

Calls 5

from_numpyMethod · 0.80
to_numpyMethod · 0.80
onesMethod · 0.45
maxMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected