| 1 |
"""Tests for gromov._dictionary.py""" |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import numpy as np |
| 8 |
|
| 9 |
import ot |
| 10 |
|
| 11 |
|
| 12 |
def test_gromov_wasserstein_linear_unmixing(nx): |
| 13 |
n = 4 |
| 14 |
|
| 15 |
X1, y1 = ot.datasets.make_data_classif("3gauss", n, random_state=42) |
| 16 |
X2, y2 = ot.datasets.make_data_classif("3gauss2", n, random_state=42) |
| 17 |
|
| 18 |
C1 = ot.dist(X1) |
| 19 |
C2 = ot.dist(X2) |
| 20 |
Cdict = np.stack([C1, C2]) |
| 21 |
p = ot.unif(n) |
| 22 |
|
| 23 |
C1b, C2b, Cdictb, pb = nx.from_numpy(C1, C2, Cdict, p) |
| 24 |
|
| 25 |
tol = 10 ** (-5) |
| 26 |
|
| 27 |
reg = 0.0 |
| 28 |
unmixing1, C1_emb, OT, reconstruction1 = ( |
| 29 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 30 |
C1, |
| 31 |
Cdict, |
| 32 |
reg=reg, |
| 33 |
p=p, |
| 34 |
q=p, |
| 35 |
tol_outer=tol, |
| 36 |
tol_inner=tol, |
| 37 |
max_iter_outer=20, |
| 38 |
max_iter_inner=200, |
| 39 |
) |
| 40 |
) |
| 41 |
|
| 42 |
unmixing1b, C1b_emb, OTb, reconstruction1b = ( |
| 43 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 44 |
C1b, |
| 45 |
Cdictb, |
| 46 |
reg=reg, |
| 47 |
p=None, |
| 48 |
q=None, |
| 49 |
tol_outer=tol, |
| 50 |
tol_inner=tol, |
| 51 |
max_iter_outer=20, |
| 52 |
max_iter_inner=200, |
| 53 |
) |
| 54 |
) |
| 55 |
|
| 56 |
unmixing2, C2_emb, OT, reconstruction2 = ( |
| 57 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 58 |
C2, |
| 59 |
Cdict, |
| 60 |
reg=reg, |
| 61 |
p=None, |
| 62 |
q=None, |
| 63 |
tol_outer=tol, |
| 64 |
tol_inner=tol, |
| 65 |
max_iter_outer=20, |
| 66 |
max_iter_inner=200, |
| 67 |
) |
| 68 |
) |
| 69 |
|
| 70 |
unmixing2b, C2b_emb, OTb, reconstruction2b = ( |
| 71 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 72 |
C2b, |
| 73 |
Cdictb, |
| 74 |
reg=reg, |
| 75 |
p=pb, |
| 76 |
q=pb, |
| 77 |
tol_outer=tol, |
| 78 |
tol_inner=tol, |
| 79 |
max_iter_outer=20, |
| 80 |
max_iter_inner=200, |
| 81 |
) |
| 82 |
) |
| 83 |
|
| 84 |
np.testing.assert_allclose(unmixing1, nx.to_numpy(unmixing1b), atol=5e-06) |
| 85 |
np.testing.assert_allclose(unmixing1, [1.0, 0.0], atol=5e-01) |
| 86 |
np.testing.assert_allclose(unmixing2, nx.to_numpy(unmixing2b), atol=5e-06) |
| 87 |
np.testing.assert_allclose(unmixing2, [0.0, 1.0], atol=5e-01) |
| 88 |
np.testing.assert_allclose(C1_emb, nx.to_numpy(C1b_emb), atol=1e-06) |
| 89 |
np.testing.assert_allclose(C2_emb, nx.to_numpy(C2b_emb), atol=1e-06) |
| 90 |
np.testing.assert_allclose( |
| 91 |
reconstruction1, nx.to_numpy(reconstruction1b), atol=1e-06 |
| 92 |
) |
| 93 |
np.testing.assert_allclose( |
| 94 |
reconstruction2, nx.to_numpy(reconstruction2b), atol=1e-06 |
| 95 |
) |
| 96 |
np.testing.assert_allclose(C1b_emb.shape, (n, n)) |
| 97 |
np.testing.assert_allclose(C2b_emb.shape, (n, n)) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
reg = 0.001 |
| 102 |
unmixing1, C1_emb, OT, reconstruction1 = ( |
| 103 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 104 |
C1, |
| 105 |
Cdict, |
| 106 |
reg=reg, |
| 107 |
p=p, |
| 108 |
q=p, |
| 109 |
tol_outer=tol, |
| 110 |
tol_inner=tol, |
| 111 |
max_iter_outer=20, |
| 112 |
max_iter_inner=200, |
| 113 |
) |
| 114 |
) |
| 115 |
|
| 116 |
unmixing1b, C1b_emb, OTb, reconstruction1b = ( |
| 117 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 118 |
C1b, |
| 119 |
Cdictb, |
| 120 |
reg=reg, |
| 121 |
p=None, |
| 122 |
q=None, |
| 123 |
tol_outer=tol, |
| 124 |
tol_inner=tol, |
| 125 |
max_iter_outer=20, |
| 126 |
max_iter_inner=200, |
| 127 |
) |
| 128 |
) |
| 129 |
|
| 130 |
unmixing2, C2_emb, OT, reconstruction2 = ( |
| 131 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 132 |
C2, |
| 133 |
Cdict, |
| 134 |
reg=reg, |
| 135 |
p=None, |
| 136 |
q=None, |
| 137 |
tol_outer=tol, |
| 138 |
tol_inner=tol, |
| 139 |
max_iter_outer=20, |
| 140 |
max_iter_inner=200, |
| 141 |
) |
| 142 |
) |
| 143 |
|
| 144 |
unmixing2b, C2b_emb, OTb, reconstruction2b = ( |
| 145 |
ot.gromov.gromov_wasserstein_linear_unmixing( |
| 146 |
C2b, |
| 147 |
Cdictb, |
| 148 |
reg=reg, |
| 149 |
p=pb, |
| 150 |
q=pb, |
| 151 |
tol_outer=tol, |
| 152 |
tol_inner=tol, |
| 153 |
max_iter_outer=20, |
| 154 |
max_iter_inner=200, |
| 155 |
) |
| 156 |
) |
| 157 |
|
| 158 |
np.testing.assert_allclose(unmixing1, nx.to_numpy(unmixing1b), atol=1e-06) |
| 159 |
np.testing.assert_allclose(unmixing1, [1.0, 0.0], atol=1e-01) |
| 160 |
np.testing.assert_allclose(unmixing2, nx.to_numpy(unmixing2b), atol=1e-06) |
| 161 |
np.testing.assert_allclose(unmixing2, [0.0, 1.0], atol=1e-01) |
| 162 |
np.testing.assert_allclose(C1_emb, nx.to_numpy(C1b_emb), atol=1e-06) |
| 163 |
np.testing.assert_allclose(C2_emb, nx.to_numpy(C2b_emb), atol=1e-06) |
| 164 |
np.testing.assert_allclose( |
| 165 |
reconstruction1, nx.to_numpy(reconstruction1b), atol=1e-06 |
| 166 |
) |
| 167 |
np.testing.assert_allclose( |
| 168 |
reconstruction2, nx.to_numpy(reconstruction2b), atol=1e-06 |
| 169 |
) |
| 170 |
np.testing.assert_allclose(C1b_emb.shape, (n, n)) |
| 171 |
np.testing.assert_allclose(C2b_emb.shape, (n, n)) |
| 172 |
|
| 173 |
|
| 174 |
def test_gromov_wasserstein_dictionary_learning(nx): |
| 175 |
|
| 176 |
shape = 4 |
| 177 |
n_samples = 2 |
| 178 |
n_atoms = 2 |
| 179 |
projection = "nonnegative_symmetric" |
| 180 |
X1, y1 = ot.datasets.make_data_classif("3gauss", shape, random_state=42) |
| 181 |
X2, y2 = ot.datasets.make_data_classif("3gauss2", shape, random_state=42) |
| 182 |
C1 = ot.dist(X1) |
| 183 |
C2 = ot.dist(X2) |
| 184 |
Cs = [C1.copy() for _ in range(n_samples // 2)] + [ |
| 185 |
C2.copy() for _ in range(n_samples // 2) |
| 186 |
] |
| 187 |
ps = [ot.unif(shape) for _ in range(n_samples)] |
| 188 |
q = ot.unif(shape) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
dataset_means = [C.mean() for C in Cs] |
| 193 |
rng = np.random.RandomState(0) |
| 194 |
Cdict_init = rng.normal( |
| 195 |
loc=np.mean(dataset_means), |
| 196 |
scale=np.std(dataset_means), |
| 197 |
size=(n_atoms, shape, shape), |
| 198 |
) |
| 199 |
|
| 200 |
if projection == "nonnegative_symmetric": |
| 201 |
Cdict_init = 0.5 * (Cdict_init + Cdict_init.transpose((0, 2, 1))) |
| 202 |
Cdict_init[Cdict_init < 0.0] = 0.0 |
| 203 |
|
| 204 |
Csb = nx.from_numpy(*Cs) |
| 205 |
psb = nx.from_numpy(*ps) |
| 206 |
qb, Cdict_initb = nx.from_numpy(q, Cdict_init) |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
use_adam_optimizer = True |
| 211 |
verbose = False |
| 212 |
tol = 10 ** (-5) |
| 213 |
epochs = 1 |
| 214 |
|
| 215 |
initial_total_reconstruction = 0 |
| 216 |
for i in range(n_samples): |
| 217 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 218 |
Cs[i], |
| 219 |
Cdict_init, |
| 220 |
p=ps[i], |
| 221 |
q=q, |
| 222 |
reg=0.0, |
| 223 |
tol_outer=tol, |
| 224 |
tol_inner=tol, |
| 225 |
max_iter_outer=10, |
| 226 |
max_iter_inner=50, |
| 227 |
) |
| 228 |
initial_total_reconstruction += reconstruction |
| 229 |
|
| 230 |
|
| 231 |
Cdict, log = ot.gromov.gromov_wasserstein_dictionary_learning( |
| 232 |
Cs, |
| 233 |
D=n_atoms, |
| 234 |
nt=shape, |
| 235 |
ps=ps, |
| 236 |
q=q, |
| 237 |
Cdict_init=Cdict_init, |
| 238 |
epochs=epochs, |
| 239 |
batch_size=2 * n_samples, |
| 240 |
learning_rate=1.0, |
| 241 |
reg=0.0, |
| 242 |
tol_outer=tol, |
| 243 |
tol_inner=tol, |
| 244 |
max_iter_outer=10, |
| 245 |
max_iter_inner=50, |
| 246 |
projection=projection, |
| 247 |
use_log=False, |
| 248 |
use_adam_optimizer=use_adam_optimizer, |
| 249 |
verbose=verbose, |
| 250 |
) |
| 251 |
|
| 252 |
total_reconstruction = 0 |
| 253 |
for i in range(n_samples): |
| 254 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 255 |
Cs[i], |
| 256 |
Cdict, |
| 257 |
p=None, |
| 258 |
q=None, |
| 259 |
reg=0.0, |
| 260 |
tol_outer=tol, |
| 261 |
tol_inner=tol, |
| 262 |
max_iter_outer=10, |
| 263 |
max_iter_inner=50, |
| 264 |
) |
| 265 |
total_reconstruction += reconstruction |
| 266 |
|
| 267 |
np.testing.assert_array_less(total_reconstruction, initial_total_reconstruction) |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
Cdictb, log = ot.gromov.gromov_wasserstein_dictionary_learning( |
| 272 |
Csb, |
| 273 |
D=n_atoms, |
| 274 |
nt=shape, |
| 275 |
ps=None, |
| 276 |
q=None, |
| 277 |
Cdict_init=Cdict_initb, |
| 278 |
epochs=epochs, |
| 279 |
batch_size=n_samples, |
| 280 |
learning_rate=1.0, |
| 281 |
reg=0.0, |
| 282 |
tol_outer=tol, |
| 283 |
tol_inner=tol, |
| 284 |
max_iter_outer=10, |
| 285 |
max_iter_inner=50, |
| 286 |
projection=projection, |
| 287 |
use_log=False, |
| 288 |
use_adam_optimizer=use_adam_optimizer, |
| 289 |
verbose=verbose, |
| 290 |
) |
| 291 |
|
| 292 |
total_reconstruction_b = 0 |
| 293 |
for i in range(n_samples): |
| 294 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 295 |
Csb[i], |
| 296 |
Cdictb, |
| 297 |
p=psb[i], |
| 298 |
q=qb, |
| 299 |
reg=0.0, |
| 300 |
tol_outer=tol, |
| 301 |
tol_inner=tol, |
| 302 |
max_iter_outer=10, |
| 303 |
max_iter_inner=50, |
| 304 |
) |
| 305 |
total_reconstruction_b += reconstruction |
| 306 |
|
| 307 |
total_reconstruction_b = nx.to_numpy(total_reconstruction_b) |
| 308 |
np.testing.assert_array_less(total_reconstruction_b, initial_total_reconstruction) |
| 309 |
np.testing.assert_allclose(total_reconstruction_b, total_reconstruction, atol=1e-05) |
| 310 |
np.testing.assert_allclose(total_reconstruction_b, total_reconstruction, atol=1e-05) |
| 311 |
np.testing.assert_allclose(Cdict, nx.to_numpy(Cdictb), atol=1e-03) |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
Cdict_bis, log = ot.gromov.gromov_wasserstein_dictionary_learning( |
| 316 |
Cs, |
| 317 |
D=n_atoms, |
| 318 |
nt=shape, |
| 319 |
ps=None, |
| 320 |
q=None, |
| 321 |
Cdict_init=None, |
| 322 |
epochs=epochs, |
| 323 |
batch_size=n_samples, |
| 324 |
learning_rate=1.0, |
| 325 |
reg=0.0, |
| 326 |
tol_outer=tol, |
| 327 |
tol_inner=tol, |
| 328 |
max_iter_outer=10, |
| 329 |
max_iter_inner=50, |
| 330 |
projection=projection, |
| 331 |
use_log=False, |
| 332 |
use_adam_optimizer=use_adam_optimizer, |
| 333 |
verbose=verbose, |
| 334 |
random_state=0, |
| 335 |
) |
| 336 |
|
| 337 |
total_reconstruction_bis = 0 |
| 338 |
for i in range(n_samples): |
| 339 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 340 |
Cs[i], |
| 341 |
Cdict_bis, |
| 342 |
p=ps[i], |
| 343 |
q=q, |
| 344 |
reg=0.0, |
| 345 |
tol_outer=tol, |
| 346 |
tol_inner=tol, |
| 347 |
max_iter_outer=10, |
| 348 |
max_iter_inner=50, |
| 349 |
) |
| 350 |
total_reconstruction_bis += reconstruction |
| 351 |
|
| 352 |
np.testing.assert_allclose( |
| 353 |
total_reconstruction_bis, total_reconstruction, atol=1e-05 |
| 354 |
) |
| 355 |
|
| 356 |
|
| 357 |
Cdictb_bis, log = ot.gromov.gromov_wasserstein_dictionary_learning( |
| 358 |
Csb, |
| 359 |
D=n_atoms, |
| 360 |
nt=shape, |
| 361 |
ps=psb, |
| 362 |
q=qb, |
| 363 |
Cdict_init=None, |
| 364 |
epochs=epochs, |
| 365 |
batch_size=n_samples, |
| 366 |
learning_rate=1.0, |
| 367 |
reg=0.0, |
| 368 |
tol_outer=tol, |
| 369 |
tol_inner=tol, |
| 370 |
max_iter_outer=10, |
| 371 |
max_iter_inner=50, |
| 372 |
projection=projection, |
| 373 |
use_log=False, |
| 374 |
use_adam_optimizer=use_adam_optimizer, |
| 375 |
verbose=verbose, |
| 376 |
random_state=0, |
| 377 |
) |
| 378 |
|
| 379 |
total_reconstruction_b_bis = 0 |
| 380 |
for i in range(n_samples): |
| 381 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 382 |
Csb[i], |
| 383 |
Cdictb_bis, |
| 384 |
p=None, |
| 385 |
q=None, |
| 386 |
reg=0.0, |
| 387 |
tol_outer=tol, |
| 388 |
tol_inner=tol, |
| 389 |
max_iter_outer=10, |
| 390 |
max_iter_inner=50, |
| 391 |
) |
| 392 |
total_reconstruction_b_bis += reconstruction |
| 393 |
|
| 394 |
total_reconstruction_b_bis = nx.to_numpy(total_reconstruction_b_bis) |
| 395 |
np.testing.assert_allclose( |
| 396 |
total_reconstruction_b_bis, total_reconstruction_b, atol=1e-05 |
| 397 |
) |
| 398 |
np.testing.assert_allclose(Cdict_bis, nx.to_numpy(Cdictb_bis), atol=1e-03) |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
use_adam_optimizer = False |
| 404 |
verbose = True |
| 405 |
use_log = True |
| 406 |
|
| 407 |
Cdict_bis2, log = ot.gromov.gromov_wasserstein_dictionary_learning( |
| 408 |
Cs, |
| 409 |
D=n_atoms, |
| 410 |
nt=shape, |
| 411 |
ps=ps, |
| 412 |
q=q, |
| 413 |
Cdict_init=Cdict, |
| 414 |
epochs=epochs, |
| 415 |
batch_size=n_samples, |
| 416 |
learning_rate=10.0, |
| 417 |
reg=0.0, |
| 418 |
tol_outer=tol, |
| 419 |
tol_inner=tol, |
| 420 |
max_iter_outer=10, |
| 421 |
max_iter_inner=50, |
| 422 |
projection=projection, |
| 423 |
use_log=use_log, |
| 424 |
use_adam_optimizer=use_adam_optimizer, |
| 425 |
verbose=verbose, |
| 426 |
random_state=0, |
| 427 |
) |
| 428 |
|
| 429 |
total_reconstruction_bis2 = 0 |
| 430 |
for i in range(n_samples): |
| 431 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 432 |
Cs[i], |
| 433 |
Cdict_bis2, |
| 434 |
p=ps[i], |
| 435 |
q=q, |
| 436 |
reg=0.0, |
| 437 |
tol_outer=tol, |
| 438 |
tol_inner=tol, |
| 439 |
max_iter_outer=10, |
| 440 |
max_iter_inner=50, |
| 441 |
) |
| 442 |
total_reconstruction_bis2 += reconstruction |
| 443 |
|
| 444 |
np.testing.assert_array_less(total_reconstruction_bis2, total_reconstruction) |
| 445 |
|
| 446 |
|
| 447 |
Cdictb_bis2, log = ot.gromov.gromov_wasserstein_dictionary_learning( |
| 448 |
Csb, |
| 449 |
D=n_atoms, |
| 450 |
nt=shape, |
| 451 |
ps=psb, |
| 452 |
q=qb, |
| 453 |
Cdict_init=Cdictb, |
| 454 |
epochs=epochs, |
| 455 |
batch_size=n_samples, |
| 456 |
learning_rate=10.0, |
| 457 |
reg=0.0, |
| 458 |
tol_outer=tol, |
| 459 |
tol_inner=tol, |
| 460 |
max_iter_outer=10, |
| 461 |
max_iter_inner=50, |
| 462 |
projection=projection, |
| 463 |
use_log=use_log, |
| 464 |
use_adam_optimizer=use_adam_optimizer, |
| 465 |
verbose=verbose, |
| 466 |
random_state=0, |
| 467 |
) |
| 468 |
|
| 469 |
total_reconstruction_b_bis2 = 0 |
| 470 |
for i in range(n_samples): |
| 471 |
_, _, _, reconstruction = ot.gromov.gromov_wasserstein_linear_unmixing( |
| 472 |
Csb[i], |
| 473 |
Cdictb_bis2, |
| 474 |
p=psb[i], |
| 475 |
q=qb, |
| 476 |
reg=0.0, |
| 477 |
tol_outer=tol, |
| 478 |
tol_inner=tol, |
| 479 |
max_iter_outer=10, |
| 480 |
max_iter_inner=50, |
| 481 |
) |
| 482 |
total_reconstruction_b_bis2 += reconstruction |
| 483 |
|
| 484 |
total_reconstruction_b_bis2 = nx.to_numpy(total_reconstruction_b_bis2) |
| 485 |
np.testing.assert_allclose( |
| 486 |
total_reconstruction_b_bis2, total_reconstruction_bis2, atol=1e-05 |
| 487 |
) |
| 488 |
|
| 489 |
|
| 490 |
def test_fused_gromov_wasserstein_linear_unmixing(nx): |
| 491 |
n = 4 |
| 492 |
X1, y1 = ot.datasets.make_data_classif("3gauss", n, random_state=42) |
| 493 |
X2, y2 = ot.datasets.make_data_classif("3gauss2", n, random_state=42) |
| 494 |
F, y = ot.datasets.make_data_classif("3gauss", n, random_state=42) |
| 495 |
|
| 496 |
C1 = ot.dist(X1) |
| 497 |
C2 = ot.dist(X2) |
| 498 |
Cdict = np.stack([C1, C2]) |
| 499 |
Ydict = np.stack([F, F]) |
| 500 |
p = ot.unif(n) |
| 501 |
|
| 502 |
C1b, C2b, Fb, Cdictb, Ydictb, pb = nx.from_numpy(C1, C2, F, Cdict, Ydict, p) |
| 503 |
|
| 504 |
|
| 505 |
reg = 0.0 |
| 506 |
|
| 507 |
unmixing1, C1_emb, Y1_emb, OT, reconstruction1 = ( |
| 508 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 509 |
C1, |
| 510 |
F, |
| 511 |
Cdict, |
| 512 |
Ydict, |
| 513 |
p=p, |
| 514 |
q=p, |
| 515 |
alpha=0.5, |
| 516 |
reg=reg, |
| 517 |
tol_outer=10 ** (-6), |
| 518 |
tol_inner=10 ** (-6), |
| 519 |
max_iter_outer=10, |
| 520 |
max_iter_inner=50, |
| 521 |
) |
| 522 |
) |
| 523 |
|
| 524 |
unmixing1b, C1b_emb, Y1b_emb, OTb, reconstruction1b = ( |
| 525 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 526 |
C1b, |
| 527 |
Fb, |
| 528 |
Cdictb, |
| 529 |
Ydictb, |
| 530 |
p=None, |
| 531 |
q=None, |
| 532 |
alpha=0.5, |
| 533 |
reg=reg, |
| 534 |
tol_outer=10 ** (-6), |
| 535 |
tol_inner=10 ** (-6), |
| 536 |
max_iter_outer=10, |
| 537 |
max_iter_inner=50, |
| 538 |
) |
| 539 |
) |
| 540 |
|
| 541 |
unmixing2, C2_emb, Y2_emb, OT, reconstruction2 = ( |
| 542 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 543 |
C2, |
| 544 |
F, |
| 545 |
Cdict, |
| 546 |
Ydict, |
| 547 |
p=None, |
| 548 |
q=None, |
| 549 |
alpha=0.5, |
| 550 |
reg=reg, |
| 551 |
tol_outer=10 ** (-6), |
| 552 |
tol_inner=10 ** (-6), |
| 553 |
max_iter_outer=10, |
| 554 |
max_iter_inner=50, |
| 555 |
) |
| 556 |
) |
| 557 |
|
| 558 |
unmixing2b, C2b_emb, Y2b_emb, OTb, reconstruction2b = ( |
| 559 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 560 |
C2b, |
| 561 |
Fb, |
| 562 |
Cdictb, |
| 563 |
Ydictb, |
| 564 |
p=pb, |
| 565 |
q=pb, |
| 566 |
alpha=0.5, |
| 567 |
reg=reg, |
| 568 |
tol_outer=10 ** (-6), |
| 569 |
tol_inner=10 ** (-6), |
| 570 |
max_iter_outer=10, |
| 571 |
max_iter_inner=50, |
| 572 |
) |
| 573 |
) |
| 574 |
|
| 575 |
np.testing.assert_allclose(unmixing1, nx.to_numpy(unmixing1b), atol=4e-06) |
| 576 |
np.testing.assert_allclose(unmixing1, [1.0, 0.0], atol=4e-01) |
| 577 |
np.testing.assert_allclose(unmixing2, nx.to_numpy(unmixing2b), atol=4e-06) |
| 578 |
np.testing.assert_allclose(unmixing2, [0.0, 1.0], atol=4e-01) |
| 579 |
np.testing.assert_allclose(C1_emb, nx.to_numpy(C1b_emb), atol=1e-03) |
| 580 |
np.testing.assert_allclose(C2_emb, nx.to_numpy(C2b_emb), atol=1e-03) |
| 581 |
np.testing.assert_allclose(Y1_emb, nx.to_numpy(Y1b_emb), atol=1e-03) |
| 582 |
np.testing.assert_allclose(Y2_emb, nx.to_numpy(Y2b_emb), atol=1e-03) |
| 583 |
np.testing.assert_allclose( |
| 584 |
reconstruction1, nx.to_numpy(reconstruction1b), atol=1e-06 |
| 585 |
) |
| 586 |
np.testing.assert_allclose( |
| 587 |
reconstruction2, nx.to_numpy(reconstruction2b), atol=1e-06 |
| 588 |
) |
| 589 |
np.testing.assert_allclose(C1b_emb.shape, (n, n)) |
| 590 |
np.testing.assert_allclose(C2b_emb.shape, (n, n)) |
| 591 |
|
| 592 |
|
| 593 |
reg = 0.001 |
| 594 |
|
| 595 |
unmixing1, C1_emb, Y1_emb, OT, reconstruction1 = ( |
| 596 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 597 |
C1, |
| 598 |
F, |
| 599 |
Cdict, |
| 600 |
Ydict, |
| 601 |
p=p, |
| 602 |
q=p, |
| 603 |
alpha=0.5, |
| 604 |
reg=reg, |
| 605 |
tol_outer=10 ** (-6), |
| 606 |
tol_inner=10 ** (-6), |
| 607 |
max_iter_outer=10, |
| 608 |
max_iter_inner=50, |
| 609 |
) |
| 610 |
) |
| 611 |
|
| 612 |
unmixing1b, C1b_emb, Y1b_emb, OTb, reconstruction1b = ( |
| 613 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 614 |
C1b, |
| 615 |
Fb, |
| 616 |
Cdictb, |
| 617 |
Ydictb, |
| 618 |
p=None, |
| 619 |
q=None, |
| 620 |
alpha=0.5, |
| 621 |
reg=reg, |
| 622 |
tol_outer=10 ** (-6), |
| 623 |
tol_inner=10 ** (-6), |
| 624 |
max_iter_outer=10, |
| 625 |
max_iter_inner=50, |
| 626 |
) |
| 627 |
) |
| 628 |
|
| 629 |
unmixing2, C2_emb, Y2_emb, OT, reconstruction2 = ( |
| 630 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 631 |
C2, |
| 632 |
F, |
| 633 |
Cdict, |
| 634 |
Ydict, |
| 635 |
p=None, |
| 636 |
q=None, |
| 637 |
alpha=0.5, |
| 638 |
reg=reg, |
| 639 |
tol_outer=10 ** (-6), |
| 640 |
tol_inner=10 ** (-6), |
| 641 |
max_iter_outer=10, |
| 642 |
max_iter_inner=50, |
| 643 |
) |
| 644 |
) |
| 645 |
|
| 646 |
unmixing2b, C2b_emb, Y2b_emb, OTb, reconstruction2b = ( |
| 647 |
ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 648 |
C2b, |
| 649 |
Fb, |
| 650 |
Cdictb, |
| 651 |
Ydictb, |
| 652 |
p=pb, |
| 653 |
q=pb, |
| 654 |
alpha=0.5, |
| 655 |
reg=reg, |
| 656 |
tol_outer=10 ** (-6), |
| 657 |
tol_inner=10 ** (-6), |
| 658 |
max_iter_outer=10, |
| 659 |
max_iter_inner=50, |
| 660 |
) |
| 661 |
) |
| 662 |
|
| 663 |
np.testing.assert_allclose(unmixing1, nx.to_numpy(unmixing1b), atol=1e-06) |
| 664 |
np.testing.assert_allclose(unmixing1, [1.0, 0.0], atol=1e-01) |
| 665 |
np.testing.assert_allclose(unmixing2, nx.to_numpy(unmixing2b), atol=1e-06) |
| 666 |
np.testing.assert_allclose(unmixing2, [0.0, 1.0], atol=1e-01) |
| 667 |
np.testing.assert_allclose(C1_emb, nx.to_numpy(C1b_emb), atol=1e-03) |
| 668 |
np.testing.assert_allclose(C2_emb, nx.to_numpy(C2b_emb), atol=1e-03) |
| 669 |
np.testing.assert_allclose(Y1_emb, nx.to_numpy(Y1b_emb), atol=1e-03) |
| 670 |
np.testing.assert_allclose(Y2_emb, nx.to_numpy(Y2b_emb), atol=1e-03) |
| 671 |
np.testing.assert_allclose( |
| 672 |
reconstruction1, nx.to_numpy(reconstruction1b), atol=1e-06 |
| 673 |
) |
| 674 |
np.testing.assert_allclose( |
| 675 |
reconstruction2, nx.to_numpy(reconstruction2b), atol=1e-06 |
| 676 |
) |
| 677 |
np.testing.assert_allclose(C1b_emb.shape, (n, n)) |
| 678 |
np.testing.assert_allclose(C2b_emb.shape, (n, n)) |
| 679 |
|
| 680 |
|
| 681 |
def test_fused_gromov_wasserstein_dictionary_learning(nx): |
| 682 |
|
| 683 |
shape = 4 |
| 684 |
n_samples = 2 |
| 685 |
n_atoms = 2 |
| 686 |
projection = "nonnegative_symmetric" |
| 687 |
X1, y1 = ot.datasets.make_data_classif("3gauss", shape, random_state=42) |
| 688 |
X2, y2 = ot.datasets.make_data_classif("3gauss2", shape, random_state=42) |
| 689 |
F, y = ot.datasets.make_data_classif("3gauss", shape, random_state=42) |
| 690 |
|
| 691 |
C1 = ot.dist(X1) |
| 692 |
C2 = ot.dist(X2) |
| 693 |
Cs = [C1.copy() for _ in range(n_samples // 2)] + [ |
| 694 |
C2.copy() for _ in range(n_samples // 2) |
| 695 |
] |
| 696 |
Ys = [F.copy() for _ in range(n_samples)] |
| 697 |
ps = [ot.unif(shape) for _ in range(n_samples)] |
| 698 |
q = ot.unif(shape) |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
dataset_structure_means = [C.mean() for C in Cs] |
| 703 |
rng = np.random.RandomState(0) |
| 704 |
Cdict_init = rng.normal( |
| 705 |
loc=np.mean(dataset_structure_means), |
| 706 |
scale=np.std(dataset_structure_means), |
| 707 |
size=(n_atoms, shape, shape), |
| 708 |
) |
| 709 |
if projection == "nonnegative_symmetric": |
| 710 |
Cdict_init = 0.5 * (Cdict_init + Cdict_init.transpose((0, 2, 1))) |
| 711 |
Cdict_init[Cdict_init < 0.0] = 0.0 |
| 712 |
dataset_feature_means = np.stack([Y.mean(axis=0) for Y in Ys]) |
| 713 |
Ydict_init = rng.normal( |
| 714 |
loc=dataset_feature_means.mean(axis=0), |
| 715 |
scale=dataset_feature_means.std(axis=0), |
| 716 |
size=(n_atoms, shape, 2), |
| 717 |
) |
| 718 |
|
| 719 |
Csb = nx.from_numpy(*Cs) |
| 720 |
Ysb = nx.from_numpy(*Ys) |
| 721 |
psb = nx.from_numpy(*ps) |
| 722 |
qb, Cdict_initb, Ydict_initb = nx.from_numpy(q, Cdict_init, Ydict_init) |
| 723 |
|
| 724 |
|
| 725 |
alpha = 0.5 |
| 726 |
use_adam_optimizer = True |
| 727 |
verbose = False |
| 728 |
tol = 1e-05 |
| 729 |
epochs = 1 |
| 730 |
|
| 731 |
initial_total_reconstruction = 0 |
| 732 |
for i in range(n_samples): |
| 733 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 734 |
Cs[i], |
| 735 |
Ys[i], |
| 736 |
Cdict_init, |
| 737 |
Ydict_init, |
| 738 |
p=ps[i], |
| 739 |
q=q, |
| 740 |
alpha=alpha, |
| 741 |
reg=0.0, |
| 742 |
tol_outer=tol, |
| 743 |
tol_inner=tol, |
| 744 |
max_iter_outer=10, |
| 745 |
max_iter_inner=50, |
| 746 |
) |
| 747 |
initial_total_reconstruction += reconstruction |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
Cdict, Ydict, log = ot.gromov.fused_gromov_wasserstein_dictionary_learning( |
| 752 |
Cs, |
| 753 |
Ys, |
| 754 |
D=n_atoms, |
| 755 |
nt=shape, |
| 756 |
ps=ps, |
| 757 |
q=q, |
| 758 |
Cdict_init=Cdict_init, |
| 759 |
Ydict_init=Ydict_init, |
| 760 |
epochs=epochs, |
| 761 |
batch_size=n_samples, |
| 762 |
learning_rate_C=1.0, |
| 763 |
learning_rate_Y=1.0, |
| 764 |
alpha=alpha, |
| 765 |
reg=0.0, |
| 766 |
tol_outer=tol, |
| 767 |
tol_inner=tol, |
| 768 |
max_iter_outer=10, |
| 769 |
max_iter_inner=50, |
| 770 |
projection=projection, |
| 771 |
use_log=False, |
| 772 |
use_adam_optimizer=use_adam_optimizer, |
| 773 |
verbose=verbose, |
| 774 |
) |
| 775 |
|
| 776 |
total_reconstruction = 0 |
| 777 |
for i in range(n_samples): |
| 778 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 779 |
Cs[i], |
| 780 |
Ys[i], |
| 781 |
Cdict, |
| 782 |
Ydict, |
| 783 |
p=None, |
| 784 |
q=None, |
| 785 |
alpha=alpha, |
| 786 |
reg=0.0, |
| 787 |
tol_outer=tol, |
| 788 |
tol_inner=tol, |
| 789 |
max_iter_outer=10, |
| 790 |
max_iter_inner=50, |
| 791 |
) |
| 792 |
total_reconstruction += reconstruction |
| 793 |
|
| 794 |
np.testing.assert_array_less(total_reconstruction, initial_total_reconstruction) |
| 795 |
|
| 796 |
|
| 797 |
Cdictb, Ydictb, log = ot.gromov.fused_gromov_wasserstein_dictionary_learning( |
| 798 |
Csb, |
| 799 |
Ysb, |
| 800 |
D=n_atoms, |
| 801 |
nt=shape, |
| 802 |
ps=None, |
| 803 |
q=None, |
| 804 |
Cdict_init=Cdict_initb, |
| 805 |
Ydict_init=Ydict_initb, |
| 806 |
epochs=epochs, |
| 807 |
batch_size=2 * n_samples, |
| 808 |
learning_rate_C=1.0, |
| 809 |
learning_rate_Y=1.0, |
| 810 |
alpha=alpha, |
| 811 |
reg=0.0, |
| 812 |
tol_outer=tol, |
| 813 |
tol_inner=tol, |
| 814 |
max_iter_outer=10, |
| 815 |
max_iter_inner=50, |
| 816 |
projection=projection, |
| 817 |
use_log=False, |
| 818 |
use_adam_optimizer=use_adam_optimizer, |
| 819 |
verbose=verbose, |
| 820 |
random_state=0, |
| 821 |
) |
| 822 |
|
| 823 |
total_reconstruction_b = 0 |
| 824 |
for i in range(n_samples): |
| 825 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 826 |
Csb[i], |
| 827 |
Ysb[i], |
| 828 |
Cdictb, |
| 829 |
Ydictb, |
| 830 |
p=psb[i], |
| 831 |
q=qb, |
| 832 |
alpha=alpha, |
| 833 |
reg=0.0, |
| 834 |
tol_outer=tol, |
| 835 |
tol_inner=tol, |
| 836 |
max_iter_outer=10, |
| 837 |
max_iter_inner=50, |
| 838 |
) |
| 839 |
total_reconstruction_b += reconstruction |
| 840 |
|
| 841 |
total_reconstruction_b = nx.to_numpy(total_reconstruction_b) |
| 842 |
np.testing.assert_array_less(total_reconstruction_b, initial_total_reconstruction) |
| 843 |
np.testing.assert_allclose(total_reconstruction_b, total_reconstruction, atol=1e-05) |
| 844 |
np.testing.assert_allclose(Cdict, nx.to_numpy(Cdictb), atol=1e-03) |
| 845 |
np.testing.assert_allclose(Ydict, nx.to_numpy(Ydictb), atol=1e-03) |
| 846 |
|
| 847 |
|
| 848 |
Cdict_bis, Ydict_bis, log = ot.gromov.fused_gromov_wasserstein_dictionary_learning( |
| 849 |
Cs, |
| 850 |
Ys, |
| 851 |
D=n_atoms, |
| 852 |
nt=shape, |
| 853 |
ps=None, |
| 854 |
q=None, |
| 855 |
Cdict_init=None, |
| 856 |
Ydict_init=None, |
| 857 |
epochs=epochs, |
| 858 |
batch_size=n_samples, |
| 859 |
learning_rate_C=1.0, |
| 860 |
learning_rate_Y=1.0, |
| 861 |
alpha=alpha, |
| 862 |
reg=0.0, |
| 863 |
tol_outer=tol, |
| 864 |
tol_inner=tol, |
| 865 |
max_iter_outer=10, |
| 866 |
max_iter_inner=50, |
| 867 |
projection=projection, |
| 868 |
use_log=False, |
| 869 |
use_adam_optimizer=use_adam_optimizer, |
| 870 |
verbose=verbose, |
| 871 |
random_state=0, |
| 872 |
) |
| 873 |
|
| 874 |
total_reconstruction_bis = 0 |
| 875 |
for i in range(n_samples): |
| 876 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 877 |
Cs[i], |
| 878 |
Ys[i], |
| 879 |
Cdict_bis, |
| 880 |
Ydict_bis, |
| 881 |
p=ps[i], |
| 882 |
q=q, |
| 883 |
alpha=alpha, |
| 884 |
reg=0.0, |
| 885 |
tol_outer=tol, |
| 886 |
tol_inner=tol, |
| 887 |
max_iter_outer=10, |
| 888 |
max_iter_inner=50, |
| 889 |
) |
| 890 |
total_reconstruction_bis += reconstruction |
| 891 |
|
| 892 |
np.testing.assert_allclose( |
| 893 |
total_reconstruction_bis, total_reconstruction, atol=1e-05 |
| 894 |
) |
| 895 |
|
| 896 |
|
| 897 |
Cdictb_bis, Ydictb_bis, log = ( |
| 898 |
ot.gromov.fused_gromov_wasserstein_dictionary_learning( |
| 899 |
Csb, |
| 900 |
Ysb, |
| 901 |
D=n_atoms, |
| 902 |
nt=shape, |
| 903 |
ps=None, |
| 904 |
q=None, |
| 905 |
Cdict_init=None, |
| 906 |
Ydict_init=None, |
| 907 |
epochs=epochs, |
| 908 |
batch_size=n_samples, |
| 909 |
learning_rate_C=1.0, |
| 910 |
learning_rate_Y=1.0, |
| 911 |
alpha=alpha, |
| 912 |
reg=0.0, |
| 913 |
tol_outer=tol, |
| 914 |
tol_inner=tol, |
| 915 |
max_iter_outer=10, |
| 916 |
max_iter_inner=50, |
| 917 |
projection=projection, |
| 918 |
use_log=False, |
| 919 |
use_adam_optimizer=use_adam_optimizer, |
| 920 |
verbose=verbose, |
| 921 |
random_state=0, |
| 922 |
) |
| 923 |
) |
| 924 |
|
| 925 |
|
| 926 |
total_reconstruction_b_bis = 0 |
| 927 |
for i in range(n_samples): |
| 928 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 929 |
Csb[i], |
| 930 |
Ysb[i], |
| 931 |
Cdictb_bis, |
| 932 |
Ydictb_bis, |
| 933 |
p=psb[i], |
| 934 |
q=qb, |
| 935 |
alpha=alpha, |
| 936 |
reg=0.0, |
| 937 |
tol_outer=tol, |
| 938 |
tol_inner=tol, |
| 939 |
max_iter_outer=10, |
| 940 |
max_iter_inner=50, |
| 941 |
) |
| 942 |
total_reconstruction_b_bis += reconstruction |
| 943 |
|
| 944 |
total_reconstruction_b_bis = nx.to_numpy(total_reconstruction_b_bis) |
| 945 |
np.testing.assert_allclose( |
| 946 |
total_reconstruction_b_bis, total_reconstruction_b, atol=1e-05 |
| 947 |
) |
| 948 |
|
| 949 |
|
| 950 |
use_adam_optimizer = False |
| 951 |
verbose = True |
| 952 |
use_log = True |
| 953 |
|
| 954 |
|
| 955 |
Cdict_bis2, Ydict_bis2, log = ( |
| 956 |
ot.gromov.fused_gromov_wasserstein_dictionary_learning( |
| 957 |
Cs, |
| 958 |
Ys, |
| 959 |
D=n_atoms, |
| 960 |
nt=shape, |
| 961 |
ps=ps, |
| 962 |
q=q, |
| 963 |
Cdict_init=Cdict, |
| 964 |
Ydict_init=Ydict, |
| 965 |
epochs=epochs, |
| 966 |
batch_size=n_samples, |
| 967 |
learning_rate_C=10.0, |
| 968 |
learning_rate_Y=10.0, |
| 969 |
alpha=alpha, |
| 970 |
reg=0.0, |
| 971 |
tol_outer=tol, |
| 972 |
tol_inner=tol, |
| 973 |
max_iter_outer=10, |
| 974 |
max_iter_inner=50, |
| 975 |
projection=projection, |
| 976 |
use_log=use_log, |
| 977 |
use_adam_optimizer=use_adam_optimizer, |
| 978 |
verbose=verbose, |
| 979 |
random_state=0, |
| 980 |
) |
| 981 |
) |
| 982 |
|
| 983 |
total_reconstruction_bis2 = 0 |
| 984 |
for i in range(n_samples): |
| 985 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 986 |
Cs[i], |
| 987 |
Ys[i], |
| 988 |
Cdict_bis2, |
| 989 |
Ydict_bis2, |
| 990 |
p=ps[i], |
| 991 |
q=q, |
| 992 |
alpha=alpha, |
| 993 |
reg=0.0, |
| 994 |
tol_outer=tol, |
| 995 |
tol_inner=tol, |
| 996 |
max_iter_outer=10, |
| 997 |
max_iter_inner=50, |
| 998 |
) |
| 999 |
total_reconstruction_bis2 += reconstruction |
| 1000 |
|
| 1001 |
np.testing.assert_array_less(total_reconstruction_bis2, total_reconstruction) |
| 1002 |
|
| 1003 |
|
| 1004 |
Cdictb_bis2, Ydictb_bis2, log = ( |
| 1005 |
ot.gromov.fused_gromov_wasserstein_dictionary_learning( |
| 1006 |
Csb, |
| 1007 |
Ysb, |
| 1008 |
D=n_atoms, |
| 1009 |
nt=shape, |
| 1010 |
ps=None, |
| 1011 |
q=None, |
| 1012 |
Cdict_init=Cdictb, |
| 1013 |
Ydict_init=Ydictb, |
| 1014 |
epochs=epochs, |
| 1015 |
batch_size=n_samples, |
| 1016 |
learning_rate_C=10.0, |
| 1017 |
learning_rate_Y=10.0, |
| 1018 |
alpha=alpha, |
| 1019 |
reg=0.0, |
| 1020 |
tol_outer=tol, |
| 1021 |
tol_inner=tol, |
| 1022 |
max_iter_outer=10, |
| 1023 |
max_iter_inner=50, |
| 1024 |
projection=projection, |
| 1025 |
use_log=use_log, |
| 1026 |
use_adam_optimizer=use_adam_optimizer, |
| 1027 |
verbose=verbose, |
| 1028 |
random_state=0, |
| 1029 |
) |
| 1030 |
) |
| 1031 |
|
| 1032 |
|
| 1033 |
total_reconstruction_b_bis2 = 0 |
| 1034 |
for i in range(n_samples): |
| 1035 |
_, _, _, _, reconstruction = ot.gromov.fused_gromov_wasserstein_linear_unmixing( |
| 1036 |
Csb[i], |
| 1037 |
Ysb[i], |
| 1038 |
Cdictb_bis2, |
| 1039 |
Ydictb_bis2, |
| 1040 |
p=None, |
| 1041 |
q=None, |
| 1042 |
alpha=alpha, |
| 1043 |
reg=0.0, |
| 1044 |
tol_outer=tol, |
| 1045 |
tol_inner=tol, |
| 1046 |
max_iter_outer=10, |
| 1047 |
max_iter_inner=50, |
| 1048 |
) |
| 1049 |
total_reconstruction_b_bis2 += reconstruction |
| 1050 |
|
| 1051 |
|
| 1052 |
total_reconstruction_b_bis2 = nx.to_numpy(total_reconstruction_b_bis2) |
| 1053 |
np.testing.assert_allclose( |
| 1054 |
total_reconstruction_bis2, total_reconstruction_b_bis2, atol=1e-05 |
| 1055 |
) |