diff options
author | Kilian <kilian.fatras@gmail.com> | 2018-06-26 11:10:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-26 11:10:40 -0700 |
commit | b4bc86176a5712fdd2f930fbf5d1968edd5efa5e (patch) | |
tree | 075c694496216f0a6db61e879ece5eb2e799fc07 /examples | |
parent | 208ff4627ba28aa3f35328c5928324019c23ddac (diff) | |
parent | 327b0c6e0ccb0c9453179eb316021c34bcdffec4 (diff) |
Merge branch 'master' into stochastic_OT
Diffstat (limited to 'examples')
-rw-r--r-- | examples/plot_OT_1D.py | 2 | ||||
-rw-r--r-- | examples/plot_OT_1D_smooth.py | 110 | ||||
-rw-r--r-- | examples/plot_OT_2D_samples.py | 5 | ||||
-rw-r--r-- | examples/plot_barycenter_1D.py | 4 | ||||
-rw-r--r-- | examples/plot_barycenter_lp_vs_entropic.py | 29 | ||||
-rw-r--r-- | examples/plot_compute_emd.py | 2 | ||||
-rw-r--r-- | examples/plot_gromov.py | 2 | ||||
-rw-r--r-- | examples/plot_optim_OTreg.py | 4 | ||||
-rw-r--r-- | examples/plot_otda_classes.py | 6 | ||||
-rw-r--r-- | examples/plot_otda_d2.py | 4 | ||||
-rw-r--r-- | examples/plot_otda_linear_mapping.py | 10 | ||||
-rw-r--r-- | examples/plot_otda_mapping.py | 6 | ||||
-rw-r--r-- | examples/plot_otda_semi_supervised.py | 4 |
13 files changed, 147 insertions, 41 deletions
diff --git a/examples/plot_OT_1D.py b/examples/plot_OT_1D.py index 90325c9..f33e2a4 100644 --- a/examples/plot_OT_1D.py +++ b/examples/plot_OT_1D.py @@ -17,7 +17,7 @@ import numpy as np import matplotlib.pylab as pl import ot import ot.plot -from ot.datasets import get_1D_gauss as gauss +from ot.datasets import make_1D_gauss as gauss ############################################################################## # Generate data diff --git a/examples/plot_OT_1D_smooth.py b/examples/plot_OT_1D_smooth.py new file mode 100644 index 0000000..b690751 --- /dev/null +++ b/examples/plot_OT_1D_smooth.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +""" +=========================== +1D smooth optimal transport +=========================== + +This example illustrates the computation of EMD, Sinkhorn and smooth OT plans +and their visualization. + +""" + +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + +import numpy as np +import matplotlib.pylab as pl +import ot +import ot.plot +from ot.datasets import make_1D_gauss as gauss + +############################################################################## +# Generate data +# ------------- + + +#%% parameters + +n = 100 # nb bins + +# bin positions +x = np.arange(n, dtype=np.float64) + +# Gaussian distributions +a = gauss(n, m=20, s=5) # m= mean, s= std +b = gauss(n, m=60, s=10) + +# loss matrix +M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) +M /= M.max() + + +############################################################################## +# Plot distributions and loss matrix +# ---------------------------------- + +#%% plot the distributions + +pl.figure(1, figsize=(6.4, 3)) +pl.plot(x, a, 'b', label='Source distribution') +pl.plot(x, b, 'r', label='Target distribution') +pl.legend() + +#%% plot distributions and loss matrix + +pl.figure(2, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, M, 'Cost matrix M') + +############################################################################## +# Solve EMD +# --------- + + +#%% EMD + +G0 = ot.emd(a, b, M) + +pl.figure(3, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, G0, 'OT matrix G0') + +############################################################################## +# Solve Sinkhorn +# -------------- + + +#%% Sinkhorn + +lambd = 2e-3 +Gs = ot.sinkhorn(a, b, M, lambd, verbose=True) + +pl.figure(4, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gs, 'OT matrix Sinkhorn') + +pl.show() + +############################################################################## +# Solve Smooth OT +# -------------- + + +#%% Smooth OT with KL regularization + +lambd = 2e-3 +Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='kl') + +pl.figure(5, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT KL reg.') + +pl.show() + + +#%% Smooth OT with KL regularization + +lambd = 1e-1 +Gsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type='l2') + +pl.figure(6, figsize=(5, 5)) +ot.plot.plot1D_mat(a, b, Gsm, 'OT matrix Smooth OT l2 reg.') + +pl.show() diff --git a/examples/plot_OT_2D_samples.py b/examples/plot_OT_2D_samples.py index 9818ec5..bb952a0 100644 --- a/examples/plot_OT_2D_samples.py +++ b/examples/plot_OT_2D_samples.py @@ -16,6 +16,7 @@ sum of diracs. The OT matrix is plotted with the samples. import numpy as np import matplotlib.pylab as pl import ot +import ot.plot ############################################################################## # Generate data @@ -31,8 +32,8 @@ cov_s = np.array([[1, 0], [0, 1]]) mu_t = np.array([4, 4]) cov_t = np.array([[1, -.8], [-.8, 1]]) -xs = ot.datasets.get_2D_samples_gauss(n, mu_s, cov_s) -xt = ot.datasets.get_2D_samples_gauss(n, mu_t, cov_t) +xs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s) +xt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t) a, b = np.ones((n,)) / n, np.ones((n,)) / n # uniform distribution on samples diff --git a/examples/plot_barycenter_1D.py b/examples/plot_barycenter_1D.py index ecf640c..5ed9f3f 100644 --- a/examples/plot_barycenter_1D.py +++ b/examples/plot_barycenter_1D.py @@ -37,8 +37,8 @@ n = 100 # nb bins x = np.arange(n, dtype=np.float64) # Gaussian distributions -a1 = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std -a2 = ot.datasets.get_1D_gauss(n, m=60, s=8) +a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std +a2 = ot.datasets.make_1D_gauss(n, m=60, s=8) # creating matrix A containing all distributions A = np.vstack((a1, a2)).T diff --git a/examples/plot_barycenter_lp_vs_entropic.py b/examples/plot_barycenter_lp_vs_entropic.py index 6936bbb..b82765e 100644 --- a/examples/plot_barycenter_lp_vs_entropic.py +++ b/examples/plot_barycenter_lp_vs_entropic.py @@ -15,8 +15,6 @@ Wasserstein problems. SIAM Journal on Imaging Sciences, 9(1), 320-343. Iterative Bregman projections for regularized transportation problems SIAM Journal on Scientific Computing, 37(2), A1111-A1138. - - """ # Author: Remi Flamary <remi.flamary@unice.fr> @@ -32,8 +30,8 @@ from matplotlib.collections import PolyCollection # noqa #import ot.lp.cvx as cvx -# -# Generate data +############################################################################## +# Gaussian Data # ------------- #%% parameters @@ -47,8 +45,8 @@ x = np.arange(n, dtype=np.float64) # Gaussian distributions # Gaussian distributions -a1 = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std -a2 = ot.datasets.get_1D_gauss(n, m=60, s=8) +a1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std +a2 = ot.datasets.make_1D_gauss(n, m=60, s=8) # creating matrix A containing all distributions A = np.vstack((a1, a2)).T @@ -58,9 +56,6 @@ n_distributions = A.shape[1] M = ot.utils.dist0(n) M /= M.max() -# -# Plot data -# --------- #%% plot the distributions @@ -70,10 +65,6 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() -# -# Barycenter computation -# ---------------------- - #%% barycenter computation alpha = 0.5 # 0<=alpha<=1 @@ -110,6 +101,10 @@ pl.tight_layout() problems.append([A, [bary_l2, bary_wass, bary_wass2]]) +############################################################################## +# Dirac Data +# ---------- + #%% parameters a1 = 1.0 * (x > 10) * (x < 50) @@ -135,9 +130,6 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() -# -# Barycenter computation -# ---------------------- #%% barycenter computation @@ -207,9 +199,6 @@ for i in range(n_distributions): pl.title('Distributions') pl.tight_layout() -# -# Barycenter computation -# ---------------------- #%% barycenter computation @@ -249,7 +238,7 @@ pl.title('Barycenters') pl.tight_layout() -# +############################################################################## # Final figure # ------------ # diff --git a/examples/plot_compute_emd.py b/examples/plot_compute_emd.py index 73b42c3..7ed2b01 100644 --- a/examples/plot_compute_emd.py +++ b/examples/plot_compute_emd.py @@ -17,7 +17,7 @@ ground metrics and plot their values for diffeent distributions. import numpy as np import matplotlib.pylab as pl import ot -from ot.datasets import get_1D_gauss as gauss +from ot.datasets import make_1D_gauss as gauss ############################################################################## diff --git a/examples/plot_gromov.py b/examples/plot_gromov.py index 5cd40f6..deb2f86 100644 --- a/examples/plot_gromov.py +++ b/examples/plot_gromov.py @@ -38,7 +38,7 @@ mu_t = np.array([4, 4, 4]) cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
-xs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)
+xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s)
P = sp.linalg.sqrtm(cov_t)
xt = np.random.randn(n_samples, 3).dot(P) + mu_t
diff --git a/examples/plot_optim_OTreg.py b/examples/plot_optim_OTreg.py index 92df016..2c58def 100644 --- a/examples/plot_optim_OTreg.py +++ b/examples/plot_optim_OTreg.py @@ -42,8 +42,8 @@ n = 100 # nb bins x = np.arange(n, dtype=np.float64) # Gaussian distributions -a = ot.datasets.get_1D_gauss(n, m=20, s=5) # m= mean, s= std -b = ot.datasets.get_1D_gauss(n, m=60, s=10) +a = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std +b = ot.datasets.make_1D_gauss(n, m=60, s=10) # loss matrix M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1))) diff --git a/examples/plot_otda_classes.py b/examples/plot_otda_classes.py index b14c11a..c311fbd 100644 --- a/examples/plot_otda_classes.py +++ b/examples/plot_otda_classes.py @@ -25,8 +25,8 @@ import ot n_source_samples = 150 n_target_samples = 150 -Xs, ys = ot.datasets.get_data_classif('3gauss', n_source_samples) -Xt, yt = ot.datasets.get_data_classif('3gauss2', n_target_samples) +Xs, ys = ot.datasets.make_data_classif('3gauss', n_source_samples) +Xt, yt = ot.datasets.make_data_classif('3gauss2', n_target_samples) ############################################################################## @@ -82,7 +82,7 @@ pl.tight_layout() # Fig 2 : plot optimal couplings and transported samples # ------------------------------------------------------ -param_img = {'interpolation': 'nearest', 'cmap': 'spectral'} +param_img = {'interpolation': 'nearest'} pl.figure(2, figsize=(15, 8)) pl.subplot(2, 4, 1) diff --git a/examples/plot_otda_d2.py b/examples/plot_otda_d2.py index 70beb35..cf22c2f 100644 --- a/examples/plot_otda_d2.py +++ b/examples/plot_otda_d2.py @@ -29,8 +29,8 @@ import ot.plot n_samples_source = 150 n_samples_target = 150 -Xs, ys = ot.datasets.get_data_classif('3gauss', n_samples_source) -Xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples_target) +Xs, ys = ot.datasets.make_data_classif('3gauss', n_samples_source) +Xt, yt = ot.datasets.make_data_classif('3gauss2', n_samples_target) # Cost matrix M = ot.dist(Xs, Xt, metric='sqeuclidean') diff --git a/examples/plot_otda_linear_mapping.py b/examples/plot_otda_linear_mapping.py index 7a3b761..c65bd4f 100644 --- a/examples/plot_otda_linear_mapping.py +++ b/examples/plot_otda_linear_mapping.py @@ -1,11 +1,17 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ -Created on Tue Mar 20 14:31:15 2018 +============================ +Linear OT mapping estimation +============================ + -@author: rflamary """ +# Author: Remi Flamary <remi.flamary@unice.fr> +# +# License: MIT License + import numpy as np import pylab as pl import ot diff --git a/examples/plot_otda_mapping.py b/examples/plot_otda_mapping.py index 167c3a1..5880adf 100644 --- a/examples/plot_otda_mapping.py +++ b/examples/plot_otda_mapping.py @@ -32,11 +32,11 @@ n_target_samples = 100 theta = 2 * np.pi / 20 noise_level = 0.1 -Xs, ys = ot.datasets.get_data_classif( +Xs, ys = ot.datasets.make_data_classif( 'gaussrot', n_source_samples, nz=noise_level) -Xs_new, _ = ot.datasets.get_data_classif( +Xs_new, _ = ot.datasets.make_data_classif( 'gaussrot', n_source_samples, nz=noise_level) -Xt, yt = ot.datasets.get_data_classif( +Xt, yt = ot.datasets.make_data_classif( 'gaussrot', n_target_samples, theta=theta, nz=noise_level) # one of the target mode changes its variance (no linear mapping) diff --git a/examples/plot_otda_semi_supervised.py b/examples/plot_otda_semi_supervised.py index 7963aef..8a67720 100644 --- a/examples/plot_otda_semi_supervised.py +++ b/examples/plot_otda_semi_supervised.py @@ -29,8 +29,8 @@ import ot n_samples_source = 150 n_samples_target = 150 -Xs, ys = ot.datasets.get_data_classif('3gauss', n_samples_source) -Xt, yt = ot.datasets.get_data_classif('3gauss2', n_samples_target) +Xs, ys = ot.datasets.make_data_classif('3gauss', n_samples_source) +Xt, yt = ot.datasets.make_data_classif('3gauss2', n_samples_target) ############################################################################## |