| 1 |
"""Tests for module Unbalanced Co-Optimal Transport""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import itertools |
| 8 |
import numpy as np |
| 9 |
import ot |
| 10 |
import pytest |
| 11 |
from ot.gromov._unbalanced import ( |
| 12 |
unbalanced_co_optimal_transport, |
| 13 |
unbalanced_co_optimal_transport2, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 18 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 19 |
@pytest.mark.parametrize( |
| 20 |
"unbalanced_solver, divergence", itertools.product(["mm", "lbfgsb"], ["kl", "l2"]) |
| 21 |
) |
| 22 |
def test_sanity(nx, unbalanced_solver, divergence): |
| 23 |
n_samples = 5 |
| 24 |
|
| 25 |
mu_s = np.array([0, 0]) |
| 26 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 27 |
|
| 28 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 29 |
xt = xs[::-1].copy() |
| 30 |
|
| 31 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 32 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 33 |
|
| 34 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 35 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 36 |
|
| 37 |
reg_m = (10, 5) |
| 38 |
eps = 0 |
| 39 |
max_iter_ot = 200 |
| 40 |
max_iter = 200 |
| 41 |
tol = 1e-7 |
| 42 |
tol_ot = 1e-7 |
| 43 |
|
| 44 |
|
| 45 |
anti_id_sample = np.flipud(np.eye(n_samples, n_samples)) / n_samples |
| 46 |
id_feature = np.eye(2, 2) / 2 |
| 47 |
|
| 48 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 49 |
X=xs, |
| 50 |
Y=xt, |
| 51 |
wx_samp=px_s, |
| 52 |
wx_feat=px_f, |
| 53 |
wy_samp=py_s, |
| 54 |
wy_feat=py_f, |
| 55 |
reg_marginals=reg_m, |
| 56 |
epsilon=eps, |
| 57 |
divergence=divergence, |
| 58 |
unbalanced_solver=unbalanced_solver, |
| 59 |
alpha=0, |
| 60 |
M_samp=None, |
| 61 |
M_feat=None, |
| 62 |
init_pi=None, |
| 63 |
init_duals=None, |
| 64 |
max_iter=max_iter, |
| 65 |
tol=tol, |
| 66 |
max_iter_ot=max_iter_ot, |
| 67 |
tol_ot=tol_ot, |
| 68 |
log=False, |
| 69 |
verbose=False, |
| 70 |
) |
| 71 |
|
| 72 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 73 |
X=xs_nx, |
| 74 |
Y=xt_nx, |
| 75 |
wx_samp=px_s_nx, |
| 76 |
wx_feat=px_f_nx, |
| 77 |
wy_samp=py_s_nx, |
| 78 |
wy_feat=py_f_nx, |
| 79 |
reg_marginals=reg_m, |
| 80 |
epsilon=eps, |
| 81 |
divergence=divergence, |
| 82 |
unbalanced_solver=unbalanced_solver, |
| 83 |
alpha=0, |
| 84 |
M_samp=None, |
| 85 |
M_feat=None, |
| 86 |
init_pi=None, |
| 87 |
init_duals=None, |
| 88 |
max_iter=max_iter, |
| 89 |
tol=tol, |
| 90 |
max_iter_ot=max_iter_ot, |
| 91 |
tol_ot=tol_ot, |
| 92 |
log=False, |
| 93 |
verbose=False, |
| 94 |
) |
| 95 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 96 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 97 |
|
| 98 |
np.testing.assert_allclose(pi_sample, anti_id_sample, atol=1e-05) |
| 99 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 100 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 101 |
np.testing.assert_allclose(pi_feature, id_feature, atol=1e-05) |
| 102 |
|
| 103 |
|
| 104 |
ucoot = unbalanced_co_optimal_transport2( |
| 105 |
X=xs, |
| 106 |
Y=xt, |
| 107 |
wx_samp=px_s, |
| 108 |
wx_feat=px_f, |
| 109 |
wy_samp=py_s, |
| 110 |
wy_feat=py_f, |
| 111 |
reg_marginals=reg_m, |
| 112 |
epsilon=eps, |
| 113 |
divergence=divergence, |
| 114 |
unbalanced_solver=unbalanced_solver, |
| 115 |
alpha=0, |
| 116 |
M_samp=None, |
| 117 |
M_feat=None, |
| 118 |
init_pi=None, |
| 119 |
init_duals=None, |
| 120 |
max_iter=max_iter, |
| 121 |
tol=tol, |
| 122 |
max_iter_ot=max_iter_ot, |
| 123 |
tol_ot=tol_ot, |
| 124 |
log=False, |
| 125 |
verbose=False, |
| 126 |
) |
| 127 |
|
| 128 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 129 |
X=xs_nx, |
| 130 |
Y=xt_nx, |
| 131 |
wx_samp=px_s_nx, |
| 132 |
wx_feat=px_f_nx, |
| 133 |
wy_samp=py_s_nx, |
| 134 |
wy_feat=py_f_nx, |
| 135 |
reg_marginals=reg_m, |
| 136 |
epsilon=eps, |
| 137 |
divergence=divergence, |
| 138 |
unbalanced_solver=unbalanced_solver, |
| 139 |
alpha=0, |
| 140 |
M_samp=None, |
| 141 |
M_feat=None, |
| 142 |
init_pi=None, |
| 143 |
init_duals=None, |
| 144 |
max_iter=max_iter, |
| 145 |
tol=tol, |
| 146 |
max_iter_ot=max_iter_ot, |
| 147 |
tol_ot=tol_ot, |
| 148 |
log=False, |
| 149 |
verbose=False, |
| 150 |
) |
| 151 |
|
| 152 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 153 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 154 |
np.testing.assert_allclose(ucoot, 0, atol=1e-06) |
| 155 |
|
| 156 |
|
| 157 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 158 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 159 |
@pytest.mark.parametrize( |
| 160 |
"unbalanced_solver, divergence, eps", |
| 161 |
itertools.product( |
| 162 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1] |
| 163 |
), |
| 164 |
) |
| 165 |
def test_init_plans(nx, unbalanced_solver, divergence, eps): |
| 166 |
n_samples = 5 |
| 167 |
|
| 168 |
mu_s = np.array([0, 0]) |
| 169 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 170 |
|
| 171 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 172 |
xt = xs[::-1].copy() |
| 173 |
|
| 174 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 175 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 176 |
G0_samp = px_s[:, None] * py_s[None, :] |
| 177 |
G0_feat = px_f[:, None] * py_f[None, :] |
| 178 |
|
| 179 |
xs_nx, xt_nx, G0_samp_nx, G0_feat_nx = nx.from_numpy(xs, xt, G0_samp, G0_feat) |
| 180 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 181 |
|
| 182 |
reg_m = (1, 5) |
| 183 |
alpha = (0.1, 0.2) |
| 184 |
max_iter_ot = 5 |
| 185 |
max_iter = 5 |
| 186 |
tol = 1e-7 |
| 187 |
tol_ot = 1e-7 |
| 188 |
|
| 189 |
|
| 190 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 191 |
X=xs, |
| 192 |
Y=xt, |
| 193 |
wx_samp=px_s, |
| 194 |
wx_feat=px_f, |
| 195 |
wy_samp=py_s, |
| 196 |
wy_feat=py_f, |
| 197 |
reg_marginals=reg_m, |
| 198 |
epsilon=eps, |
| 199 |
divergence=divergence, |
| 200 |
unbalanced_solver=unbalanced_solver, |
| 201 |
alpha=alpha, |
| 202 |
M_samp=None, |
| 203 |
M_feat=None, |
| 204 |
init_pi=None, |
| 205 |
init_duals=None, |
| 206 |
max_iter=max_iter, |
| 207 |
tol=tol, |
| 208 |
max_iter_ot=max_iter_ot, |
| 209 |
tol_ot=tol_ot, |
| 210 |
log=False, |
| 211 |
verbose=False, |
| 212 |
) |
| 213 |
|
| 214 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 215 |
X=xs_nx, |
| 216 |
Y=xt_nx, |
| 217 |
wx_samp=px_s_nx, |
| 218 |
wx_feat=px_f_nx, |
| 219 |
wy_samp=py_s_nx, |
| 220 |
wy_feat=py_f_nx, |
| 221 |
reg_marginals=reg_m, |
| 222 |
epsilon=eps, |
| 223 |
divergence=divergence, |
| 224 |
unbalanced_solver=unbalanced_solver, |
| 225 |
alpha=alpha, |
| 226 |
M_samp=None, |
| 227 |
M_feat=None, |
| 228 |
init_pi=(G0_samp_nx, G0_feat_nx), |
| 229 |
init_duals=None, |
| 230 |
max_iter=max_iter, |
| 231 |
tol=tol, |
| 232 |
max_iter_ot=max_iter_ot, |
| 233 |
tol_ot=tol_ot, |
| 234 |
log=False, |
| 235 |
verbose=False, |
| 236 |
) |
| 237 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 238 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 239 |
|
| 240 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-03) |
| 241 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-03) |
| 242 |
|
| 243 |
|
| 244 |
ucoot = unbalanced_co_optimal_transport2( |
| 245 |
X=xs, |
| 246 |
Y=xt, |
| 247 |
wx_samp=px_s, |
| 248 |
wx_feat=px_f, |
| 249 |
wy_samp=py_s, |
| 250 |
wy_feat=py_f, |
| 251 |
reg_marginals=reg_m, |
| 252 |
epsilon=eps, |
| 253 |
divergence=divergence, |
| 254 |
unbalanced_solver=unbalanced_solver, |
| 255 |
alpha=alpha, |
| 256 |
M_samp=None, |
| 257 |
M_feat=None, |
| 258 |
init_pi=None, |
| 259 |
init_duals=None, |
| 260 |
max_iter=max_iter, |
| 261 |
tol=tol, |
| 262 |
max_iter_ot=max_iter_ot, |
| 263 |
tol_ot=tol_ot, |
| 264 |
log=False, |
| 265 |
verbose=False, |
| 266 |
) |
| 267 |
|
| 268 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 269 |
X=xs_nx, |
| 270 |
Y=xt_nx, |
| 271 |
wx_samp=px_s_nx, |
| 272 |
wx_feat=px_f_nx, |
| 273 |
wy_samp=py_s_nx, |
| 274 |
wy_feat=py_f_nx, |
| 275 |
reg_marginals=reg_m, |
| 276 |
epsilon=eps, |
| 277 |
divergence=divergence, |
| 278 |
unbalanced_solver=unbalanced_solver, |
| 279 |
alpha=alpha, |
| 280 |
M_samp=None, |
| 281 |
M_feat=None, |
| 282 |
init_pi=(G0_samp_nx, G0_feat_nx), |
| 283 |
init_duals=None, |
| 284 |
max_iter=max_iter, |
| 285 |
tol=tol, |
| 286 |
max_iter_ot=max_iter_ot, |
| 287 |
tol_ot=tol_ot, |
| 288 |
log=False, |
| 289 |
verbose=False, |
| 290 |
) |
| 291 |
|
| 292 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 293 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 294 |
|
| 295 |
|
| 296 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 297 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 298 |
@pytest.mark.parametrize( |
| 299 |
"unbalanced_solver, divergence, eps", |
| 300 |
itertools.product( |
| 301 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1] |
| 302 |
), |
| 303 |
) |
| 304 |
def test_init_duals(nx, unbalanced_solver, divergence, eps): |
| 305 |
n_samples = 5 |
| 306 |
|
| 307 |
mu_s = np.array([0, 0]) |
| 308 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 309 |
|
| 310 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 311 |
xt = xs[::-1].copy() |
| 312 |
|
| 313 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 314 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 315 |
|
| 316 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 317 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 318 |
|
| 319 |
init_duals_samp = nx.from_numpy(np.zeros(n_samples), np.zeros(n_samples)) |
| 320 |
init_duals_feat = nx.from_numpy(np.zeros(2), np.zeros(2)) |
| 321 |
init_duals = (init_duals_samp, init_duals_feat) |
| 322 |
|
| 323 |
reg_m = (10, 5) |
| 324 |
alpha = (0.1, 0.2) |
| 325 |
max_iter_ot = 5 |
| 326 |
max_iter = 5 |
| 327 |
tol = 1e-7 |
| 328 |
tol_ot = 1e-7 |
| 329 |
|
| 330 |
|
| 331 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 332 |
X=xs, |
| 333 |
Y=xt, |
| 334 |
wx_samp=px_s, |
| 335 |
wx_feat=px_f, |
| 336 |
wy_samp=py_s, |
| 337 |
wy_feat=py_f, |
| 338 |
reg_marginals=reg_m, |
| 339 |
epsilon=eps, |
| 340 |
divergence=divergence, |
| 341 |
unbalanced_solver=unbalanced_solver, |
| 342 |
alpha=alpha, |
| 343 |
M_samp=None, |
| 344 |
M_feat=None, |
| 345 |
init_pi=None, |
| 346 |
init_duals=None, |
| 347 |
max_iter=max_iter, |
| 348 |
tol=tol, |
| 349 |
max_iter_ot=max_iter_ot, |
| 350 |
tol_ot=tol_ot, |
| 351 |
log=False, |
| 352 |
verbose=False, |
| 353 |
) |
| 354 |
|
| 355 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 356 |
X=xs_nx, |
| 357 |
Y=xt_nx, |
| 358 |
wx_samp=px_s_nx, |
| 359 |
wx_feat=px_f_nx, |
| 360 |
wy_samp=py_s_nx, |
| 361 |
wy_feat=py_f_nx, |
| 362 |
reg_marginals=reg_m, |
| 363 |
epsilon=eps, |
| 364 |
divergence=divergence, |
| 365 |
unbalanced_solver=unbalanced_solver, |
| 366 |
alpha=alpha, |
| 367 |
M_samp=None, |
| 368 |
M_feat=None, |
| 369 |
init_pi=None, |
| 370 |
init_duals=init_duals, |
| 371 |
max_iter=max_iter, |
| 372 |
tol=tol, |
| 373 |
max_iter_ot=max_iter_ot, |
| 374 |
tol_ot=tol_ot, |
| 375 |
log=False, |
| 376 |
verbose=False, |
| 377 |
) |
| 378 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 379 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 380 |
|
| 381 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-03) |
| 382 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-03) |
| 383 |
|
| 384 |
|
| 385 |
ucoot = unbalanced_co_optimal_transport2( |
| 386 |
X=xs, |
| 387 |
Y=xt, |
| 388 |
wx_samp=px_s, |
| 389 |
wx_feat=px_f, |
| 390 |
wy_samp=py_s, |
| 391 |
wy_feat=py_f, |
| 392 |
reg_marginals=reg_m, |
| 393 |
epsilon=eps, |
| 394 |
divergence=divergence, |
| 395 |
unbalanced_solver=unbalanced_solver, |
| 396 |
alpha=alpha, |
| 397 |
M_samp=None, |
| 398 |
M_feat=None, |
| 399 |
init_pi=None, |
| 400 |
init_duals=None, |
| 401 |
max_iter=max_iter, |
| 402 |
tol=tol, |
| 403 |
max_iter_ot=max_iter_ot, |
| 404 |
tol_ot=tol_ot, |
| 405 |
log=False, |
| 406 |
verbose=False, |
| 407 |
) |
| 408 |
|
| 409 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 410 |
X=xs_nx, |
| 411 |
Y=xt_nx, |
| 412 |
wx_samp=px_s_nx, |
| 413 |
wx_feat=px_f_nx, |
| 414 |
wy_samp=py_s_nx, |
| 415 |
wy_feat=py_f_nx, |
| 416 |
reg_marginals=reg_m, |
| 417 |
epsilon=eps, |
| 418 |
divergence=divergence, |
| 419 |
unbalanced_solver=unbalanced_solver, |
| 420 |
alpha=alpha, |
| 421 |
M_samp=None, |
| 422 |
M_feat=None, |
| 423 |
init_pi=None, |
| 424 |
init_duals=init_duals, |
| 425 |
max_iter=max_iter, |
| 426 |
tol=tol, |
| 427 |
max_iter_ot=max_iter_ot, |
| 428 |
tol_ot=tol_ot, |
| 429 |
log=False, |
| 430 |
verbose=False, |
| 431 |
) |
| 432 |
|
| 433 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 434 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 435 |
|
| 436 |
|
| 437 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 438 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 439 |
@pytest.mark.parametrize( |
| 440 |
"unbalanced_solver, divergence, eps", |
| 441 |
itertools.product( |
| 442 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2] |
| 443 |
), |
| 444 |
) |
| 445 |
def test_linear_part(nx, unbalanced_solver, divergence, eps): |
| 446 |
n_samples = 5 |
| 447 |
|
| 448 |
mu_s = np.array([0, 0]) |
| 449 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 450 |
|
| 451 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 452 |
xt = xs[::-1].copy() |
| 453 |
|
| 454 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 455 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 456 |
|
| 457 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 458 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 459 |
|
| 460 |
|
| 461 |
M_samp = np.ones((n_samples, n_samples)) |
| 462 |
np.fill_diagonal(np.fliplr(M_samp), 0) |
| 463 |
M_feat = np.ones((2, 2)) |
| 464 |
np.fill_diagonal(M_feat, 0) |
| 465 |
M_samp_nx, M_feat_nx = nx.from_numpy(M_samp, M_feat) |
| 466 |
|
| 467 |
reg_m = (10, 5) |
| 468 |
alpha = (0.1, 0.2) |
| 469 |
max_iter_ot = 5 |
| 470 |
max_iter = 5 |
| 471 |
tol = 1e-7 |
| 472 |
tol_ot = 1e-7 |
| 473 |
|
| 474 |
|
| 475 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 476 |
X=xs, |
| 477 |
Y=xt, |
| 478 |
wx_samp=px_s, |
| 479 |
wx_feat=px_f, |
| 480 |
wy_samp=py_s, |
| 481 |
wy_feat=py_f, |
| 482 |
reg_marginals=reg_m, |
| 483 |
epsilon=eps, |
| 484 |
divergence=divergence, |
| 485 |
unbalanced_solver=unbalanced_solver, |
| 486 |
alpha=alpha, |
| 487 |
M_samp=M_samp, |
| 488 |
M_feat=M_feat, |
| 489 |
init_pi=None, |
| 490 |
init_duals=None, |
| 491 |
max_iter=max_iter, |
| 492 |
tol=tol, |
| 493 |
max_iter_ot=max_iter_ot, |
| 494 |
tol_ot=tol_ot, |
| 495 |
log=False, |
| 496 |
verbose=False, |
| 497 |
) |
| 498 |
|
| 499 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 500 |
X=xs_nx, |
| 501 |
Y=xt_nx, |
| 502 |
wx_samp=px_s_nx, |
| 503 |
wx_feat=px_f_nx, |
| 504 |
wy_samp=py_s_nx, |
| 505 |
wy_feat=py_f_nx, |
| 506 |
reg_marginals=reg_m, |
| 507 |
epsilon=eps, |
| 508 |
divergence=divergence, |
| 509 |
unbalanced_solver=unbalanced_solver, |
| 510 |
alpha=alpha, |
| 511 |
M_samp=M_samp_nx, |
| 512 |
M_feat=M_feat_nx, |
| 513 |
init_pi=None, |
| 514 |
init_duals=None, |
| 515 |
max_iter=max_iter, |
| 516 |
tol=tol, |
| 517 |
max_iter_ot=max_iter_ot, |
| 518 |
tol_ot=tol_ot, |
| 519 |
log=False, |
| 520 |
verbose=False, |
| 521 |
) |
| 522 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 523 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 524 |
|
| 525 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 526 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 527 |
|
| 528 |
|
| 529 |
ucoot = unbalanced_co_optimal_transport2( |
| 530 |
X=xs, |
| 531 |
Y=xt, |
| 532 |
wx_samp=px_s, |
| 533 |
wx_feat=px_f, |
| 534 |
wy_samp=py_s, |
| 535 |
wy_feat=py_f, |
| 536 |
reg_marginals=reg_m, |
| 537 |
epsilon=eps, |
| 538 |
divergence=divergence, |
| 539 |
unbalanced_solver=unbalanced_solver, |
| 540 |
alpha=alpha, |
| 541 |
M_samp=M_samp, |
| 542 |
M_feat=M_feat, |
| 543 |
init_pi=None, |
| 544 |
init_duals=None, |
| 545 |
max_iter=max_iter, |
| 546 |
tol=tol, |
| 547 |
max_iter_ot=max_iter_ot, |
| 548 |
tol_ot=tol_ot, |
| 549 |
log=False, |
| 550 |
verbose=False, |
| 551 |
) |
| 552 |
|
| 553 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 554 |
X=xs_nx, |
| 555 |
Y=xt_nx, |
| 556 |
wx_samp=px_s_nx, |
| 557 |
wx_feat=px_f_nx, |
| 558 |
wy_samp=py_s_nx, |
| 559 |
wy_feat=py_f_nx, |
| 560 |
reg_marginals=reg_m, |
| 561 |
epsilon=eps, |
| 562 |
divergence=divergence, |
| 563 |
unbalanced_solver=unbalanced_solver, |
| 564 |
alpha=alpha, |
| 565 |
M_samp=M_samp_nx, |
| 566 |
M_feat=M_feat_nx, |
| 567 |
init_pi=None, |
| 568 |
init_duals=None, |
| 569 |
max_iter=max_iter, |
| 570 |
tol=tol, |
| 571 |
max_iter_ot=max_iter_ot, |
| 572 |
tol_ot=tol_ot, |
| 573 |
log=False, |
| 574 |
verbose=False, |
| 575 |
) |
| 576 |
|
| 577 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 578 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 579 |
|
| 580 |
|
| 581 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 582 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 583 |
@pytest.mark.parametrize( |
| 584 |
"unbalanced_solver, divergence, eps", |
| 585 |
itertools.product( |
| 586 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1] |
| 587 |
), |
| 588 |
) |
| 589 |
def test_reg_marginals(nx, unbalanced_solver, divergence, eps): |
| 590 |
n_samples = 5 |
| 591 |
|
| 592 |
mu_s = np.array([0, 0]) |
| 593 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 594 |
|
| 595 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 596 |
xt = xs[::-1].copy() |
| 597 |
|
| 598 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 599 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 600 |
|
| 601 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 602 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 603 |
|
| 604 |
alpha = (0.1, 0.2) |
| 605 |
max_iter_ot = 5 |
| 606 |
max_iter = 5 |
| 607 |
tol = 1e-7 |
| 608 |
tol_ot = 1e-7 |
| 609 |
|
| 610 |
reg_m = 100 |
| 611 |
full_list_reg_m = [reg_m, reg_m] |
| 612 |
full_tuple_reg_m = (reg_m, reg_m) |
| 613 |
tuple_reg_m, list_reg_m = (reg_m), [reg_m] |
| 614 |
|
| 615 |
list_options = [full_tuple_reg_m, tuple_reg_m, full_list_reg_m, list_reg_m] |
| 616 |
|
| 617 |
|
| 618 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 619 |
X=xs, |
| 620 |
Y=xt, |
| 621 |
wx_samp=px_s, |
| 622 |
wx_feat=px_f, |
| 623 |
wy_samp=py_s, |
| 624 |
wy_feat=py_f, |
| 625 |
reg_marginals=reg_m, |
| 626 |
epsilon=eps, |
| 627 |
divergence=divergence, |
| 628 |
unbalanced_solver=unbalanced_solver, |
| 629 |
alpha=alpha, |
| 630 |
M_samp=None, |
| 631 |
M_feat=None, |
| 632 |
init_pi=None, |
| 633 |
init_duals=None, |
| 634 |
max_iter=max_iter, |
| 635 |
tol=tol, |
| 636 |
max_iter_ot=max_iter_ot, |
| 637 |
tol_ot=tol_ot, |
| 638 |
log=False, |
| 639 |
verbose=False, |
| 640 |
) |
| 641 |
|
| 642 |
|
| 643 |
ucoot = unbalanced_co_optimal_transport2( |
| 644 |
X=xs, |
| 645 |
Y=xt, |
| 646 |
wx_samp=px_s, |
| 647 |
wx_feat=px_f, |
| 648 |
wy_samp=py_s, |
| 649 |
wy_feat=py_f, |
| 650 |
reg_marginals=reg_m, |
| 651 |
epsilon=eps, |
| 652 |
divergence=divergence, |
| 653 |
unbalanced_solver=unbalanced_solver, |
| 654 |
alpha=alpha, |
| 655 |
M_samp=None, |
| 656 |
M_feat=None, |
| 657 |
init_pi=None, |
| 658 |
init_duals=None, |
| 659 |
max_iter=max_iter, |
| 660 |
tol=tol, |
| 661 |
max_iter_ot=max_iter_ot, |
| 662 |
tol_ot=tol_ot, |
| 663 |
log=False, |
| 664 |
verbose=False, |
| 665 |
) |
| 666 |
|
| 667 |
for opt in list_options: |
| 668 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 669 |
X=xs_nx, |
| 670 |
Y=xt_nx, |
| 671 |
wx_samp=px_s_nx, |
| 672 |
wx_feat=px_f_nx, |
| 673 |
wy_samp=py_s_nx, |
| 674 |
wy_feat=py_f_nx, |
| 675 |
reg_marginals=opt, |
| 676 |
epsilon=eps, |
| 677 |
divergence=divergence, |
| 678 |
unbalanced_solver=unbalanced_solver, |
| 679 |
alpha=alpha, |
| 680 |
M_samp=None, |
| 681 |
M_feat=None, |
| 682 |
init_pi=None, |
| 683 |
init_duals=None, |
| 684 |
max_iter=max_iter, |
| 685 |
tol=tol, |
| 686 |
max_iter_ot=max_iter_ot, |
| 687 |
tol_ot=tol_ot, |
| 688 |
log=False, |
| 689 |
verbose=False, |
| 690 |
) |
| 691 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 692 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 693 |
|
| 694 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 695 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 696 |
|
| 697 |
|
| 698 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 699 |
X=xs_nx, |
| 700 |
Y=xt_nx, |
| 701 |
wx_samp=px_s_nx, |
| 702 |
wx_feat=px_f_nx, |
| 703 |
wy_samp=py_s_nx, |
| 704 |
wy_feat=py_f_nx, |
| 705 |
reg_marginals=opt, |
| 706 |
epsilon=eps, |
| 707 |
divergence=divergence, |
| 708 |
unbalanced_solver=unbalanced_solver, |
| 709 |
alpha=alpha, |
| 710 |
M_samp=None, |
| 711 |
M_feat=None, |
| 712 |
init_pi=None, |
| 713 |
init_duals=None, |
| 714 |
max_iter=max_iter, |
| 715 |
tol=tol, |
| 716 |
max_iter_ot=max_iter_ot, |
| 717 |
tol_ot=tol_ot, |
| 718 |
method_sinkhorn="sinkhorn", |
| 719 |
log=False, |
| 720 |
verbose=False, |
| 721 |
) |
| 722 |
|
| 723 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 724 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 725 |
|
| 726 |
|
| 727 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 728 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 729 |
@pytest.mark.parametrize( |
| 730 |
"unbalanced_solver, divergence, alpha", |
| 731 |
itertools.product( |
| 732 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1] |
| 733 |
), |
| 734 |
) |
| 735 |
def test_eps(nx, unbalanced_solver, divergence, alpha): |
| 736 |
n_samples = 5 |
| 737 |
|
| 738 |
mu_s = np.array([0, 0]) |
| 739 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 740 |
|
| 741 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 742 |
xt = xs[::-1].copy() |
| 743 |
|
| 744 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 745 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 746 |
|
| 747 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 748 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 749 |
|
| 750 |
reg_m = (10, 5) |
| 751 |
alpha = (0.1, 0.2) |
| 752 |
max_iter_ot = 5 |
| 753 |
max_iter = 5 |
| 754 |
tol = 1e-7 |
| 755 |
tol_ot = 1e-7 |
| 756 |
|
| 757 |
eps = 1 |
| 758 |
full_list_eps = [eps, eps] |
| 759 |
full_tuple_eps = (eps, eps) |
| 760 |
tuple_eps, list_eps = (eps), [eps] |
| 761 |
|
| 762 |
list_options = [full_list_eps, full_tuple_eps, tuple_eps, list_eps] |
| 763 |
|
| 764 |
|
| 765 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 766 |
X=xs, |
| 767 |
Y=xt, |
| 768 |
wx_samp=px_s, |
| 769 |
wx_feat=px_f, |
| 770 |
wy_samp=py_s, |
| 771 |
wy_feat=py_f, |
| 772 |
reg_marginals=reg_m, |
| 773 |
epsilon=eps, |
| 774 |
divergence=divergence, |
| 775 |
unbalanced_solver=unbalanced_solver, |
| 776 |
alpha=alpha, |
| 777 |
M_samp=None, |
| 778 |
M_feat=None, |
| 779 |
init_pi=None, |
| 780 |
init_duals=None, |
| 781 |
max_iter=max_iter, |
| 782 |
tol=tol, |
| 783 |
max_iter_ot=max_iter_ot, |
| 784 |
tol_ot=tol_ot, |
| 785 |
log=False, |
| 786 |
verbose=False, |
| 787 |
) |
| 788 |
|
| 789 |
|
| 790 |
ucoot = unbalanced_co_optimal_transport2( |
| 791 |
X=xs, |
| 792 |
Y=xt, |
| 793 |
wx_samp=px_s, |
| 794 |
wx_feat=px_f, |
| 795 |
wy_samp=py_s, |
| 796 |
wy_feat=py_f, |
| 797 |
reg_marginals=reg_m, |
| 798 |
epsilon=eps, |
| 799 |
divergence=divergence, |
| 800 |
unbalanced_solver=unbalanced_solver, |
| 801 |
alpha=alpha, |
| 802 |
M_samp=None, |
| 803 |
M_feat=None, |
| 804 |
init_pi=None, |
| 805 |
init_duals=None, |
| 806 |
max_iter=max_iter, |
| 807 |
tol=tol, |
| 808 |
max_iter_ot=max_iter_ot, |
| 809 |
tol_ot=tol_ot, |
| 810 |
log=False, |
| 811 |
verbose=False, |
| 812 |
) |
| 813 |
|
| 814 |
for opt in list_options: |
| 815 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 816 |
X=xs_nx, |
| 817 |
Y=xt_nx, |
| 818 |
wx_samp=px_s_nx, |
| 819 |
wx_feat=px_f_nx, |
| 820 |
wy_samp=py_s_nx, |
| 821 |
wy_feat=py_f_nx, |
| 822 |
reg_marginals=reg_m, |
| 823 |
epsilon=opt, |
| 824 |
divergence=divergence, |
| 825 |
unbalanced_solver=unbalanced_solver, |
| 826 |
alpha=alpha, |
| 827 |
M_samp=None, |
| 828 |
M_feat=None, |
| 829 |
init_pi=None, |
| 830 |
init_duals=None, |
| 831 |
max_iter=max_iter, |
| 832 |
tol=tol, |
| 833 |
max_iter_ot=max_iter_ot, |
| 834 |
tol_ot=tol_ot, |
| 835 |
log=False, |
| 836 |
verbose=False, |
| 837 |
) |
| 838 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 839 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 840 |
|
| 841 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 842 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 843 |
|
| 844 |
|
| 845 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 846 |
X=xs_nx, |
| 847 |
Y=xt_nx, |
| 848 |
wx_samp=px_s_nx, |
| 849 |
wx_feat=px_f_nx, |
| 850 |
wy_samp=py_s_nx, |
| 851 |
wy_feat=py_f_nx, |
| 852 |
reg_marginals=reg_m, |
| 853 |
epsilon=opt, |
| 854 |
divergence=divergence, |
| 855 |
unbalanced_solver=unbalanced_solver, |
| 856 |
alpha=alpha, |
| 857 |
M_samp=None, |
| 858 |
M_feat=None, |
| 859 |
init_pi=None, |
| 860 |
init_duals=None, |
| 861 |
max_iter=max_iter, |
| 862 |
tol=tol, |
| 863 |
max_iter_ot=max_iter_ot, |
| 864 |
tol_ot=tol_ot, |
| 865 |
log=False, |
| 866 |
verbose=False, |
| 867 |
) |
| 868 |
|
| 869 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 870 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 871 |
|
| 872 |
|
| 873 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 874 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 875 |
@pytest.mark.parametrize( |
| 876 |
"unbalanced_solver, divergence, eps", |
| 877 |
itertools.product( |
| 878 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1e-2] |
| 879 |
), |
| 880 |
) |
| 881 |
def test_alpha(nx, unbalanced_solver, divergence, eps): |
| 882 |
n_samples = 5 |
| 883 |
|
| 884 |
mu_s = np.array([0, 0]) |
| 885 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 886 |
|
| 887 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 888 |
xt = xs[::-1].copy() |
| 889 |
|
| 890 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 891 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 892 |
|
| 893 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 894 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 895 |
|
| 896 |
|
| 897 |
M_samp = np.ones((n_samples, n_samples)) |
| 898 |
np.fill_diagonal(np.fliplr(M_samp), 0) |
| 899 |
M_feat = np.ones((2, 2)) |
| 900 |
np.fill_diagonal(M_feat, 0) |
| 901 |
M_samp_nx, M_feat_nx = nx.from_numpy(M_samp, M_feat) |
| 902 |
|
| 903 |
reg_m = (10, 5) |
| 904 |
max_iter_ot = 5 |
| 905 |
max_iter = 5 |
| 906 |
tol = 1e-7 |
| 907 |
tol_ot = 1e-7 |
| 908 |
|
| 909 |
alpha = 1 |
| 910 |
full_list_alpha = [alpha, alpha] |
| 911 |
full_tuple_alpha = (alpha, alpha) |
| 912 |
tuple_alpha, list_alpha = (alpha), [alpha] |
| 913 |
|
| 914 |
list_options = [full_list_alpha, full_tuple_alpha, tuple_alpha, list_alpha] |
| 915 |
|
| 916 |
|
| 917 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 918 |
X=xs, |
| 919 |
Y=xt, |
| 920 |
wx_samp=px_s, |
| 921 |
wx_feat=px_f, |
| 922 |
wy_samp=py_s, |
| 923 |
wy_feat=py_f, |
| 924 |
reg_marginals=reg_m, |
| 925 |
epsilon=eps, |
| 926 |
divergence=divergence, |
| 927 |
unbalanced_solver=unbalanced_solver, |
| 928 |
alpha=alpha, |
| 929 |
M_samp=M_samp, |
| 930 |
M_feat=M_feat, |
| 931 |
init_pi=None, |
| 932 |
init_duals=None, |
| 933 |
max_iter=max_iter, |
| 934 |
tol=tol, |
| 935 |
max_iter_ot=max_iter_ot, |
| 936 |
tol_ot=tol_ot, |
| 937 |
log=False, |
| 938 |
verbose=False, |
| 939 |
) |
| 940 |
|
| 941 |
|
| 942 |
ucoot = unbalanced_co_optimal_transport2( |
| 943 |
X=xs, |
| 944 |
Y=xt, |
| 945 |
wx_samp=px_s, |
| 946 |
wx_feat=px_f, |
| 947 |
wy_samp=py_s, |
| 948 |
wy_feat=py_f, |
| 949 |
reg_marginals=reg_m, |
| 950 |
epsilon=eps, |
| 951 |
divergence=divergence, |
| 952 |
unbalanced_solver=unbalanced_solver, |
| 953 |
alpha=alpha, |
| 954 |
M_samp=M_samp, |
| 955 |
M_feat=M_feat, |
| 956 |
init_pi=None, |
| 957 |
init_duals=None, |
| 958 |
max_iter=max_iter, |
| 959 |
tol=tol, |
| 960 |
max_iter_ot=max_iter_ot, |
| 961 |
tol_ot=tol_ot, |
| 962 |
log=False, |
| 963 |
verbose=False, |
| 964 |
) |
| 965 |
|
| 966 |
for opt in list_options: |
| 967 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 968 |
X=xs_nx, |
| 969 |
Y=xt_nx, |
| 970 |
wx_samp=px_s_nx, |
| 971 |
wx_feat=px_f_nx, |
| 972 |
wy_samp=py_s_nx, |
| 973 |
wy_feat=py_f_nx, |
| 974 |
reg_marginals=reg_m, |
| 975 |
epsilon=eps, |
| 976 |
divergence=divergence, |
| 977 |
unbalanced_solver=unbalanced_solver, |
| 978 |
alpha=opt, |
| 979 |
M_samp=M_samp_nx, |
| 980 |
M_feat=M_feat_nx, |
| 981 |
init_pi=None, |
| 982 |
init_duals=None, |
| 983 |
max_iter=max_iter, |
| 984 |
tol=tol, |
| 985 |
max_iter_ot=max_iter_ot, |
| 986 |
tol_ot=tol_ot, |
| 987 |
log=False, |
| 988 |
verbose=False, |
| 989 |
) |
| 990 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 991 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 992 |
|
| 993 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 994 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 995 |
|
| 996 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 997 |
X=xs_nx, |
| 998 |
Y=xt_nx, |
| 999 |
wx_samp=px_s_nx, |
| 1000 |
wx_feat=px_f_nx, |
| 1001 |
wy_samp=py_s_nx, |
| 1002 |
wy_feat=py_f_nx, |
| 1003 |
reg_marginals=reg_m, |
| 1004 |
epsilon=eps, |
| 1005 |
divergence=divergence, |
| 1006 |
unbalanced_solver=unbalanced_solver, |
| 1007 |
alpha=opt, |
| 1008 |
M_samp=M_samp_nx, |
| 1009 |
M_feat=M_feat_nx, |
| 1010 |
init_pi=None, |
| 1011 |
init_duals=None, |
| 1012 |
max_iter=max_iter, |
| 1013 |
tol=tol, |
| 1014 |
max_iter_ot=max_iter_ot, |
| 1015 |
tol_ot=tol_ot, |
| 1016 |
log=False, |
| 1017 |
verbose=False, |
| 1018 |
) |
| 1019 |
|
| 1020 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 1021 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 1022 |
|
| 1023 |
|
| 1024 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 1025 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 1026 |
@pytest.mark.parametrize( |
| 1027 |
"unbalanced_solver, divergence, eps", |
| 1028 |
itertools.product( |
| 1029 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1] |
| 1030 |
), |
| 1031 |
) |
| 1032 |
def test_log(nx, unbalanced_solver, divergence, eps): |
| 1033 |
n_samples = 5 |
| 1034 |
|
| 1035 |
mu_s = np.array([0, 0]) |
| 1036 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 1037 |
|
| 1038 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 1039 |
xt = xs[::-1].copy() |
| 1040 |
|
| 1041 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 1042 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 1043 |
|
| 1044 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 1045 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 1046 |
|
| 1047 |
reg_m = (10, 5) |
| 1048 |
alpha = (0.1, 0.2) |
| 1049 |
max_iter_ot = 5 |
| 1050 |
max_iter = 5 |
| 1051 |
tol = 1e-7 |
| 1052 |
tol_ot = 1e-7 |
| 1053 |
|
| 1054 |
|
| 1055 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 1056 |
X=xs, |
| 1057 |
Y=xt, |
| 1058 |
wx_samp=px_s, |
| 1059 |
wx_feat=px_f, |
| 1060 |
wy_samp=py_s, |
| 1061 |
wy_feat=py_f, |
| 1062 |
reg_marginals=reg_m, |
| 1063 |
epsilon=eps, |
| 1064 |
divergence=divergence, |
| 1065 |
unbalanced_solver=unbalanced_solver, |
| 1066 |
alpha=alpha, |
| 1067 |
M_samp=None, |
| 1068 |
M_feat=None, |
| 1069 |
init_pi=None, |
| 1070 |
init_duals=None, |
| 1071 |
max_iter=max_iter, |
| 1072 |
tol=tol, |
| 1073 |
max_iter_ot=max_iter_ot, |
| 1074 |
tol_ot=tol_ot, |
| 1075 |
log=False, |
| 1076 |
verbose=False, |
| 1077 |
) |
| 1078 |
|
| 1079 |
pi_sample_nx, pi_feature_nx, log = unbalanced_co_optimal_transport( |
| 1080 |
X=xs_nx, |
| 1081 |
Y=xt_nx, |
| 1082 |
wx_samp=px_s_nx, |
| 1083 |
wx_feat=px_f_nx, |
| 1084 |
wy_samp=py_s_nx, |
| 1085 |
wy_feat=py_f_nx, |
| 1086 |
reg_marginals=reg_m, |
| 1087 |
epsilon=eps, |
| 1088 |
divergence=divergence, |
| 1089 |
unbalanced_solver=unbalanced_solver, |
| 1090 |
alpha=alpha, |
| 1091 |
M_samp=None, |
| 1092 |
M_feat=None, |
| 1093 |
init_pi=None, |
| 1094 |
init_duals=None, |
| 1095 |
max_iter=max_iter, |
| 1096 |
tol=tol, |
| 1097 |
max_iter_ot=max_iter_ot, |
| 1098 |
tol_ot=tol_ot, |
| 1099 |
log=True, |
| 1100 |
verbose=False, |
| 1101 |
) |
| 1102 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 1103 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 1104 |
|
| 1105 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 1106 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 1107 |
|
| 1108 |
|
| 1109 |
ucoot = unbalanced_co_optimal_transport2( |
| 1110 |
X=xs, |
| 1111 |
Y=xt, |
| 1112 |
wx_samp=px_s, |
| 1113 |
wx_feat=px_f, |
| 1114 |
wy_samp=py_s, |
| 1115 |
wy_feat=py_f, |
| 1116 |
reg_marginals=reg_m, |
| 1117 |
epsilon=eps, |
| 1118 |
divergence=divergence, |
| 1119 |
unbalanced_solver=unbalanced_solver, |
| 1120 |
alpha=alpha, |
| 1121 |
M_samp=None, |
| 1122 |
M_feat=None, |
| 1123 |
init_pi=None, |
| 1124 |
init_duals=None, |
| 1125 |
max_iter=max_iter, |
| 1126 |
tol=tol, |
| 1127 |
max_iter_ot=max_iter_ot, |
| 1128 |
tol_ot=tol_ot, |
| 1129 |
log=False, |
| 1130 |
verbose=False, |
| 1131 |
) |
| 1132 |
|
| 1133 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 1134 |
X=xs_nx, |
| 1135 |
Y=xt_nx, |
| 1136 |
wx_samp=px_s_nx, |
| 1137 |
wx_feat=px_f_nx, |
| 1138 |
wy_samp=py_s_nx, |
| 1139 |
wy_feat=py_f_nx, |
| 1140 |
reg_marginals=reg_m, |
| 1141 |
epsilon=eps, |
| 1142 |
divergence=divergence, |
| 1143 |
unbalanced_solver=unbalanced_solver, |
| 1144 |
alpha=alpha, |
| 1145 |
M_samp=None, |
| 1146 |
M_feat=None, |
| 1147 |
init_pi=None, |
| 1148 |
init_duals=None, |
| 1149 |
max_iter=max_iter, |
| 1150 |
tol=tol, |
| 1151 |
max_iter_ot=max_iter_ot, |
| 1152 |
tol_ot=tol_ot, |
| 1153 |
log=False, |
| 1154 |
verbose=False, |
| 1155 |
) |
| 1156 |
|
| 1157 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 1158 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 1159 |
|
| 1160 |
|
| 1161 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 1162 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 1163 |
@pytest.mark.parametrize( |
| 1164 |
"unbalanced_solver, divergence, eps", |
| 1165 |
itertools.product( |
| 1166 |
["sinkhorn", "sinkhorn_log", "mm", "lbfgsb"], ["kl", "l2"], [0, 1] |
| 1167 |
), |
| 1168 |
) |
| 1169 |
def test_marginals(nx, unbalanced_solver, divergence, eps): |
| 1170 |
n_samples = 5 |
| 1171 |
|
| 1172 |
mu_s = np.array([0, 0]) |
| 1173 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 1174 |
|
| 1175 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 1176 |
xt = xs[::-1].copy() |
| 1177 |
|
| 1178 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 1179 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 1180 |
|
| 1181 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 1182 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 1183 |
|
| 1184 |
reg_m = (10, 5) |
| 1185 |
alpha = (0.1, 0.2) |
| 1186 |
max_iter_ot = 5 |
| 1187 |
max_iter = 5 |
| 1188 |
tol = 1e-7 |
| 1189 |
tol_ot = 1e-7 |
| 1190 |
|
| 1191 |
|
| 1192 |
pi_sample, pi_feature = unbalanced_co_optimal_transport( |
| 1193 |
X=xs, |
| 1194 |
Y=xt, |
| 1195 |
wx_samp=None, |
| 1196 |
wx_feat=None, |
| 1197 |
wy_samp=None, |
| 1198 |
wy_feat=None, |
| 1199 |
reg_marginals=reg_m, |
| 1200 |
epsilon=eps, |
| 1201 |
divergence=divergence, |
| 1202 |
unbalanced_solver=unbalanced_solver, |
| 1203 |
alpha=alpha, |
| 1204 |
M_samp=None, |
| 1205 |
M_feat=None, |
| 1206 |
init_pi=None, |
| 1207 |
init_duals=None, |
| 1208 |
max_iter=max_iter, |
| 1209 |
tol=tol, |
| 1210 |
max_iter_ot=max_iter_ot, |
| 1211 |
tol_ot=tol_ot, |
| 1212 |
log=False, |
| 1213 |
verbose=False, |
| 1214 |
) |
| 1215 |
|
| 1216 |
pi_sample_nx, pi_feature_nx = unbalanced_co_optimal_transport( |
| 1217 |
X=xs_nx, |
| 1218 |
Y=xt_nx, |
| 1219 |
wx_samp=px_s_nx, |
| 1220 |
wx_feat=px_f_nx, |
| 1221 |
wy_samp=py_s_nx, |
| 1222 |
wy_feat=py_f_nx, |
| 1223 |
reg_marginals=reg_m, |
| 1224 |
epsilon=eps, |
| 1225 |
divergence=divergence, |
| 1226 |
unbalanced_solver=unbalanced_solver, |
| 1227 |
alpha=alpha, |
| 1228 |
M_samp=None, |
| 1229 |
M_feat=None, |
| 1230 |
init_pi=None, |
| 1231 |
init_duals=None, |
| 1232 |
max_iter=max_iter, |
| 1233 |
tol=tol, |
| 1234 |
max_iter_ot=max_iter_ot, |
| 1235 |
tol_ot=tol_ot, |
| 1236 |
log=False, |
| 1237 |
verbose=False, |
| 1238 |
) |
| 1239 |
pi_sample_nx = nx.to_numpy(pi_sample_nx) |
| 1240 |
pi_feature_nx = nx.to_numpy(pi_feature_nx) |
| 1241 |
|
| 1242 |
np.testing.assert_allclose(pi_sample_nx, pi_sample, atol=1e-06) |
| 1243 |
np.testing.assert_allclose(pi_feature, pi_feature_nx, atol=1e-06) |
| 1244 |
|
| 1245 |
|
| 1246 |
ucoot = unbalanced_co_optimal_transport2( |
| 1247 |
X=xs, |
| 1248 |
Y=xt, |
| 1249 |
wx_samp=None, |
| 1250 |
wx_feat=None, |
| 1251 |
wy_samp=None, |
| 1252 |
wy_feat=None, |
| 1253 |
reg_marginals=reg_m, |
| 1254 |
epsilon=eps, |
| 1255 |
divergence=divergence, |
| 1256 |
unbalanced_solver=unbalanced_solver, |
| 1257 |
alpha=alpha, |
| 1258 |
M_samp=None, |
| 1259 |
M_feat=None, |
| 1260 |
init_pi=None, |
| 1261 |
init_duals=None, |
| 1262 |
max_iter=max_iter, |
| 1263 |
tol=tol, |
| 1264 |
max_iter_ot=max_iter_ot, |
| 1265 |
tol_ot=tol_ot, |
| 1266 |
log=False, |
| 1267 |
verbose=False, |
| 1268 |
) |
| 1269 |
|
| 1270 |
ucoot_nx = unbalanced_co_optimal_transport2( |
| 1271 |
X=xs_nx, |
| 1272 |
Y=xt_nx, |
| 1273 |
wx_samp=px_s_nx, |
| 1274 |
wx_feat=px_f_nx, |
| 1275 |
wy_samp=py_s_nx, |
| 1276 |
wy_feat=py_f_nx, |
| 1277 |
reg_marginals=reg_m, |
| 1278 |
epsilon=eps, |
| 1279 |
divergence=divergence, |
| 1280 |
unbalanced_solver=unbalanced_solver, |
| 1281 |
alpha=alpha, |
| 1282 |
M_samp=None, |
| 1283 |
M_feat=None, |
| 1284 |
init_pi=None, |
| 1285 |
init_duals=None, |
| 1286 |
max_iter=max_iter, |
| 1287 |
tol=tol, |
| 1288 |
max_iter_ot=max_iter_ot, |
| 1289 |
tol_ot=tol_ot, |
| 1290 |
log=False, |
| 1291 |
verbose=False, |
| 1292 |
) |
| 1293 |
|
| 1294 |
ucoot_nx = nx.to_numpy(ucoot_nx) |
| 1295 |
np.testing.assert_allclose(ucoot, ucoot_nx, atol=1e-08) |
| 1296 |
|
| 1297 |
|
| 1298 |
@pytest.skip_backend("jax", reason="test very slow with jax backend") |
| 1299 |
@pytest.skip_backend("tf", reason="test very slow with tensorflow backend") |
| 1300 |
def test_raise_value_error(nx): |
| 1301 |
n_samples = 5 |
| 1302 |
|
| 1303 |
mu_s = np.array([0, 0]) |
| 1304 |
cov_s = np.array([[1, 0], [0, 1]]) |
| 1305 |
|
| 1306 |
xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s, random_state=4) |
| 1307 |
xt = xs[::-1].copy() |
| 1308 |
|
| 1309 |
px_s, px_f = ot.unif(n_samples), ot.unif(2) |
| 1310 |
py_s, py_f = ot.unif(n_samples), ot.unif(2) |
| 1311 |
|
| 1312 |
xs_nx, xt_nx = nx.from_numpy(xs, xt) |
| 1313 |
px_s_nx, px_f_nx, py_s_nx, py_f_nx = nx.from_numpy(px_s, px_f, py_s, py_f) |
| 1314 |
|
| 1315 |
reg_m = (10, 5) |
| 1316 |
eps = 0 |
| 1317 |
max_iter_ot = 5 |
| 1318 |
max_iter = 5 |
| 1319 |
tol = 1e-7 |
| 1320 |
tol_ot = 1e-7 |
| 1321 |
|
| 1322 |
|
| 1323 |
def ucoot_div(divergence): |
| 1324 |
return unbalanced_co_optimal_transport( |
| 1325 |
X=xs, |
| 1326 |
Y=xt, |
| 1327 |
wx_samp=px_s, |
| 1328 |
wx_feat=px_f, |
| 1329 |
wy_samp=py_s, |
| 1330 |
wy_feat=py_f, |
| 1331 |
reg_marginals=reg_m, |
| 1332 |
epsilon=eps, |
| 1333 |
divergence=divergence, |
| 1334 |
unbalanced_solver="mm", |
| 1335 |
alpha=0, |
| 1336 |
M_samp=None, |
| 1337 |
M_feat=None, |
| 1338 |
init_pi=None, |
| 1339 |
init_duals=None, |
| 1340 |
max_iter=max_iter, |
| 1341 |
tol=tol, |
| 1342 |
max_iter_ot=max_iter_ot, |
| 1343 |
tol_ot=tol_ot, |
| 1344 |
log=False, |
| 1345 |
verbose=False, |
| 1346 |
) |
| 1347 |
|
| 1348 |
def ucoot_div_nx(divergence): |
| 1349 |
return unbalanced_co_optimal_transport( |
| 1350 |
X=xs_nx, |
| 1351 |
Y=xt_nx, |
| 1352 |
wx_samp=px_s_nx, |
| 1353 |
wx_feat=px_f_nx, |
| 1354 |
wy_samp=py_s_nx, |
| 1355 |
wy_feat=py_f_nx, |
| 1356 |
reg_marginals=reg_m, |
| 1357 |
epsilon=eps, |
| 1358 |
divergence=divergence, |
| 1359 |
unbalanced_solver="mm", |
| 1360 |
alpha=0, |
| 1361 |
M_samp=None, |
| 1362 |
M_feat=None, |
| 1363 |
init_pi=None, |
| 1364 |
init_duals=None, |
| 1365 |
max_iter=max_iter, |
| 1366 |
tol=tol, |
| 1367 |
max_iter_ot=max_iter_ot, |
| 1368 |
tol_ot=tol_ot, |
| 1369 |
log=False, |
| 1370 |
verbose=False, |
| 1371 |
) |
| 1372 |
|
| 1373 |
np.testing.assert_raises(NotImplementedError, ucoot_div, "div_not_existed") |
| 1374 |
np.testing.assert_raises(NotImplementedError, ucoot_div_nx, "div_not_existed") |
| 1375 |
|
| 1376 |
|
| 1377 |
def ucoot_solver(unbalanced_solver): |
| 1378 |
return unbalanced_co_optimal_transport( |
| 1379 |
X=xs, |
| 1380 |
Y=xt, |
| 1381 |
wx_samp=px_s, |
| 1382 |
wx_feat=px_f, |
| 1383 |
wy_samp=py_s, |
| 1384 |
wy_feat=py_f, |
| 1385 |
reg_marginals=reg_m, |
| 1386 |
epsilon=eps, |
| 1387 |
divergence="kl", |
| 1388 |
unbalanced_solver=unbalanced_solver, |
| 1389 |
alpha=0, |
| 1390 |
M_samp=None, |
| 1391 |
M_feat=None, |
| 1392 |
init_pi=None, |
| 1393 |
init_duals=None, |
| 1394 |
max_iter=max_iter, |
| 1395 |
tol=tol, |
| 1396 |
max_iter_ot=max_iter_ot, |
| 1397 |
tol_ot=tol_ot, |
| 1398 |
log=False, |
| 1399 |
verbose=False, |
| 1400 |
) |
| 1401 |
|
| 1402 |
def ucoot_solver_nx(unbalanced_solver): |
| 1403 |
return unbalanced_co_optimal_transport( |
| 1404 |
X=xs_nx, |
| 1405 |
Y=xt_nx, |
| 1406 |
wx_samp=px_s_nx, |
| 1407 |
wx_feat=px_f_nx, |
| 1408 |
wy_samp=py_s_nx, |
| 1409 |
wy_feat=py_f_nx, |
| 1410 |
reg_marginals=reg_m, |
| 1411 |
epsilon=eps, |
| 1412 |
divergence="kl", |
| 1413 |
unbalanced_solver=unbalanced_solver, |
| 1414 |
alpha=0, |
| 1415 |
M_samp=None, |
| 1416 |
M_feat=None, |
| 1417 |
init_pi=None, |
| 1418 |
init_duals=None, |
| 1419 |
max_iter=max_iter, |
| 1420 |
tol=tol, |
| 1421 |
max_iter_ot=max_iter_ot, |
| 1422 |
tol_ot=tol_ot, |
| 1423 |
log=False, |
| 1424 |
verbose=False, |
| 1425 |
) |
| 1426 |
|
| 1427 |
np.testing.assert_raises(NotImplementedError, ucoot_solver, "solver_not_existed") |
| 1428 |
np.testing.assert_raises(NotImplementedError, ucoot_solver_nx, "solver_not_existed") |