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

Function test_generalized_conditional_gradient

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

Source from the content-addressed store, hash-verified

88
89
90def test_generalized_conditional_gradient(nx):
91 n_bins = 100 # nb bins
92 # bin positions
93 x = np.arange(n_bins, dtype=np.float64)
94
95 # Gaussian distributions
96 a = ot.datasets.make_1D_gauss(n_bins, m=20, s=5) # m= mean, s= std
97 b = ot.datasets.make_1D_gauss(n_bins, m=60, s=10)
98
99 # loss matrix
100 M = ot.dist(x.reshape((n_bins, 1)), x.reshape((n_bins, 1)))
101 M /= M.max()
102
103 def f(G):
104 return 0.5 * np.sum(G**2)
105
106 def df(G):
107 return G
108
109 def fb(G):
110 return 0.5 * nx.sum(G**2)
111
112 reg1 = 1e-3
113 reg2 = 1e-1
114
115 ab, bb, Mb = nx.from_numpy(a, b, M)
116
117 G, log = ot.optim.gcg(a, b, M, reg1, reg2, f, df, verbose=True, log=True)
118 Gb, log = ot.optim.gcg(ab, bb, Mb, reg1, reg2, fb, df, verbose=True, log=True)
119 Gb = nx.to_numpy(Gb)
120
121 np.testing.assert_allclose(Gb, G, atol=1e-12)
122 np.testing.assert_allclose(a, Gb.sum(1), atol=1e-05)
123 np.testing.assert_allclose(b, Gb.sum(0), atol=1e-05)
124
125
126def test_solve_1d_linesearch_quad_funct():

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