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

Function test_conditional_gradient

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

Source from the content-addressed store, hash-verified

9
10
11def test_conditional_gradient(nx):
12 n_bins = 100 # nb bins
13 # bin positions
14 x = np.arange(n_bins, dtype=np.float64)
15
16 # Gaussian distributions
17 a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std
18 b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10)
19
20 # loss matrix
21 M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1)))
22 M /= M.max()
23
24 def f(G):
25 return 0.5 * np.sum(G**2)
26
27 def df(G):
28 return G
29
30 def fb(G):
31 return 0.5 * nx.sum(G**2)
32
33 ab, bb, Mb = nx.from_numpy(a, b, M)
34
35 reg = 1e-1
36
37 G, log = ot.optim.cg(a, b, M, reg, f, df, verbose=True, log=True)
38 Gb, log = ot.optim.cg(ab, bb, Mb, reg, fb, df, verbose=True, log=True)
39 Gb = nx.to_numpy(Gb)
40
41 np.testing.assert_allclose(Gb, G)
42 np.testing.assert_allclose(a, Gb.sum(1))
43 np.testing.assert_allclose(b, Gb.sum(0))
44
45
46def test_conditional_gradient_itermax(nx):

Callers

nothing calls this directly

Calls 6

from_numpyMethod · 0.80
to_numpyMethod · 0.80
arangeMethod · 0.45
reshapeMethod · 0.45
maxMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected