| 1 |
"""Tests for module da on Domain Adaptation""" |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import numpy as np |
| 8 |
from numpy.testing import assert_allclose, assert_equal |
| 9 |
import pytest |
| 10 |
|
| 11 |
import ot |
| 12 |
from ot.datasets import make_data_classif |
| 13 |
from ot.utils import unif |
| 14 |
|
| 15 |
try: |
| 16 |
import sklearn |
| 17 |
|
| 18 |
nosklearn = False |
| 19 |
except ImportError: |
| 20 |
nosklearn = True |
| 21 |
|
| 22 |
try: |
| 23 |
import cvxpy |
| 24 |
|
| 25 |
nocvxpy = False |
| 26 |
except ImportError: |
| 27 |
nocvxpy = True |
| 28 |
|
| 29 |
|
| 30 |
def test_class_jax_tf(): |
| 31 |
from ot.backend import tf |
| 32 |
|
| 33 |
backends = [] |
| 34 |
if tf: |
| 35 |
backends.append(ot.backend.TensorflowBackend()) |
| 36 |
|
| 37 |
for nx in backends: |
| 38 |
ns = 150 |
| 39 |
nt = 200 |
| 40 |
|
| 41 |
Xs, ys = make_data_classif("3gauss", ns) |
| 42 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 43 |
|
| 44 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 45 |
|
| 46 |
otda = ot.da.SinkhornLpl1Transport() |
| 47 |
|
| 48 |
with pytest.raises(TypeError): |
| 49 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 50 |
|
| 51 |
|
| 52 |
@pytest.skip_backend("jax") |
| 53 |
@pytest.skip_backend("tf") |
| 54 |
@pytest.mark.parametrize( |
| 55 |
"class_to_test", |
| 56 |
[ |
| 57 |
ot.da.EMDTransport, |
| 58 |
ot.da.SinkhornTransport, |
| 59 |
ot.da.SinkhornLpl1Transport, |
| 60 |
ot.da.SinkhornL1l2Transport, |
| 61 |
ot.da.SinkhornL1l2Transport, |
| 62 |
], |
| 63 |
) |
| 64 |
def test_log_da(nx, class_to_test): |
| 65 |
ns = 50 |
| 66 |
nt = 50 |
| 67 |
|
| 68 |
Xs, ys = make_data_classif("3gauss", ns) |
| 69 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 70 |
|
| 71 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 72 |
|
| 73 |
otda = class_to_test(log=True) |
| 74 |
|
| 75 |
|
| 76 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 77 |
assert hasattr(otda, "log_") |
| 78 |
|
| 79 |
|
| 80 |
@pytest.skip_backend("tf") |
| 81 |
def test_sinkhorn_lpl1_transport_class(nx): |
| 82 |
"""test_sinkhorn_transport""" |
| 83 |
|
| 84 |
ns = 50 |
| 85 |
nt = 50 |
| 86 |
|
| 87 |
Xs, ys = make_data_classif("3gauss", ns, random_state=42) |
| 88 |
Xt, yt = make_data_classif("3gauss2", nt, random_state=43) |
| 89 |
|
| 90 |
yt_semi = np.copy(yt) |
| 91 |
yt_semi[np.arange(0, nt, 2)] = -1 |
| 92 |
|
| 93 |
Xs, ys, Xt, yt, yt_semi = nx.from_numpy(Xs, ys, Xt, yt, yt_semi) |
| 94 |
|
| 95 |
otda = ot.da.SinkhornLpl1Transport() |
| 96 |
|
| 97 |
|
| 98 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 99 |
assert hasattr(otda, "cost_") |
| 100 |
assert not np.any(np.isnan(nx.to_numpy(otda.cost_))), "cost is finite" |
| 101 |
assert hasattr(otda, "coupling_") |
| 102 |
assert np.all(np.isfinite(nx.to_numpy(otda.coupling_))), "coupling is finite" |
| 103 |
|
| 104 |
|
| 105 |
assert_equal(otda.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 106 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 107 |
|
| 108 |
|
| 109 |
mu_s = unif(ns) |
| 110 |
mu_t = unif(nt) |
| 111 |
assert_allclose( |
| 112 |
nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 113 |
) |
| 114 |
assert_allclose( |
| 115 |
nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
transp_Xs = otda.transform(Xs=Xs) |
| 120 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 121 |
|
| 122 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1, random_state=44)[0]) |
| 123 |
transp_Xs_new = otda.transform(Xs_new) |
| 124 |
|
| 125 |
|
| 126 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 127 |
|
| 128 |
|
| 129 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 130 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 131 |
|
| 132 |
Xt_new = nx.from_numpy(make_data_classif("3gauss2", nt + 1, random_state=45)[0]) |
| 133 |
transp_Xt_new = otda.inverse_transform(Xt=Xt_new) |
| 134 |
|
| 135 |
|
| 136 |
assert_equal(transp_Xt_new.shape, Xt_new.shape) |
| 137 |
|
| 138 |
|
| 139 |
transp_Xs = otda.fit_transform(Xs=Xs, ys=ys, Xt=Xt) |
| 140 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 141 |
|
| 142 |
|
| 143 |
transp_yt = otda.transform_labels(ys) |
| 144 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 145 |
assert_equal(transp_yt.shape[1], len(np.unique(ys))) |
| 146 |
|
| 147 |
|
| 148 |
transp_ys = otda.inverse_transform_labels(yt) |
| 149 |
assert_equal(transp_ys.shape[0], ys.shape[0]) |
| 150 |
assert_equal(transp_ys.shape[1], len(np.unique(yt))) |
| 151 |
|
| 152 |
|
| 153 |
otda_unsup = ot.da.SinkhornLpl1Transport() |
| 154 |
otda_unsup.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 155 |
assert np.all( |
| 156 |
np.isfinite(nx.to_numpy(otda_unsup.coupling_)) |
| 157 |
), "unsup coupling is finite" |
| 158 |
n_unsup = nx.sum(otda_unsup.cost_) |
| 159 |
|
| 160 |
otda_semi = ot.da.SinkhornLpl1Transport() |
| 161 |
otda_semi.fit(Xs=Xs, ys=ys, Xt=Xt, yt=yt_semi) |
| 162 |
assert np.all( |
| 163 |
np.isfinite(nx.to_numpy(otda_semi.coupling_)) |
| 164 |
), "semi coupling is finite" |
| 165 |
assert_equal(otda_semi.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 166 |
n_semisup = nx.sum(otda_semi.cost_) |
| 167 |
|
| 168 |
|
| 169 |
assert np.allclose( |
| 170 |
n_unsup, n_semisup, atol=1e-7 |
| 171 |
), "semisupervised mode is not working" |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
mass_semi = nx.sum(otda_semi.coupling_[otda_semi.cost_ == otda_semi.limit_max]) |
| 176 |
assert mass_semi == 0, "semisupervised mode not working" |
| 177 |
|
| 178 |
|
| 179 |
@pytest.skip_backend("tf") |
| 180 |
def test_sinkhorn_l1l2_transport_class(nx): |
| 181 |
"""test_sinkhorn_transport""" |
| 182 |
|
| 183 |
ns = 50 |
| 184 |
nt = 50 |
| 185 |
|
| 186 |
Xs, ys = make_data_classif("3gauss", ns, random_state=42) |
| 187 |
Xt, yt = make_data_classif("3gauss2", nt, random_state=43) |
| 188 |
|
| 189 |
yt_semi = np.copy(yt) |
| 190 |
yt_semi[np.arange(0, nt, 2)] = -1 |
| 191 |
|
| 192 |
Xs, ys, Xt, yt, yt_semi = nx.from_numpy(Xs, ys, Xt, yt, yt_semi) |
| 193 |
|
| 194 |
otda = ot.da.SinkhornL1l2Transport(max_inner_iter=500) |
| 195 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 196 |
|
| 197 |
|
| 198 |
assert hasattr(otda, "cost_") |
| 199 |
assert hasattr(otda, "coupling_") |
| 200 |
assert hasattr(otda, "log_") |
| 201 |
|
| 202 |
|
| 203 |
assert_equal(otda.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 204 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 205 |
|
| 206 |
|
| 207 |
mu_s = unif(ns) |
| 208 |
mu_t = unif(nt) |
| 209 |
assert_allclose( |
| 210 |
nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 211 |
) |
| 212 |
assert_allclose( |
| 213 |
nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
transp_Xs = otda.transform(Xs=Xs) |
| 218 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 219 |
|
| 220 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1)[0]) |
| 221 |
transp_Xs_new = otda.transform(Xs_new) |
| 222 |
|
| 223 |
|
| 224 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 225 |
|
| 226 |
|
| 227 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 228 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 229 |
|
| 230 |
|
| 231 |
transp_yt = otda.transform_labels(ys) |
| 232 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 233 |
assert_equal(transp_yt.shape[1], len(np.unique(ys))) |
| 234 |
|
| 235 |
|
| 236 |
transp_ys = otda.inverse_transform_labels(yt) |
| 237 |
assert_equal(transp_ys.shape[0], ys.shape[0]) |
| 238 |
assert_equal(transp_ys.shape[1], len(np.unique(yt))) |
| 239 |
|
| 240 |
Xt_new = nx.from_numpy(make_data_classif("3gauss2", nt + 1)[0]) |
| 241 |
transp_Xt_new = otda.inverse_transform(Xt=Xt_new) |
| 242 |
|
| 243 |
|
| 244 |
assert_equal(transp_Xt_new.shape, Xt_new.shape) |
| 245 |
|
| 246 |
|
| 247 |
transp_Xs = otda.fit_transform(Xs=Xs, ys=ys, Xt=Xt) |
| 248 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 249 |
|
| 250 |
|
| 251 |
otda_unsup = ot.da.SinkhornL1l2Transport() |
| 252 |
otda_unsup.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 253 |
n_unsup = nx.sum(otda_unsup.cost_) |
| 254 |
|
| 255 |
otda_semi = ot.da.SinkhornL1l2Transport() |
| 256 |
otda_semi.fit(Xs=Xs, ys=ys, Xt=Xt, yt=yt_semi) |
| 257 |
assert_equal(otda_semi.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 258 |
n_semisup = nx.sum(otda_semi.cost_) |
| 259 |
|
| 260 |
|
| 261 |
assert np.allclose( |
| 262 |
n_unsup, n_semisup, atol=1e-7 |
| 263 |
), "semisupervised mode is not working" |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
mass_semi = nx.sum(otda_semi.coupling_[otda_semi.cost_ == otda_semi.limit_max]) |
| 268 |
mass_semi = otda_semi.coupling_[otda_semi.cost_ == otda_semi.limit_max] |
| 269 |
assert_allclose( |
| 270 |
nx.to_numpy(mass_semi), np.zeros_like(mass_semi), rtol=1e-9, atol=1e-9 |
| 271 |
) |
| 272 |
|
| 273 |
|
| 274 |
otda = ot.da.SinkhornL1l2Transport(log=True) |
| 275 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 276 |
assert len(otda.log_.keys()) != 0 |
| 277 |
|
| 278 |
|
| 279 |
@pytest.skip_backend("jax") |
| 280 |
@pytest.skip_backend("tf") |
| 281 |
def test_sinkhorn_transport_class(nx): |
| 282 |
"""test_sinkhorn_transport""" |
| 283 |
|
| 284 |
ns = 50 |
| 285 |
nt = 50 |
| 286 |
|
| 287 |
Xs, ys = make_data_classif("3gauss", ns) |
| 288 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 289 |
|
| 290 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 291 |
|
| 292 |
otda = ot.da.SinkhornTransport() |
| 293 |
|
| 294 |
|
| 295 |
otda.fit(Xs=Xs, Xt=Xt) |
| 296 |
assert hasattr(otda, "cost_") |
| 297 |
assert hasattr(otda, "coupling_") |
| 298 |
assert hasattr(otda, "log_") |
| 299 |
|
| 300 |
|
| 301 |
assert_equal(otda.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 302 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 303 |
|
| 304 |
|
| 305 |
mu_s = unif(ns) |
| 306 |
mu_t = unif(nt) |
| 307 |
assert_allclose( |
| 308 |
nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 309 |
) |
| 310 |
assert_allclose( |
| 311 |
nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 312 |
) |
| 313 |
|
| 314 |
|
| 315 |
transp_Xs = otda.transform(Xs=Xs) |
| 316 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 317 |
|
| 318 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1)[0]) |
| 319 |
transp_Xs_new = otda.transform(Xs_new) |
| 320 |
|
| 321 |
|
| 322 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 323 |
|
| 324 |
|
| 325 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 326 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 327 |
|
| 328 |
|
| 329 |
transp_yt = otda.transform_labels(ys) |
| 330 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 331 |
assert_equal(transp_yt.shape[1], len(np.unique(ys))) |
| 332 |
|
| 333 |
|
| 334 |
transp_ys = otda.inverse_transform_labels(yt) |
| 335 |
assert_equal(transp_ys.shape[0], ys.shape[0]) |
| 336 |
assert_equal(transp_ys.shape[1], len(np.unique(yt))) |
| 337 |
|
| 338 |
Xt_new = nx.from_numpy(make_data_classif("3gauss2", nt + 1)[0]) |
| 339 |
transp_Xt_new = otda.inverse_transform(Xt=Xt_new) |
| 340 |
|
| 341 |
|
| 342 |
assert_equal(transp_Xt_new.shape, Xt_new.shape) |
| 343 |
|
| 344 |
|
| 345 |
transp_Xs = otda.fit_transform(Xs=Xs, Xt=Xt) |
| 346 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 347 |
|
| 348 |
|
| 349 |
otda_unsup = ot.da.SinkhornTransport() |
| 350 |
otda_unsup.fit(Xs=Xs, Xt=Xt) |
| 351 |
n_unsup = nx.sum(otda_unsup.cost_) |
| 352 |
|
| 353 |
otda_semi = ot.da.SinkhornTransport() |
| 354 |
otda_semi.fit(Xs=Xs, ys=ys, Xt=Xt, yt=yt) |
| 355 |
assert_equal(otda_semi.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 356 |
n_semisup = nx.sum(otda_semi.cost_) |
| 357 |
|
| 358 |
|
| 359 |
assert np.allclose( |
| 360 |
n_unsup, n_semisup, atol=1e-7 |
| 361 |
), "semisupervised mode is not working" |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
mass_semi = nx.sum(otda_semi.coupling_[otda_semi.cost_ == otda_semi.limit_max]) |
| 366 |
assert mass_semi == 0, "semisupervised mode not working" |
| 367 |
|
| 368 |
|
| 369 |
otda = ot.da.SinkhornTransport(log=True) |
| 370 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 371 |
assert len(otda.log_.keys()) != 0 |
| 372 |
|
| 373 |
|
| 374 |
otda = ot.da.SinkhornTransport(out_of_sample_map="ferradans") |
| 375 |
transp_Xs = otda.fit_transform(Xs=Xs, Xt=Xt) |
| 376 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 377 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 378 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 379 |
|
| 380 |
|
| 381 |
otda = ot.da.SinkhornTransport(out_of_sample_map="continuous", method="sinkhorn") |
| 382 |
transp_Xs2 = otda.fit_transform(Xs=Xs, Xt=Xt) |
| 383 |
assert_equal(transp_Xs2.shape, Xs.shape) |
| 384 |
transp_Xt2 = otda.inverse_transform(Xt=Xt) |
| 385 |
assert_equal(transp_Xt2.shape, Xt.shape) |
| 386 |
|
| 387 |
np.testing.assert_almost_equal( |
| 388 |
nx.to_numpy(transp_Xs), nx.to_numpy(transp_Xs2), decimal=5 |
| 389 |
) |
| 390 |
np.testing.assert_almost_equal( |
| 391 |
nx.to_numpy(transp_Xt), nx.to_numpy(transp_Xt2), decimal=5 |
| 392 |
) |
| 393 |
|
| 394 |
with pytest.raises(ValueError): |
| 395 |
otda = ot.da.SinkhornTransport(out_of_sample_map="unknown") |
| 396 |
|
| 397 |
|
| 398 |
@pytest.skip_backend("jax") |
| 399 |
@pytest.skip_backend("tf") |
| 400 |
def test_unbalanced_sinkhorn_transport_class(nx): |
| 401 |
"""test_sinkhorn_transport""" |
| 402 |
|
| 403 |
ns = 50 |
| 404 |
nt = 50 |
| 405 |
|
| 406 |
Xs, ys = make_data_classif("3gauss", ns) |
| 407 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 408 |
|
| 409 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 410 |
|
| 411 |
for log in [True, False]: |
| 412 |
otda = ot.da.UnbalancedSinkhornTransport(log=log) |
| 413 |
|
| 414 |
|
| 415 |
otda.fit(Xs=Xs, Xt=Xt) |
| 416 |
assert hasattr(otda, "cost_") |
| 417 |
assert hasattr(otda, "coupling_") |
| 418 |
assert hasattr(otda, "log_") |
| 419 |
|
| 420 |
|
| 421 |
assert_equal(otda.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 422 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 423 |
assert not np.any(np.isnan(nx.to_numpy(otda.cost_))), "cost is finite" |
| 424 |
|
| 425 |
|
| 426 |
assert np.all(np.isfinite(nx.to_numpy(otda.coupling_))), "coupling is finite" |
| 427 |
|
| 428 |
|
| 429 |
transp_Xs = otda.transform(Xs=Xs) |
| 430 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 431 |
|
| 432 |
|
| 433 |
transp_yt = otda.transform_labels(ys) |
| 434 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 435 |
assert_equal(transp_yt.shape[1], len(np.unique(ys))) |
| 436 |
|
| 437 |
|
| 438 |
transp_ys = otda.inverse_transform_labels(yt) |
| 439 |
assert_equal(transp_ys.shape[0], ys.shape[0]) |
| 440 |
assert_equal(transp_ys.shape[1], len(np.unique(yt))) |
| 441 |
|
| 442 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1)[0]) |
| 443 |
transp_Xs_new = otda.transform(Xs_new) |
| 444 |
|
| 445 |
|
| 446 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 447 |
|
| 448 |
|
| 449 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 450 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 451 |
|
| 452 |
Xt_new = nx.from_numpy(make_data_classif("3gauss2", nt + 1)[0]) |
| 453 |
transp_Xt_new = otda.inverse_transform(Xt=Xt_new) |
| 454 |
|
| 455 |
|
| 456 |
assert_equal(transp_Xt_new.shape, Xt_new.shape) |
| 457 |
|
| 458 |
|
| 459 |
transp_Xs = otda.fit_transform(Xs=Xs, Xt=Xt) |
| 460 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 461 |
|
| 462 |
|
| 463 |
otda_unsup = ot.da.SinkhornTransport() |
| 464 |
otda_unsup.fit(Xs=Xs, Xt=Xt) |
| 465 |
assert not np.any(np.isnan(nx.to_numpy(otda_unsup.cost_))), "cost is finite" |
| 466 |
n_unsup = nx.sum(otda_unsup.cost_) |
| 467 |
|
| 468 |
otda_semi = ot.da.SinkhornTransport() |
| 469 |
otda_semi.fit(Xs=Xs, ys=ys, Xt=Xt, yt=yt) |
| 470 |
assert not np.any(np.isnan(nx.to_numpy(otda_semi.cost_))), "cost is finite" |
| 471 |
assert_equal(otda_semi.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 472 |
n_semisup = nx.sum(otda_semi.cost_) |
| 473 |
|
| 474 |
|
| 475 |
assert np.allclose( |
| 476 |
n_unsup, n_semisup, atol=1e-7 |
| 477 |
), "semisupervised mode is not working" |
| 478 |
|
| 479 |
|
| 480 |
otda = ot.da.SinkhornTransport(log=True) |
| 481 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 482 |
assert not np.any(np.isnan(nx.to_numpy(otda.cost_))), "cost is finite" |
| 483 |
assert len(otda.log_.keys()) != 0 |
| 484 |
|
| 485 |
|
| 486 |
@pytest.skip_backend("jax") |
| 487 |
@pytest.skip_backend("tf") |
| 488 |
def test_emd_transport_class(nx): |
| 489 |
"""test_sinkhorn_transport""" |
| 490 |
|
| 491 |
ns = 50 |
| 492 |
nt = 50 |
| 493 |
|
| 494 |
Xs, ys = make_data_classif("3gauss", ns) |
| 495 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 496 |
|
| 497 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 498 |
|
| 499 |
otda = ot.da.EMDTransport() |
| 500 |
|
| 501 |
|
| 502 |
otda.fit(Xs=Xs, Xt=Xt) |
| 503 |
assert hasattr(otda, "cost_") |
| 504 |
assert hasattr(otda, "coupling_") |
| 505 |
|
| 506 |
|
| 507 |
assert_equal(otda.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 508 |
assert not np.any(np.isnan(nx.to_numpy(otda.cost_))), "cost is finite" |
| 509 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 510 |
assert np.all(np.isfinite(nx.to_numpy(otda.coupling_))), "coupling is finite" |
| 511 |
|
| 512 |
|
| 513 |
mu_s = unif(ns) |
| 514 |
mu_t = unif(nt) |
| 515 |
assert_allclose( |
| 516 |
nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 517 |
) |
| 518 |
assert_allclose( |
| 519 |
nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 520 |
) |
| 521 |
|
| 522 |
|
| 523 |
transp_Xs = otda.transform(Xs=Xs) |
| 524 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 525 |
|
| 526 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1)[0]) |
| 527 |
transp_Xs_new = otda.transform(Xs_new) |
| 528 |
|
| 529 |
|
| 530 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 531 |
|
| 532 |
|
| 533 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 534 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 535 |
|
| 536 |
|
| 537 |
transp_yt = otda.transform_labels(ys) |
| 538 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 539 |
assert_equal(transp_yt.shape[1], len(np.unique(ys))) |
| 540 |
|
| 541 |
|
| 542 |
transp_ys = otda.inverse_transform_labels(yt) |
| 543 |
assert_equal(transp_ys.shape[0], ys.shape[0]) |
| 544 |
assert_equal(transp_ys.shape[1], len(np.unique(yt))) |
| 545 |
|
| 546 |
Xt_new = nx.from_numpy(make_data_classif("3gauss2", nt + 1)[0]) |
| 547 |
transp_Xt_new = otda.inverse_transform(Xt=Xt_new) |
| 548 |
|
| 549 |
|
| 550 |
assert_equal(transp_Xt_new.shape, Xt_new.shape) |
| 551 |
|
| 552 |
|
| 553 |
transp_Xs = otda.fit_transform(Xs=Xs, Xt=Xt) |
| 554 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 555 |
|
| 556 |
|
| 557 |
otda_unsup = ot.da.EMDTransport() |
| 558 |
otda_unsup.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 559 |
assert_equal(otda_unsup.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 560 |
assert not np.any(np.isnan(nx.to_numpy(otda_unsup.cost_))), "cost is finite" |
| 561 |
assert_equal(otda_unsup.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 562 |
assert np.all(np.isfinite(nx.to_numpy(otda_unsup.coupling_))), "coupling is finite" |
| 563 |
n_unsup = nx.sum(otda_unsup.cost_) |
| 564 |
|
| 565 |
otda_semi = ot.da.EMDTransport() |
| 566 |
otda_semi.fit(Xs=Xs, ys=ys, Xt=Xt, yt=yt) |
| 567 |
assert_equal(otda_semi.cost_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 568 |
assert not np.any(np.isnan(nx.to_numpy(otda_semi.cost_))), "cost is finite" |
| 569 |
assert_equal(otda_semi.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 570 |
assert np.all(np.isfinite(nx.to_numpy(otda_semi.coupling_))), "coupling is finite" |
| 571 |
n_semisup = nx.sum(otda_semi.cost_) |
| 572 |
|
| 573 |
|
| 574 |
assert np.allclose( |
| 575 |
n_unsup, n_semisup, atol=1e-7 |
| 576 |
), "semisupervised mode is not working" |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
mass_semi = nx.sum(otda_semi.coupling_[otda_semi.cost_ == otda_semi.limit_max]) |
| 581 |
mass_semi = otda_semi.coupling_[otda_semi.cost_ == otda_semi.limit_max] |
| 582 |
|
| 583 |
|
| 584 |
assert_allclose( |
| 585 |
nx.to_numpy(mass_semi), np.zeros(list(mass_semi.shape)), rtol=1e-2, atol=1e-2 |
| 586 |
) |
| 587 |
|
| 588 |
|
| 589 |
@pytest.skip_backend("jax") |
| 590 |
@pytest.skip_backend("tf") |
| 591 |
@pytest.mark.parametrize("kernel", ["linear", "gaussian"]) |
| 592 |
@pytest.mark.parametrize("bias", ["unbiased", "biased"]) |
| 593 |
def test_mapping_transport_class(nx, kernel, bias): |
| 594 |
"""test_mapping_transport""" |
| 595 |
|
| 596 |
ns = 20 |
| 597 |
nt = 30 |
| 598 |
|
| 599 |
Xs, ys = make_data_classif("3gauss", ns) |
| 600 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 601 |
Xs_new, _ = make_data_classif("3gauss", ns + 1) |
| 602 |
|
| 603 |
Xs, Xt, Xs_new = nx.from_numpy(Xs, Xt, Xs_new) |
| 604 |
|
| 605 |
|
| 606 |
bias = bias == "biased" |
| 607 |
otda = ot.da.MappingTransport(kernel=kernel, bias=bias) |
| 608 |
otda.fit(Xs=Xs, Xt=Xt) |
| 609 |
assert hasattr(otda, "coupling_") |
| 610 |
assert hasattr(otda, "mapping_") |
| 611 |
assert hasattr(otda, "log_") |
| 612 |
|
| 613 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 614 |
S = Xs.shape[0] if kernel == "gaussian" else Xs.shape[1] |
| 615 |
if bias: |
| 616 |
S += 1 |
| 617 |
assert_equal(otda.mapping_.shape, ((S, Xt.shape[1]))) |
| 618 |
|
| 619 |
|
| 620 |
mu_s = unif(ns) |
| 621 |
mu_t = unif(nt) |
| 622 |
assert_allclose( |
| 623 |
nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 624 |
) |
| 625 |
assert_allclose( |
| 626 |
nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 627 |
) |
| 628 |
|
| 629 |
|
| 630 |
transp_Xs = otda.transform(Xs=Xs) |
| 631 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 632 |
|
| 633 |
transp_Xs_new = otda.transform(Xs_new) |
| 634 |
|
| 635 |
|
| 636 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 637 |
|
| 638 |
|
| 639 |
otda = ot.da.MappingTransport(kernel=kernel, bias=bias, log=True) |
| 640 |
otda.fit(Xs=Xs, Xt=Xt) |
| 641 |
assert len(otda.log_.keys()) != 0 |
| 642 |
|
| 643 |
|
| 644 |
@pytest.skip_backend("jax") |
| 645 |
@pytest.skip_backend("tf") |
| 646 |
def test_mapping_transport_class_specific_seed(nx): |
| 647 |
|
| 648 |
ns = 20 |
| 649 |
nt = 30 |
| 650 |
rng = np.random.RandomState(39) |
| 651 |
Xs, ys = make_data_classif("3gauss", ns, random_state=rng) |
| 652 |
Xt, yt = make_data_classif("3gauss2", nt, random_state=rng) |
| 653 |
otda = ot.da.MappingTransport(kernel="gaussian", bias=False) |
| 654 |
otda.fit(Xs=nx.from_numpy(Xs), Xt=nx.from_numpy(Xt)) |
| 655 |
|
| 656 |
|
| 657 |
@pytest.skip_backend("jax") |
| 658 |
@pytest.skip_backend("tf") |
| 659 |
def test_linear_mapping_class(nx): |
| 660 |
ns = 50 |
| 661 |
nt = 50 |
| 662 |
|
| 663 |
Xs, ys = make_data_classif("3gauss", ns) |
| 664 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 665 |
|
| 666 |
Xsb, Xtb = nx.from_numpy(Xs, Xt) |
| 667 |
|
| 668 |
for log in [True, False]: |
| 669 |
otmap = ot.da.LinearTransport(log=log) |
| 670 |
|
| 671 |
otmap.fit(Xs=Xsb, Xt=Xtb) |
| 672 |
assert hasattr(otmap, "A_") |
| 673 |
assert hasattr(otmap, "B_") |
| 674 |
assert hasattr(otmap, "A1_") |
| 675 |
assert hasattr(otmap, "B1_") |
| 676 |
|
| 677 |
Xst = nx.to_numpy(otmap.transform(Xs=Xsb)) |
| 678 |
|
| 679 |
Ct = np.cov(Xt.T) |
| 680 |
Cst = np.cov(Xst.T) |
| 681 |
|
| 682 |
np.testing.assert_allclose(Ct, Cst, rtol=1e-2, atol=1e-2) |
| 683 |
|
| 684 |
Xts = nx.to_numpy(otmap.inverse_transform(Xt=Xtb)) |
| 685 |
|
| 686 |
Cs = np.cov(Xs.T) |
| 687 |
Cts = np.cov(Xts.T) |
| 688 |
|
| 689 |
np.testing.assert_allclose(Cs, Cts, rtol=1e-2, atol=1e-2) |
| 690 |
|
| 691 |
|
| 692 |
@pytest.skip_backend("jax") |
| 693 |
@pytest.skip_backend("tf") |
| 694 |
def test_linear_gw_mapping_class(nx): |
| 695 |
ns = 50 |
| 696 |
nt = 50 |
| 697 |
|
| 698 |
Xs, ys = make_data_classif("3gauss", ns) |
| 699 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 700 |
|
| 701 |
Xsb, Xtb = nx.from_numpy(Xs, Xt) |
| 702 |
|
| 703 |
for log in [True, False]: |
| 704 |
otmap = ot.da.LinearGWTransport(log=log) |
| 705 |
|
| 706 |
otmap.fit(Xs=Xsb, Xt=Xtb) |
| 707 |
assert hasattr(otmap, "A_") |
| 708 |
assert hasattr(otmap, "B_") |
| 709 |
assert hasattr(otmap, "A1_") |
| 710 |
assert hasattr(otmap, "B1_") |
| 711 |
|
| 712 |
Xst = nx.to_numpy(otmap.transform(Xs=Xsb)) |
| 713 |
|
| 714 |
Ct = np.cov(Xt.T) |
| 715 |
Cst = np.cov(Xst.T) |
| 716 |
|
| 717 |
np.testing.assert_allclose(Ct, Cst, rtol=1e-2, atol=1e-2) |
| 718 |
|
| 719 |
|
| 720 |
@pytest.skip_backend("jax") |
| 721 |
@pytest.skip_backend("tf") |
| 722 |
def test_jcpot_transport_class(nx): |
| 723 |
"""test_jcpot_transport""" |
| 724 |
|
| 725 |
ns1 = 50 |
| 726 |
ns2 = 50 |
| 727 |
nt = 50 |
| 728 |
|
| 729 |
Xs1, ys1 = make_data_classif("3gauss", ns1) |
| 730 |
Xs2, ys2 = make_data_classif("3gauss", ns2) |
| 731 |
|
| 732 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 733 |
|
| 734 |
Xs1, ys1, Xs2, ys2, Xt, yt = nx.from_numpy(Xs1, ys1, Xs2, ys2, Xt, yt) |
| 735 |
|
| 736 |
Xs = [Xs1, Xs2] |
| 737 |
ys = [ys1, ys2] |
| 738 |
|
| 739 |
for log in [True, False]: |
| 740 |
otda = ot.da.JCPOTTransport( |
| 741 |
reg_e=1, max_iter=10000, tol=1e-9, verbose=True, log=log |
| 742 |
) |
| 743 |
|
| 744 |
|
| 745 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 746 |
|
| 747 |
assert hasattr(otda, "coupling_") |
| 748 |
assert hasattr(otda, "proportions_") |
| 749 |
assert hasattr(otda, "log_") |
| 750 |
|
| 751 |
|
| 752 |
for i, xs in enumerate(Xs): |
| 753 |
assert_equal(otda.coupling_[i].shape, ((xs.shape[0], Xt.shape[0]))) |
| 754 |
|
| 755 |
|
| 756 |
mu_t = unif(nt) |
| 757 |
|
| 758 |
for i in range(len(Xs)): |
| 759 |
|
| 760 |
assert_allclose( |
| 761 |
nx.to_numpy(nx.sum(otda.coupling_[i], axis=0)), |
| 762 |
mu_t, |
| 763 |
rtol=1e-3, |
| 764 |
atol=1e-3, |
| 765 |
) |
| 766 |
|
| 767 |
if log: |
| 768 |
|
| 769 |
|
| 770 |
assert_allclose( |
| 771 |
nx.to_numpy( |
| 772 |
nx.dot(otda.log_["D1"][i], nx.sum(otda.coupling_[i], axis=1)) |
| 773 |
), |
| 774 |
nx.to_numpy(otda.proportions_), |
| 775 |
rtol=1e-3, |
| 776 |
atol=1e-3, |
| 777 |
) |
| 778 |
|
| 779 |
|
| 780 |
transp_Xs = otda.transform(Xs=Xs) |
| 781 |
[assert_equal(x.shape, y.shape) for x, y in zip(transp_Xs, Xs)] |
| 782 |
|
| 783 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns1 + 1)[0]) |
| 784 |
transp_Xs_new = otda.transform(Xs_new) |
| 785 |
|
| 786 |
|
| 787 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 788 |
|
| 789 |
|
| 790 |
transp_yt = otda.transform_labels(ys) |
| 791 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 792 |
assert_equal(transp_yt.shape[1], len(np.unique(nx.to_numpy(*ys)))) |
| 793 |
|
| 794 |
|
| 795 |
transp_ys = otda.inverse_transform_labels(yt) |
| 796 |
for x, y in zip(transp_ys, ys): |
| 797 |
assert_equal(x.shape[0], y.shape[0]) |
| 798 |
assert_equal(x.shape[1], len(np.unique(nx.to_numpy(y)))) |
| 799 |
|
| 800 |
|
| 801 |
def test_jcpot_barycenter(nx): |
| 802 |
"""test_jcpot_barycenter""" |
| 803 |
|
| 804 |
ns1 = 50 |
| 805 |
ns2 = 50 |
| 806 |
nt = 50 |
| 807 |
|
| 808 |
sigma = 0.1 |
| 809 |
|
| 810 |
ps1 = 0.2 |
| 811 |
ps2 = 0.9 |
| 812 |
pt = 0.4 |
| 813 |
|
| 814 |
Xs1, ys1 = make_data_classif("2gauss_prop", ns1, nz=sigma, p=ps1) |
| 815 |
Xs2, ys2 = make_data_classif("2gauss_prop", ns2, nz=sigma, p=ps2) |
| 816 |
Xt, _ = make_data_classif("2gauss_prop", nt, nz=sigma, p=pt) |
| 817 |
|
| 818 |
Xs1b, ys1b, Xs2b, ys2b, Xtb = nx.from_numpy(Xs1, ys1, Xs2, ys2, Xt) |
| 819 |
|
| 820 |
Xsb = [Xs1b, Xs2b] |
| 821 |
ysb = [ys1b, ys2b] |
| 822 |
|
| 823 |
prop = ot.bregman.jcpot_barycenter( |
| 824 |
Xsb, |
| 825 |
ysb, |
| 826 |
Xtb, |
| 827 |
reg=0.5, |
| 828 |
metric="sqeuclidean", |
| 829 |
numItermax=10000, |
| 830 |
stopThr=1e-9, |
| 831 |
verbose=False, |
| 832 |
log=False, |
| 833 |
) |
| 834 |
|
| 835 |
np.testing.assert_allclose(nx.to_numpy(prop), [1 - pt, pt], rtol=1e-3, atol=1e-3) |
| 836 |
|
| 837 |
|
| 838 |
@pytest.mark.skipif(nosklearn, reason="No sklearn available") |
| 839 |
@pytest.skip_backend("jax") |
| 840 |
@pytest.skip_backend("tf") |
| 841 |
def test_emd_laplace_class(nx): |
| 842 |
"""test_emd_laplace_transport""" |
| 843 |
ns = 50 |
| 844 |
nt = 50 |
| 845 |
|
| 846 |
Xs, ys = make_data_classif("3gauss", ns) |
| 847 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 848 |
|
| 849 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 850 |
|
| 851 |
for log in [True, False]: |
| 852 |
otda = ot.da.EMDLaplaceTransport( |
| 853 |
reg_lap=0.01, max_iter=1000, tol=1e-9, verbose=False, log=log |
| 854 |
) |
| 855 |
|
| 856 |
|
| 857 |
otda.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 858 |
|
| 859 |
assert hasattr(otda, "coupling_") |
| 860 |
assert hasattr(otda, "log_") |
| 861 |
|
| 862 |
|
| 863 |
assert_equal(otda.coupling_.shape, ((Xs.shape[0], Xt.shape[0]))) |
| 864 |
|
| 865 |
|
| 866 |
mu_s = unif(ns) |
| 867 |
mu_t = unif(nt) |
| 868 |
|
| 869 |
assert_allclose( |
| 870 |
nx.to_numpy(nx.sum(otda.coupling_, axis=0)), mu_t, rtol=1e-3, atol=1e-3 |
| 871 |
) |
| 872 |
assert_allclose( |
| 873 |
nx.to_numpy(nx.sum(otda.coupling_, axis=1)), mu_s, rtol=1e-3, atol=1e-3 |
| 874 |
) |
| 875 |
|
| 876 |
|
| 877 |
transp_Xs = otda.transform(Xs=Xs) |
| 878 |
[assert_equal(x.shape, y.shape) for x, y in zip(transp_Xs, Xs)] |
| 879 |
|
| 880 |
Xs_new = nx.from_numpy(make_data_classif("3gauss", ns + 1)[0]) |
| 881 |
transp_Xs_new = otda.transform(Xs_new) |
| 882 |
|
| 883 |
|
| 884 |
assert_equal(transp_Xs_new.shape, Xs_new.shape) |
| 885 |
|
| 886 |
|
| 887 |
transp_Xt = otda.inverse_transform(Xt=Xt) |
| 888 |
assert_equal(transp_Xt.shape, Xt.shape) |
| 889 |
|
| 890 |
Xt_new = nx.from_numpy(make_data_classif("3gauss2", nt + 1)[0]) |
| 891 |
transp_Xt_new = otda.inverse_transform(Xt=Xt_new) |
| 892 |
|
| 893 |
|
| 894 |
assert_equal(transp_Xt_new.shape, Xt_new.shape) |
| 895 |
|
| 896 |
|
| 897 |
transp_Xs = otda.fit_transform(Xs=Xs, Xt=Xt) |
| 898 |
assert_equal(transp_Xs.shape, Xs.shape) |
| 899 |
|
| 900 |
|
| 901 |
transp_yt = otda.transform_labels(ys) |
| 902 |
assert_equal(transp_yt.shape[0], yt.shape[0]) |
| 903 |
assert_equal(transp_yt.shape[1], len(np.unique(nx.to_numpy(ys)))) |
| 904 |
|
| 905 |
|
| 906 |
transp_ys = otda.inverse_transform_labels(yt) |
| 907 |
assert_equal(transp_ys.shape[0], ys.shape[0]) |
| 908 |
assert_equal(transp_ys.shape[1], len(np.unique(nx.to_numpy(yt)))) |
| 909 |
|
| 910 |
|
| 911 |
@pytest.mark.skipif(nocvxpy, reason="No CVXPY available") |
| 912 |
def test_nearest_brenier_potential(nx): |
| 913 |
X = nx.ones((2, 2)) |
| 914 |
for ssnb in [ |
| 915 |
ot.da.NearestBrenierPotential(log=True), |
| 916 |
ot.da.NearestBrenierPotential(log=False), |
| 917 |
]: |
| 918 |
ssnb.fit(Xs=X, Xt=X) |
| 919 |
G_lu = ssnb.transform(Xs=X) |
| 920 |
|
| 921 |
np.testing.assert_almost_equal(nx.to_numpy(G_lu[0]), nx.to_numpy(X)) |
| 922 |
np.testing.assert_almost_equal(nx.to_numpy(G_lu[1]), nx.to_numpy(X)) |
| 923 |
|
| 924 |
|
| 925 |
@pytest.mark.skipif(nosklearn, reason="No sklearn available") |
| 926 |
@pytest.skip_backend("jax") |
| 927 |
@pytest.skip_backend("tf") |
| 928 |
def test_emd_laplace(nx): |
| 929 |
"""Complements :code:`test_emd_laplace_class` for uncovered options in :code:`emd_laplace`""" |
| 930 |
ns = 50 |
| 931 |
nt = 50 |
| 932 |
|
| 933 |
Xs, ys = make_data_classif("3gauss", ns) |
| 934 |
Xt, yt = make_data_classif("3gauss2", nt) |
| 935 |
|
| 936 |
Xs, ys, Xt, yt = nx.from_numpy(Xs, ys, Xt, yt) |
| 937 |
M = ot.dist(Xs, Xt) |
| 938 |
with pytest.raises(ValueError): |
| 939 |
ot.da.emd_laplace( |
| 940 |
ot.unif(ns), ot.unif(nt), Xs, Xt, M, sim_param=["INVALID", "INPUT", 2] |
| 941 |
) |
| 942 |
with pytest.raises(ValueError): |
| 943 |
ot.da.emd_laplace( |
| 944 |
ot.unif(ns), ot.unif(nt), Xs, Xt, M, sim=["INVALID", "INPUT", 2] |
| 945 |
) |
| 946 |
|
| 947 |
|
| 948 |
coupling = ot.da.emd_laplace( |
| 949 |
ot.unif(ns, type_as=Xs), |
| 950 |
ot.unif(nt, type_as=Xs), |
| 951 |
Xs, |
| 952 |
Xt, |
| 953 |
M, |
| 954 |
sim="gauss", |
| 955 |
reg="disp", |
| 956 |
) |
| 957 |
|
| 958 |
assert_allclose( |
| 959 |
nx.to_numpy(nx.sum(coupling, axis=0)), unif(nt), rtol=1e-3, atol=1e-3 |
| 960 |
) |
| 961 |
assert_allclose( |
| 962 |
nx.to_numpy(nx.sum(coupling, axis=1)), unif(ns), rtol=1e-3, atol=1e-3 |
| 963 |
) |
| 964 |
|
| 965 |
|
| 966 |
@pytest.skip_backend("jax") |
| 967 |
@pytest.skip_backend("tf") |
| 968 |
def test_sinkhorn_l1l2_gl_cost_vectorized(nx): |
| 969 |
n_samples, n_labels = 150, 3 |
| 970 |
rng = np.random.RandomState(42) |
| 971 |
G = rng.rand(n_samples, n_samples) |
| 972 |
labels_a = rng.randint(n_labels, size=(n_samples,)) |
| 973 |
G, labels_a = nx.from_numpy(G), nx.from_numpy(labels_a) |
| 974 |
|
| 975 |
|
| 976 |
lstlab = nx.unique(labels_a) |
| 977 |
|
| 978 |
def f(G): |
| 979 |
res = 0 |
| 980 |
for i in range(G.shape[1]): |
| 981 |
for lab in lstlab: |
| 982 |
temp = G[labels_a == lab, i] |
| 983 |
res += nx.norm(temp) |
| 984 |
return res |
| 985 |
|
| 986 |
def df(G): |
| 987 |
W = nx.zeros(G.shape, type_as=G) |
| 988 |
for i in range(G.shape[1]): |
| 989 |
for lab in lstlab: |
| 990 |
temp = G[labels_a == lab, i] |
| 991 |
n = nx.norm(temp) |
| 992 |
if n: |
| 993 |
W[labels_a == lab, i] = temp / n |
| 994 |
return W |
| 995 |
|
| 996 |
|
| 997 |
labels_u, labels_idx = nx.unique(labels_a, return_inverse=True) |
| 998 |
n_labels = labels_u.shape[0] |
| 999 |
unroll_labels_idx = nx.eye(n_labels, type_as=labels_u)[None, labels_idx] |
| 1000 |
|
| 1001 |
def f2(G): |
| 1002 |
G_split = nx.repeat(G.T[:, :, None], n_labels, axis=2) |
| 1003 |
return nx.sum(nx.norm(G_split * unroll_labels_idx, axis=1)) |
| 1004 |
|
| 1005 |
def df2(G): |
| 1006 |
G_split = nx.repeat(G.T[:, :, None], n_labels, axis=2) * unroll_labels_idx |
| 1007 |
W = nx.norm(G_split * unroll_labels_idx, axis=1, keepdims=True) |
| 1008 |
G_norm = G_split / nx.clip(W, 1e-12, None) |
| 1009 |
return nx.sum(G_norm, axis=2).T |
| 1010 |
|
| 1011 |
assert np.allclose(f(G), f2(G)) |
| 1012 |
assert np.allclose(df(G), df2(G)) |
| 1013 |
|
| 1014 |
|
| 1015 |
@pytest.skip_backend("jax") |
| 1016 |
@pytest.skip_backend("tf") |
| 1017 |
def test_sinkhorn_lpl1_vectorization(nx): |
| 1018 |
n_samples, n_labels = 150, 3 |
| 1019 |
rng = np.random.RandomState(42) |
| 1020 |
M = rng.rand(n_samples, n_samples) |
| 1021 |
labels_a = rng.randint(n_labels, size=(n_samples,)) |
| 1022 |
M, labels_a = nx.from_numpy(M), nx.from_numpy(labels_a) |
| 1023 |
|
| 1024 |
|
| 1025 |
p, epsilon = 0.5, 1e-3 |
| 1026 |
T = nx.from_numpy(rng.rand(n_samples, n_samples)) |
| 1027 |
|
| 1028 |
def unvectorized(transp): |
| 1029 |
indices_labels = [] |
| 1030 |
classes = nx.unique(labels_a) |
| 1031 |
for c in classes: |
| 1032 |
(idxc,) = nx.where(labels_a == c) |
| 1033 |
indices_labels.append(idxc) |
| 1034 |
W = nx.ones(M.shape, type_as=M) |
| 1035 |
for i, c in enumerate(classes): |
| 1036 |
majs = nx.sum(transp[indices_labels[i]], axis=0) |
| 1037 |
majs = p * ((majs + epsilon) ** (p - 1)) |
| 1038 |
W[indices_labels[i]] = majs |
| 1039 |
return W |
| 1040 |
|
| 1041 |
def vectorized(transp): |
| 1042 |
labels_u, labels_idx = nx.unique(labels_a, return_inverse=True) |
| 1043 |
n_labels = labels_u.shape[0] |
| 1044 |
unroll_labels_idx = nx.eye(n_labels, type_as=transp)[labels_idx] |
| 1045 |
W = ( |
| 1046 |
nx.repeat(transp.T[:, :, None], n_labels, axis=2) |
| 1047 |
* unroll_labels_idx[None, :, :] |
| 1048 |
) |
| 1049 |
W = nx.sum(W, axis=1) |
| 1050 |
W = p * ((W + epsilon) ** (p - 1)) |
| 1051 |
W = nx.dot(W, unroll_labels_idx.T) |
| 1052 |
return W.T |
| 1053 |
|
| 1054 |
assert np.allclose(unvectorized(T), vectorized(T)) |