| 221 | |
| 222 | |
| 223 | def test_partial_gromov_wasserstein(): |
| 224 | rng = np.random.RandomState(42) |
| 225 | n_samples = 20 # nb samples |
| 226 | n_noise = 10 # nb of samples (noise) |
| 227 | |
| 228 | p = ot.unif(n_samples + n_noise) |
| 229 | q = ot.unif(n_samples + n_noise) |
| 230 | |
| 231 | mu_s = np.array([0, 0]) |
| 232 | cov_s = np.array([[1, 0], [0, 1]]) |
| 233 | |
| 234 | mu_t = np.array([0, 0, 0]) |
| 235 | cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) |
| 236 | |
| 237 | xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=rng) |
| 238 | xs = np.concatenate((xs, ((rng.rand(n_noise, 2) + 1) * 4)), axis=0) |
| 239 | P = sp.linalg.sqrtm(cov_t) |
| 240 | xt = rng.randn(n_samples, 3).dot(P) + mu_t |
| 241 | xt = np.concatenate((xt, ((rng.rand(n_noise, 3) + 1) * 10)), axis=0) |
| 242 | xt2 = xs[::-1].copy() |
| 243 | |
| 244 | C1 = ot.dist(xs, xs) |
| 245 | C2 = ot.dist(xt, xt) |
| 246 | C3 = ot.dist(xt2, xt2) |
| 247 | |
| 248 | m = 2 / 3 |
| 249 | res0, log0 = ot.partial.partial_gromov_wasserstein( |
| 250 | C1, C3, p, q, m=m, log=True, verbose=True |
| 251 | ) |
| 252 | np.testing.assert_allclose(res0, 0, atol=1e-1, rtol=1e-1) |
| 253 | |
| 254 | C1 = sp.spatial.distance.cdist(xs, xs) |
| 255 | C2 = sp.spatial.distance.cdist(xt, xt) |
| 256 | |
| 257 | m = 1 |
| 258 | res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True) |
| 259 | G = ot.gromov.gromov_wasserstein(C1, C2, p, q, "square_loss") |
| 260 | np.testing.assert_allclose(G, res0, atol=1e-04) |
| 261 | |
| 262 | res, log = ot.partial.entropic_partial_gromov_wasserstein( |
| 263 | C1, C2, p, q, 10, m=m, log=True |
| 264 | ) |
| 265 | G = ot.gromov.entropic_gromov_wasserstein(C1, C2, p, q, "square_loss", epsilon=10) |
| 266 | np.testing.assert_allclose(G, res, atol=1e-02) |
| 267 | |
| 268 | w0, log0 = ot.partial.partial_gromov_wasserstein2(C1, C2, p, q, m=m, log=True) |
| 269 | w0_val = ot.partial.partial_gromov_wasserstein2(C1, C2, p, q, m=m, log=False) |
| 270 | G = log0["T"] |
| 271 | np.testing.assert_allclose(w0, w0_val, atol=1e-1, rtol=1e-1) |
| 272 | |
| 273 | m = 2 / 3 |
| 274 | res0, log0 = ot.partial.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True) |
| 275 | res, log = ot.partial.entropic_partial_gromov_wasserstein( |
| 276 | C1, C2, p, q, 100, m=m, log=True |
| 277 | ) |
| 278 | |
| 279 | # check constraints |
| 280 | np.testing.assert_equal( |