summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/plot_OTDA_2D.py126
-rw-r--r--examples/plot_OTDA_classes.py117
-rw-r--r--examples/plot_OTDA_color_images.py152
-rw-r--r--examples/plot_OTDA_mapping.py124
-rw-r--r--examples/plot_OTDA_mapping_color_images.py169
-rw-r--r--ot/da.py217
6 files changed, 121 insertions, 784 deletions
diff --git a/examples/plot_OTDA_2D.py b/examples/plot_OTDA_2D.py
deleted file mode 100644
index f2108c6..0000000
--- a/examples/plot_OTDA_2D.py
+++ /dev/null
@@ -1,126 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-==============================
-OT for empirical distributions
-==============================
-
-"""
-
-# Author: Remi Flamary <remi.flamary@unice.fr>
-#
-# License: MIT License
-
-import numpy as np
-import matplotlib.pylab as pl
-import ot
-
-
-#%% parameters
-
-n = 150 # nb bins
-
-xs, ys = ot.datasets.get_data_classif('3gauss', n)
-xt, yt = ot.datasets.get_data_classif('3gauss2', n)
-
-a, b = ot.unif(n), ot.unif(n)
-# loss matrix
-M = ot.dist(xs, xt)
-# M/=M.max()
-
-#%% plot samples
-
-pl.figure(1)
-pl.subplot(2, 2, 1)
-pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
-pl.legend(loc=0)
-pl.title('Source distributions')
-
-pl.subplot(2, 2, 2)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
-pl.legend(loc=0)
-pl.title('target distributions')
-
-pl.figure(2)
-pl.imshow(M, interpolation='nearest')
-pl.title('Cost matrix M')
-
-
-#%% OT estimation
-
-# EMD
-G0 = ot.emd(a, b, M)
-
-# sinkhorn
-lambd = 1e-1
-Gs = ot.sinkhorn(a, b, M, lambd)
-
-
-# Group lasso regularization
-reg = 1e-1
-eta = 1e0
-Gg = ot.da.sinkhorn_lpl1_mm(a, ys.astype(np.int), b, M, reg, eta)
-
-
-#%% visu matrices
-
-pl.figure(3)
-
-pl.subplot(2, 3, 1)
-pl.imshow(G0, interpolation='nearest')
-pl.title('OT matrix ')
-
-pl.subplot(2, 3, 2)
-pl.imshow(Gs, interpolation='nearest')
-pl.title('OT matrix Sinkhorn')
-
-pl.subplot(2, 3, 3)
-pl.imshow(Gg, interpolation='nearest')
-pl.title('OT matrix Group lasso')
-
-pl.subplot(2, 3, 4)
-ot.plot.plot2D_samples_mat(xs, xt, G0, c=[.5, .5, 1])
-pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
-
-
-pl.subplot(2, 3, 5)
-ot.plot.plot2D_samples_mat(xs, xt, Gs, c=[.5, .5, 1])
-pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
-
-pl.subplot(2, 3, 6)
-ot.plot.plot2D_samples_mat(xs, xt, Gg, c=[.5, .5, 1])
-pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
-pl.tight_layout()
-
-#%% sample interpolation
-
-xst0 = n * G0.dot(xt)
-xsts = n * Gs.dot(xt)
-xstg = n * Gg.dot(xt)
-
-pl.figure(4, figsize=(8, 3))
-pl.subplot(1, 3, 1)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.5)
-pl.scatter(xst0[:, 0], xst0[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples')
-pl.legend(loc=0)
-
-pl.subplot(1, 3, 2)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.5)
-pl.scatter(xsts[:, 0], xsts[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples Sinkhorn')
-
-pl.subplot(1, 3, 3)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.5)
-pl.scatter(xstg[:, 0], xstg[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples Grouplasso')
-pl.tight_layout()
-pl.show()
diff --git a/examples/plot_OTDA_classes.py b/examples/plot_OTDA_classes.py
deleted file mode 100644
index 53e4bae..0000000
--- a/examples/plot_OTDA_classes.py
+++ /dev/null
@@ -1,117 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-========================
-OT for domain adaptation
-========================
-
-"""
-
-# Author: Remi Flamary <remi.flamary@unice.fr>
-#
-# License: MIT License
-
-import matplotlib.pylab as pl
-import ot
-
-
-#%% parameters
-
-n = 150 # nb samples in source and target datasets
-
-xs, ys = ot.datasets.get_data_classif('3gauss', n)
-xt, yt = ot.datasets.get_data_classif('3gauss2', n)
-
-
-#%% plot samples
-
-pl.figure(1, figsize=(6.4, 3))
-
-pl.subplot(1, 2, 1)
-pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
-pl.legend(loc=0)
-pl.title('Source distributions')
-
-pl.subplot(1, 2, 2)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
-pl.legend(loc=0)
-pl.title('target distributions')
-
-
-#%% OT estimation
-
-# LP problem
-da_emd = ot.da.OTDA() # init class
-da_emd.fit(xs, xt) # fit distributions
-xst0 = da_emd.interp() # interpolation of source samples
-
-# sinkhorn regularization
-lambd = 1e-1
-da_entrop = ot.da.OTDA_sinkhorn()
-da_entrop.fit(xs, xt, reg=lambd)
-xsts = da_entrop.interp()
-
-# non-convex Group lasso regularization
-reg = 1e-1
-eta = 1e0
-da_lpl1 = ot.da.OTDA_lpl1()
-da_lpl1.fit(xs, ys, xt, reg=reg, eta=eta)
-xstg = da_lpl1.interp()
-
-# True Group lasso regularization
-reg = 1e-1
-eta = 2e0
-da_l1l2 = ot.da.OTDA_l1l2()
-da_l1l2.fit(xs, ys, xt, reg=reg, eta=eta, numItermax=20, verbose=True)
-xstgl = da_l1l2.interp()
-
-#%% plot interpolated source samples
-
-param_img = {'interpolation': 'nearest', 'cmap': 'spectral'}
-
-pl.figure(2, figsize=(8, 4.5))
-pl.subplot(2, 4, 1)
-pl.imshow(da_emd.G, **param_img)
-pl.title('OT matrix')
-
-pl.subplot(2, 4, 2)
-pl.imshow(da_entrop.G, **param_img)
-pl.title('OT matrix\nsinkhorn')
-
-pl.subplot(2, 4, 3)
-pl.imshow(da_lpl1.G, **param_img)
-pl.title('OT matrix\nnon-convex Group Lasso')
-
-pl.subplot(2, 4, 4)
-pl.imshow(da_l1l2.G, **param_img)
-pl.title('OT matrix\nGroup Lasso')
-
-pl.subplot(2, 4, 5)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.3)
-pl.scatter(xst0[:, 0], xst0[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples')
-pl.legend(loc=0)
-
-pl.subplot(2, 4, 6)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.3)
-pl.scatter(xsts[:, 0], xsts[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples\nSinkhorn')
-
-pl.subplot(2, 4, 7)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.3)
-pl.scatter(xstg[:, 0], xstg[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples\nnon-convex Group Lasso')
-
-pl.subplot(2, 4, 8)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=0.3)
-pl.scatter(xstgl[:, 0], xstgl[:, 1], c=ys,
- marker='+', label='Transp samples', s=30)
-pl.title('Interp samples\nGroup Lasso')
-pl.tight_layout()
-pl.show()
diff --git a/examples/plot_OTDA_color_images.py b/examples/plot_OTDA_color_images.py
deleted file mode 100644
index c5ff873..0000000
--- a/examples/plot_OTDA_color_images.py
+++ /dev/null
@@ -1,152 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-========================================================
-OT for domain adaptation with image color adaptation [6]
-========================================================
-
-[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).
-Regularized discrete optimal transport.
-SIAM Journal on Imaging Sciences, 7(3), 1853-1882.
-"""
-
-# Author: Remi Flamary <remi.flamary@unice.fr>
-#
-# License: MIT License
-
-import numpy as np
-from scipy import ndimage
-import matplotlib.pylab as pl
-import ot
-
-
-#%% Loading images
-
-I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256
-I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256
-
-#%% Plot images
-
-pl.figure(1, figsize=(6.4, 3))
-
-pl.subplot(1, 2, 1)
-pl.imshow(I1)
-pl.axis('off')
-pl.title('Image 1')
-
-pl.subplot(1, 2, 2)
-pl.imshow(I2)
-pl.axis('off')
-pl.title('Image 2')
-
-pl.show()
-
-#%% Image conversion and dataset generation
-
-
-def im2mat(I):
- """Converts and image to matrix (one pixel per line)"""
- return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))
-
-
-def mat2im(X, shape):
- """Converts back a matrix to an image"""
- return X.reshape(shape)
-
-
-X1 = im2mat(I1)
-X2 = im2mat(I2)
-
-# training samples
-nb = 1000
-idx1 = np.random.randint(X1.shape[0], size=(nb,))
-idx2 = np.random.randint(X2.shape[0], size=(nb,))
-
-xs = X1[idx1, :]
-xt = X2[idx2, :]
-
-#%% Plot image distributions
-
-
-pl.figure(2, figsize=(6.4, 3))
-
-pl.subplot(1, 2, 1)
-pl.scatter(xs[:, 0], xs[:, 2], c=xs)
-pl.axis([0, 1, 0, 1])
-pl.xlabel('Red')
-pl.ylabel('Blue')
-pl.title('Image 1')
-
-pl.subplot(1, 2, 2)
-pl.scatter(xt[:, 0], xt[:, 2], c=xt)
-pl.axis([0, 1, 0, 1])
-pl.xlabel('Red')
-pl.ylabel('Blue')
-pl.title('Image 2')
-pl.tight_layout()
-
-#%% domain adaptation between images
-
-# LP problem
-da_emd = ot.da.OTDA() # init class
-da_emd.fit(xs, xt) # fit distributions
-
-# sinkhorn regularization
-lambd = 1e-1
-da_entrop = ot.da.OTDA_sinkhorn()
-da_entrop.fit(xs, xt, reg=lambd)
-
-#%% prediction between images (using out of sample prediction as in [6])
-
-X1t = da_emd.predict(X1)
-X2t = da_emd.predict(X2, -1)
-
-X1te = da_entrop.predict(X1)
-X2te = da_entrop.predict(X2, -1)
-
-
-def minmax(I):
- return np.clip(I, 0, 1)
-
-
-I1t = minmax(mat2im(X1t, I1.shape))
-I2t = minmax(mat2im(X2t, I2.shape))
-
-I1te = minmax(mat2im(X1te, I1.shape))
-I2te = minmax(mat2im(X2te, I2.shape))
-
-#%% plot all images
-
-pl.figure(2, figsize=(8, 4))
-
-pl.subplot(2, 3, 1)
-pl.imshow(I1)
-pl.axis('off')
-pl.title('Image 1')
-
-pl.subplot(2, 3, 2)
-pl.imshow(I1t)
-pl.axis('off')
-pl.title('Image 1 Adapt')
-
-pl.subplot(2, 3, 3)
-pl.imshow(I1te)
-pl.axis('off')
-pl.title('Image 1 Adapt (reg)')
-
-pl.subplot(2, 3, 4)
-pl.imshow(I2)
-pl.axis('off')
-pl.title('Image 2')
-
-pl.subplot(2, 3, 5)
-pl.imshow(I2t)
-pl.axis('off')
-pl.title('Image 2 Adapt')
-
-pl.subplot(2, 3, 6)
-pl.imshow(I2te)
-pl.axis('off')
-pl.title('Image 2 Adapt (reg)')
-pl.tight_layout()
-
-pl.show()
diff --git a/examples/plot_OTDA_mapping.py b/examples/plot_OTDA_mapping.py
deleted file mode 100644
index a0d7f8b..0000000
--- a/examples/plot_OTDA_mapping.py
+++ /dev/null
@@ -1,124 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-===============================================
-OT mapping estimation for domain adaptation [8]
-===============================================
-
-[8] M. Perrot, N. Courty, R. Flamary, A. Habrard,
- "Mapping estimation for discrete optimal transport",
- Neural Information Processing Systems (NIPS), 2016.
-"""
-
-# Author: Remi Flamary <remi.flamary@unice.fr>
-#
-# License: MIT License
-
-import numpy as np
-import matplotlib.pylab as pl
-import ot
-
-
-#%% dataset generation
-
-np.random.seed(0) # makes example reproducible
-
-n = 100 # nb samples in source and target datasets
-theta = 2 * np.pi / 20
-nz = 0.1
-xs, ys = ot.datasets.get_data_classif('gaussrot', n, nz=nz)
-xt, yt = ot.datasets.get_data_classif('gaussrot', n, theta=theta, nz=nz)
-
-# one of the target mode changes its variance (no linear mapping)
-xt[yt == 2] *= 3
-xt = xt + 4
-
-
-#%% plot samples
-
-pl.figure(1, (6.4, 3))
-pl.clf()
-pl.scatter(xs[:, 0], xs[:, 1], c=ys, marker='+', label='Source samples')
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o', label='Target samples')
-pl.legend(loc=0)
-pl.title('Source and target distributions')
-
-
-#%% OT linear mapping estimation
-
-eta = 1e-8 # quadratic regularization for regression
-mu = 1e0 # weight of the OT linear term
-bias = True # estimate a bias
-
-ot_mapping = ot.da.OTDA_mapping_linear()
-ot_mapping.fit(xs, xt, mu=mu, eta=eta, bias=bias, numItermax=20, verbose=True)
-
-xst = ot_mapping.predict(xs) # use the estimated mapping
-xst0 = ot_mapping.interp() # use barycentric mapping
-
-
-pl.figure(2)
-pl.clf()
-pl.subplot(2, 2, 1)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=.3)
-pl.scatter(xst0[:, 0], xst0[:, 1], c=ys,
- marker='+', label='barycentric mapping')
-pl.title("barycentric mapping")
-
-pl.subplot(2, 2, 2)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=.3)
-pl.scatter(xst[:, 0], xst[:, 1], c=ys, marker='+', label='Learned mapping')
-pl.title("Learned mapping")
-pl.tight_layout()
-
-#%% Kernel mapping estimation
-
-eta = 1e-5 # quadratic regularization for regression
-mu = 1e-1 # weight of the OT linear term
-bias = True # estimate a bias
-sigma = 1 # sigma bandwidth fot gaussian kernel
-
-
-ot_mapping_kernel = ot.da.OTDA_mapping_kernel()
-ot_mapping_kernel.fit(
- xs, xt, mu=mu, eta=eta, sigma=sigma, bias=bias, numItermax=10, verbose=True)
-
-xst_kernel = ot_mapping_kernel.predict(xs) # use the estimated mapping
-xst0_kernel = ot_mapping_kernel.interp() # use barycentric mapping
-
-
-#%% Plotting the mapped samples
-
-pl.figure(2)
-pl.clf()
-pl.subplot(2, 2, 1)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=.2)
-pl.scatter(xst0[:, 0], xst0[:, 1], c=ys, marker='+',
- label='Mapped source samples')
-pl.title("Bary. mapping (linear)")
-pl.legend(loc=0)
-
-pl.subplot(2, 2, 2)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=.2)
-pl.scatter(xst[:, 0], xst[:, 1], c=ys, marker='+', label='Learned mapping')
-pl.title("Estim. mapping (linear)")
-
-pl.subplot(2, 2, 3)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=.2)
-pl.scatter(xst0_kernel[:, 0], xst0_kernel[:, 1], c=ys,
- marker='+', label='barycentric mapping')
-pl.title("Bary. mapping (kernel)")
-
-pl.subplot(2, 2, 4)
-pl.scatter(xt[:, 0], xt[:, 1], c=yt, marker='o',
- label='Target samples', alpha=.2)
-pl.scatter(xst_kernel[:, 0], xst_kernel[:, 1], c=ys,
- marker='+', label='Learned mapping')
-pl.title("Estim. mapping (kernel)")
-pl.tight_layout()
-
-pl.show()
diff --git a/examples/plot_OTDA_mapping_color_images.py b/examples/plot_OTDA_mapping_color_images.py
deleted file mode 100644
index 8064b25..0000000
--- a/examples/plot_OTDA_mapping_color_images.py
+++ /dev/null
@@ -1,169 +0,0 @@
-# -*- coding: utf-8 -*-
-"""
-====================================================================================
-OT for domain adaptation with image color adaptation [6] with mapping estimation [8]
-====================================================================================
-
-[6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014). Regularized
- discrete optimal transport. SIAM Journal on Imaging Sciences, 7(3), 1853-1882.
-[8] M. Perrot, N. Courty, R. Flamary, A. Habrard, "Mapping estimation for
- discrete optimal transport", Neural Information Processing Systems (NIPS), 2016.
-
-"""
-
-# Author: Remi Flamary <remi.flamary@unice.fr>
-#
-# License: MIT License
-
-import numpy as np
-from scipy import ndimage
-import matplotlib.pylab as pl
-import ot
-
-
-#%% Loading images
-
-I1 = ndimage.imread('../data/ocean_day.jpg').astype(np.float64) / 256
-I2 = ndimage.imread('../data/ocean_sunset.jpg').astype(np.float64) / 256
-
-#%% Plot images
-
-pl.figure(1, figsize=(6.4, 3))
-pl.subplot(1, 2, 1)
-pl.imshow(I1)
-pl.axis('off')
-pl.title('Image 1')
-
-pl.subplot(1, 2, 2)
-pl.imshow(I2)
-pl.axis('off')
-pl.title('Image 2')
-pl.tight_layout()
-
-
-#%% Image conversion and dataset generation
-
-def im2mat(I):
- """Converts and image to matrix (one pixel per line)"""
- return I.reshape((I.shape[0] * I.shape[1], I.shape[2]))
-
-
-def mat2im(X, shape):
- """Converts back a matrix to an image"""
- return X.reshape(shape)
-
-
-X1 = im2mat(I1)
-X2 = im2mat(I2)
-
-# training samples
-nb = 1000
-idx1 = np.random.randint(X1.shape[0], size=(nb,))
-idx2 = np.random.randint(X2.shape[0], size=(nb,))
-
-xs = X1[idx1, :]
-xt = X2[idx2, :]
-
-#%% Plot image distributions
-
-
-pl.figure(2, figsize=(6.4, 5))
-
-pl.subplot(1, 2, 1)
-pl.scatter(xs[:, 0], xs[:, 2], c=xs)
-pl.axis([0, 1, 0, 1])
-pl.xlabel('Red')
-pl.ylabel('Blue')
-pl.title('Image 1')
-
-pl.subplot(1, 2, 2)
-pl.scatter(xt[:, 0], xt[:, 2], c=xt)
-pl.axis([0, 1, 0, 1])
-pl.xlabel('Red')
-pl.ylabel('Blue')
-pl.title('Image 2')
-pl.tight_layout()
-
-
-#%% domain adaptation between images
-
-def minmax(I):
- return np.clip(I, 0, 1)
-
-
-# LP problem
-da_emd = ot.da.OTDA() # init class
-da_emd.fit(xs, xt) # fit distributions
-
-X1t = da_emd.predict(X1) # out of sample
-I1t = minmax(mat2im(X1t, I1.shape))
-
-# sinkhorn regularization
-lambd = 1e-1
-da_entrop = ot.da.OTDA_sinkhorn()
-da_entrop.fit(xs, xt, reg=lambd)
-
-X1te = da_entrop.predict(X1)
-I1te = minmax(mat2im(X1te, I1.shape))
-
-# linear mapping estimation
-eta = 1e-8 # quadratic regularization for regression
-mu = 1e0 # weight of the OT linear term
-bias = True # estimate a bias
-
-ot_mapping = ot.da.OTDA_mapping_linear()
-ot_mapping.fit(xs, xt, mu=mu, eta=eta, bias=bias, numItermax=20, verbose=True)
-
-X1tl = ot_mapping.predict(X1) # use the estimated mapping
-I1tl = minmax(mat2im(X1tl, I1.shape))
-
-# nonlinear mapping estimation
-eta = 1e-2 # quadratic regularization for regression
-mu = 1e0 # weight of the OT linear term
-bias = False # estimate a bias
-sigma = 1 # sigma bandwidth fot gaussian kernel
-
-
-ot_mapping_kernel = ot.da.OTDA_mapping_kernel()
-ot_mapping_kernel.fit(
- xs, xt, mu=mu, eta=eta, sigma=sigma, bias=bias, numItermax=10, verbose=True)
-
-X1tn = ot_mapping_kernel.predict(X1) # use the estimated mapping
-I1tn = minmax(mat2im(X1tn, I1.shape))
-
-#%% plot images
-
-pl.figure(2, figsize=(8, 4))
-
-pl.subplot(2, 3, 1)
-pl.imshow(I1)
-pl.axis('off')
-pl.title('Im. 1')
-
-pl.subplot(2, 3, 2)
-pl.imshow(I2)
-pl.axis('off')
-pl.title('Im. 2')
-
-pl.subplot(2, 3, 3)
-pl.imshow(I1t)
-pl.axis('off')
-pl.title('Im. 1 Interp LP')
-
-pl.subplot(2, 3, 4)
-pl.imshow(I1te)
-pl.axis('off')
-pl.title('Im. 1 Interp Entrop')
-
-pl.subplot(2, 3, 5)
-pl.imshow(I1tl)
-pl.axis('off')
-pl.title('Im. 1 Linear mapping')
-
-pl.subplot(2, 3, 6)
-pl.imshow(I1tn)
-pl.axis('off')
-pl.title('Im. 1 nonlinear mapping')
-pl.tight_layout()
-
-pl.show()
diff --git a/ot/da.py b/ot/da.py
index 8c62669..369b6a2 100644
--- a/ot/da.py
+++ b/ot/da.py
@@ -976,36 +976,41 @@ class BaseTransport(BaseEstimator):
Returns self.
"""
- # pairwise distance
- self.cost_ = dist(Xs, Xt, metric=self.metric)
+ if Xs is not None and Xt is not None:
+ # pairwise distance
+ self.cost_ = dist(Xs, Xt, metric=self.metric)
- if (ys is not None) and (yt is not None):
+ if (ys is not None) and (yt is not None):
- if self.limit_max != np.infty:
- self.limit_max = self.limit_max * np.max(self.cost_)
+ if self.limit_max != np.infty:
+ self.limit_max = self.limit_max * np.max(self.cost_)
- # assumes labeled source samples occupy the first rows
- # and labeled target samples occupy the first columns
- classes = np.unique(ys)
- for c in classes:
- idx_s = np.where((ys != c) & (ys != -1))
- idx_t = np.where(yt == c)
+ # assumes labeled source samples occupy the first rows
+ # and labeled target samples occupy the first columns
+ classes = np.unique(ys)
+ for c in classes:
+ idx_s = np.where((ys != c) & (ys != -1))
+ idx_t = np.where(yt == c)
- # all the coefficients corresponding to a source sample
- # and a target sample :
- # with different labels get a infinite
- for j in idx_t[0]:
- self.cost_[idx_s[0], j] = self.limit_max
+ # all the coefficients corresponding to a source sample
+ # and a target sample :
+ # with different labels get a infinite
+ for j in idx_t[0]:
+ self.cost_[idx_s[0], j] = self.limit_max
- # distribution estimation
- self.mu_s = self.distribution_estimation(Xs)
- self.mu_t = self.distribution_estimation(Xt)
+ # distribution estimation
+ self.mu_s = self.distribution_estimation(Xs)
+ self.mu_t = self.distribution_estimation(Xt)
- # store arrays of samples
- self.Xs = Xs
- self.Xt = Xt
+ # store arrays of samples
+ self.Xs = Xs
+ self.Xt = Xt
- return self
+ return self
+ else:
+ print("POT-Warning")
+ print("Please provide both Xs and Xt arguments when calling")
+ print("fit method")
def fit_transform(self, Xs=None, ys=None, Xt=None, yt=None):
"""Build a coupling matrix from source and target sets of samples
@@ -1053,42 +1058,47 @@ class BaseTransport(BaseEstimator):
The transport source samples.
"""
- if np.array_equal(self.Xs, Xs):
- # perform standard barycentric mapping
- transp = self.coupling_ / np.sum(self.coupling_, 1)[:, None]
+ if Xs is not None:
+ if np.array_equal(self.Xs, Xs):
+ # perform standard barycentric mapping
+ transp = self.coupling_ / np.sum(self.coupling_, 1)[:, None]
- # set nans to 0
- transp[~ np.isfinite(transp)] = 0
+ # set nans to 0
+ transp[~ np.isfinite(transp)] = 0
- # compute transported samples
- transp_Xs = np.dot(transp, self.Xt)
- else:
- # perform out of sample mapping
- indices = np.arange(Xs.shape[0])
- batch_ind = [
- indices[i:i + batch_size]
- for i in range(0, len(indices), batch_size)]
+ # compute transported samples
+ transp_Xs = np.dot(transp, self.Xt)
+ else:
+ # perform out of sample mapping
+ indices = np.arange(Xs.shape[0])
+ batch_ind = [
+ indices[i:i + batch_size]
+ for i in range(0, len(indices), batch_size)]
- transp_Xs = []
- for bi in batch_ind:
+ transp_Xs = []
+ for bi in batch_ind:
- # get the nearest neighbor in the source domain
- D0 = dist(Xs[bi], self.Xs)
- idx = np.argmin(D0, axis=1)
+ # get the nearest neighbor in the source domain
+ D0 = dist(Xs[bi], self.Xs)
+ idx = np.argmin(D0, axis=1)
- # transport the source samples
- transp = self.coupling_ / np.sum(self.coupling_, 1)[:, None]
- transp[~ np.isfinite(transp)] = 0
- transp_Xs_ = np.dot(transp, self.Xt)
+ # transport the source samples
+ transp = self.coupling_ / np.sum(
+ self.coupling_, 1)[:, None]
+ transp[~ np.isfinite(transp)] = 0
+ transp_Xs_ = np.dot(transp, self.Xt)
- # define the transported points
- transp_Xs_ = transp_Xs_[idx, :] + Xs[bi] - self.Xs[idx, :]
+ # define the transported points
+ transp_Xs_ = transp_Xs_[idx, :] + Xs[bi] - self.Xs[idx, :]
- transp_Xs.append(transp_Xs_)
+ transp_Xs.append(transp_Xs_)
- transp_Xs = np.concatenate(transp_Xs, axis=0)
+ transp_Xs = np.concatenate(transp_Xs, axis=0)
- return transp_Xs
+ return transp_Xs
+ else:
+ print("POT-Warning")
+ print("Please provide Xs argument when calling transform method")
def inverse_transform(self, Xs=None, ys=None, Xt=None, yt=None,
batch_size=128):
@@ -1113,41 +1123,46 @@ class BaseTransport(BaseEstimator):
The transported target samples.
"""
- if np.array_equal(self.Xt, Xt):
- # perform standard barycentric mapping
- transp_ = self.coupling_.T / np.sum(self.coupling_, 0)[:, None]
+ if Xt is not None:
+ if np.array_equal(self.Xt, Xt):
+ # perform standard barycentric mapping
+ transp_ = self.coupling_.T / np.sum(self.coupling_, 0)[:, None]
- # set nans to 0
- transp_[~ np.isfinite(transp_)] = 0
+ # set nans to 0
+ transp_[~ np.isfinite(transp_)] = 0
- # compute transported samples
- transp_Xt = np.dot(transp_, self.Xs)
- else:
- # perform out of sample mapping
- indices = np.arange(Xt.shape[0])
- batch_ind = [
- indices[i:i + batch_size]
- for i in range(0, len(indices), batch_size)]
+ # compute transported samples
+ transp_Xt = np.dot(transp_, self.Xs)
+ else:
+ # perform out of sample mapping
+ indices = np.arange(Xt.shape[0])
+ batch_ind = [
+ indices[i:i + batch_size]
+ for i in range(0, len(indices), batch_size)]
- transp_Xt = []
- for bi in batch_ind:
+ transp_Xt = []
+ for bi in batch_ind:
- D0 = dist(Xt[bi], self.Xt)
- idx = np.argmin(D0, axis=1)
+ D0 = dist(Xt[bi], self.Xt)
+ idx = np.argmin(D0, axis=1)
- # transport the target samples
- transp_ = self.coupling_.T / np.sum(self.coupling_, 0)[:, None]
- transp_[~ np.isfinite(transp_)] = 0
- transp_Xt_ = np.dot(transp_, self.Xs)
+ # transport the target samples
+ transp_ = self.coupling_.T / np.sum(
+ self.coupling_, 0)[:, None]
+ transp_[~ np.isfinite(transp_)] = 0
+ transp_Xt_ = np.dot(transp_, self.Xs)
- # define the transported points
- transp_Xt_ = transp_Xt_[idx, :] + Xt[bi] - self.Xt[idx, :]
+ # define the transported points
+ transp_Xt_ = transp_Xt_[idx, :] + Xt[bi] - self.Xt[idx, :]
- transp_Xt.append(transp_Xt_)
+ transp_Xt.append(transp_Xt_)
- transp_Xt = np.concatenate(transp_Xt, axis=0)
+ transp_Xt = np.concatenate(transp_Xt, axis=0)
- return transp_Xt
+ return transp_Xt
+ else:
+ print("POT-Warning")
+ print("Please provide Xt argument when calling inverse_transform")
class SinkhornTransport(BaseTransport):
@@ -1413,15 +1428,20 @@ class SinkhornLpl1Transport(BaseTransport):
Returns self.
"""
- super(SinkhornLpl1Transport, self).fit(Xs, ys, Xt, yt)
+ if Xs is not None and Xt is not None and ys is not None:
- self.coupling_ = sinkhorn_lpl1_mm(
- a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_,
- reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter,
- numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol,
- verbose=self.verbose)
+ super(SinkhornLpl1Transport, self).fit(Xs, ys, Xt, yt)
- return self
+ self.coupling_ = sinkhorn_lpl1_mm(
+ a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_,
+ reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter,
+ numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol,
+ verbose=self.verbose)
+
+ return self
+ else:
+ print("POT-Warning")
+ print("Please provide both Xs, Xt, ys arguments to fit method")
class SinkhornL1l2Transport(BaseTransport):
@@ -1517,22 +1537,27 @@ class SinkhornL1l2Transport(BaseTransport):
Returns self.
"""
- super(SinkhornL1l2Transport, self).fit(Xs, ys, Xt, yt)
+ if Xs is not None and Xt is not None and ys is not None:
- returned_ = sinkhorn_l1l2_gl(
- a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_,
- reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter,
- numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol,
- verbose=self.verbose, log=self.log)
+ super(SinkhornL1l2Transport, self).fit(Xs, ys, Xt, yt)
- # deal with the value of log
- if self.log:
- self.coupling_, self.log_ = returned_
- else:
- self.coupling_ = returned_
- self.log_ = dict()
+ returned_ = sinkhorn_l1l2_gl(
+ a=self.mu_s, labels_a=ys, b=self.mu_t, M=self.cost_,
+ reg=self.reg_e, eta=self.reg_cl, numItermax=self.max_iter,
+ numInnerItermax=self.max_inner_iter, stopInnerThr=self.tol,
+ verbose=self.verbose, log=self.log)
- return self
+ # deal with the value of log
+ if self.log:
+ self.coupling_, self.log_ = returned_
+ else:
+ self.coupling_ = returned_
+ self.log_ = dict()
+
+ return self
+ else:
+ print("POT-Warning")
+ print("Please, provide both Xs, Xt and ys argument to fit method")
class MappingTransport(BaseEstimator):