Debian Codegraph @

examples/unbalanced-partial/plot_partial_wass_and_gromov.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ==================================================
4 Partial Wasserstein and Gromov-Wasserstein example
5 ==================================================
6
7 This example is designed to show how to use the Partial (Gromov-)Wasserstein
8 distance computation in POT.
9 """
10
11 # Author: Laetitia Chapel <[email protected]>
12 # License: MIT License
13
14 # sphinx_gallery_thumbnail_number = 2
15
16 # necessary for 3d plot even if not used
17 from mpl_toolkits.mplot3d import Axes3D # noqa
18 import scipy as sp
19 import numpy as np
20 import matplotlib.pylab as pl
21 import ot
22
23
24 #############################################################################
25 #
26 # Sample two 2D Gaussian distributions and plot them
27 # --------------------------------------------------
28 #
29 # For demonstration purpose, we sample two Gaussian distributions in 2-d
30 # spaces and add some random noise.
31
32
33 n_samples = 20 # nb samples (gaussian)
34 n_noise = 20 # nb of samples (noise)
35
36 mu = np.array([0, 0])
37 cov = np.array([[1, 0], [0, 2]])
38
39 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov)
40 xs = np.append(xs, (np.random.rand(n_noise, 2) + 1) * 4).reshape((-1, 2))
41 xt = ot.datasets.make_2D_samples_gauss(n_samples, mu, cov)
42 xt = np.append(xt, (np.random.rand(n_noise, 2) + 1) * -3).reshape((-1, 2))
43
44 M = sp.spatial.distance.cdist(xs, xt)
45
46 fig = pl.figure()
47 ax1 = fig.add_subplot(131)
48 ax1.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples")
49 ax2 = fig.add_subplot(132)
50 ax2.scatter(xt[:, 0], xt[:, 1], color="r")
51 ax3 = fig.add_subplot(133)
52 ax3.imshow(M)
53 pl.show()
54
55 #############################################################################
56 #
57 # Compute partial Wasserstein plans and distance
58 # ----------------------------------------------
59
60 p = ot.unif(n_samples + n_noise)
61 q = ot.unif(n_samples + n_noise)
62
63 w0, log0 = ot.partial.partial_wasserstein(p, q, M, m=0.5, log=True)
64 w, log = ot.partial.entropic_partial_wasserstein(p, q, M, reg=0.1, m=0.5, log=True)
65
66 print("Partial Wasserstein distance (m = 0.5): " + str(log0["partial_w_dist"]))
67 print("Entropic partial Wasserstein distance (m = 0.5): " + str(log["partial_w_dist"]))
68
69 pl.figure(1, (10, 5))
70 pl.subplot(1, 2, 1)
71 pl.imshow(w0, cmap="jet")
72 pl.title("Partial Wasserstein")
73 pl.subplot(1, 2, 2)
74 pl.imshow(w, cmap="jet")
75 pl.title("Entropic partial Wasserstein")
76 pl.show()
77
78
79 #############################################################################
80 #
81 # Sample one 2D and 3D Gaussian distributions and plot them
82 # ---------------------------------------------------------
83 #
84 # The Gromov-Wasserstein distance allows to compute distances with samples that
85 # do not belong to the same metric space. For demonstration purpose, we sample
86 # two Gaussian distributions in 2- and 3-dimensional spaces.
87
88 n_samples = 20 # nb samples
89 n_noise = 10 # nb of samples (noise)
90
91 p = ot.unif(n_samples + n_noise)
92 q = ot.unif(n_samples + n_noise)
93
94 mu_s = np.array([0, 0])
95 cov_s = np.array([[1, 0], [0, 1]])
96
97 mu_t = np.array([0, 0, 0])
98 cov_t = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
99
100
101 xs = ot.datasets.make_2D_samples_gauss(n_samples, mu_s, cov_s)
102 xs = np.concatenate((xs, ((np.random.rand(n_noise, 2) + 1) * 4)), axis=0)
103 P = sp.linalg.sqrtm(cov_t)
104 xt = np.random.randn(n_samples, 3).dot(P) + mu_t
105 xt = np.concatenate((xt, ((np.random.rand(n_noise, 3) + 1) * 10)), axis=0)
106
107 fig = pl.figure()
108 ax1 = fig.add_subplot(121)
109 ax1.plot(xs[:, 0], xs[:, 1], "+b", label="Source samples")
110 ax2 = fig.add_subplot(122, projection="3d")
111 ax2.scatter(xt[:, 0], xt[:, 1], xt[:, 2], color="r")
112 pl.show()
113
114
115 #############################################################################
116 #
117 # Compute partial Gromov-Wasserstein plans and distance
118 # -----------------------------------------------------
119
120 C1 = sp.spatial.distance.cdist(xs, xs)
121 C2 = sp.spatial.distance.cdist(xt, xt)
122
123 # transport 100% of the mass
124 print("------m = 1")
125 m = 1
126 res0, log0 = ot.gromov.partial_gromov_wasserstein(C1, C2, p, q, m=m, log=True)
127 res, log = ot.gromov.entropic_partial_gromov_wasserstein(
128 C1, C2, p, q, 10, m=m, log=True, verbose=True
129 )
130
131 print("Wasserstein distance (m = 1): " + str(log0["partial_gw_dist"]))
132 print("Entropic Wasserstein distance (m = 1): " + str(log["partial_gw_dist"]))
133
134 pl.figure(1, (10, 5))
135 pl.title("mass to be transported m = 1")
136 pl.subplot(1, 2, 1)
137 pl.imshow(res0, cmap="jet")
138 pl.title("Gromov-Wasserstein")
139 pl.subplot(1, 2, 2)
140 pl.imshow(res, cmap="jet")
141 pl.title("Entropic Gromov-Wasserstein")
142 pl.show()
143
144 # transport 2/3 of the mass
145 print("------m = 2/3")
146 m = 2 / 3
147 res0, log0 = ot.gromov.partial_gromov_wasserstein(
148 C1, C2, p, q, m=m, log=True, verbose=True
149 )
150 res, log = ot.gromov.entropic_partial_gromov_wasserstein(
151 C1, C2, p, q, 10, m=m, log=True, verbose=True
152 )
153
154 print("Partial Wasserstein distance (m = 2/3): " + str(log0["partial_gw_dist"]))
155 print("Entropic partial Wasserstein distance (m = 2/3): " + str(log["partial_gw_dist"]))
156
157 pl.figure(1, (10, 5))
158 pl.title("mass to be transported m = 2/3")
159 pl.subplot(1, 2, 1)
160 pl.imshow(res0, cmap="jet")
161 pl.title("Partial Gromov-Wasserstein")
162 pl.subplot(1, 2, 2)
163 pl.imshow(res, cmap="jet")
164 pl.title("Entropic partial Gromov-Wasserstein")
165 pl.show()