| 1 |
|
| 2 |
""" |
| 3 |
=================================================== |
| 4 |
OT for domain adaptation on empirical distributions |
| 5 |
=================================================== |
| 6 |
|
| 7 |
This example introduces a domain adaptation in a 2D setting. It explicit |
| 8 |
the problem of domain adaptation and introduces some optimal transport |
| 9 |
approaches to solve it. |
| 10 |
|
| 11 |
Quantities such as optimal couplings, greater coupling coefficients and |
| 12 |
transported samples are represented in order to give a visual understanding |
| 13 |
of what the transport methods are doing. |
| 14 |
""" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
import matplotlib.pylab as pl |
| 24 |
import ot |
| 25 |
import ot.plot |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
n_samples_source = 150 |
| 32 |
n_samples_target = 150 |
| 33 |
|
| 34 |
Xs, ys = ot.datasets.make_data_classif("3gauss", n_samples_source) |
| 35 |
Xt, yt = ot.datasets.make_data_classif("3gauss2", n_samples_target) |
| 36 |
|
| 37 |
|
| 38 |
M = ot.dist(Xs, Xt, metric="sqeuclidean") |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ot_emd = ot.da.EMDTransport() |
| 47 |
ot_emd.fit(Xs=Xs, Xt=Xt) |
| 48 |
|
| 49 |
|
| 50 |
ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1) |
| 51 |
ot_sinkhorn.fit(Xs=Xs, Xt=Xt) |
| 52 |
|
| 53 |
|
| 54 |
ot_lpl1 = ot.da.SinkhornLpl1Transport(reg_e=1e-1, reg_cl=1e0) |
| 55 |
ot_lpl1.fit(Xs=Xs, ys=ys, Xt=Xt) |
| 56 |
|
| 57 |
|
| 58 |
transp_Xs_emd = ot_emd.transform(Xs=Xs) |
| 59 |
transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) |
| 60 |
transp_Xs_lpl1 = ot_lpl1.transform(Xs=Xs) |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
pl.figure(1, figsize=(10, 10)) |
| 68 |
pl.subplot(2, 2, 1) |
| 69 |
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples") |
| 70 |
pl.xticks([]) |
| 71 |
pl.yticks([]) |
| 72 |
pl.legend(loc=0) |
| 73 |
pl.title("Source samples") |
| 74 |
|
| 75 |
pl.subplot(2, 2, 2) |
| 76 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples") |
| 77 |
pl.xticks([]) |
| 78 |
pl.yticks([]) |
| 79 |
pl.legend(loc=0) |
| 80 |
pl.title("Target samples") |
| 81 |
|
| 82 |
pl.subplot(2, 2, 3) |
| 83 |
pl.imshow(M, interpolation="nearest") |
| 84 |
pl.xticks([]) |
| 85 |
pl.yticks([]) |
| 86 |
pl.title("Matrix of pairwise distances") |
| 87 |
pl.tight_layout() |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
pl.figure(2, figsize=(10, 6)) |
| 94 |
|
| 95 |
pl.subplot(2, 3, 1) |
| 96 |
pl.imshow(ot_emd.coupling_, interpolation="nearest") |
| 97 |
pl.xticks([]) |
| 98 |
pl.yticks([]) |
| 99 |
pl.title("Optimal coupling\nEMDTransport") |
| 100 |
|
| 101 |
pl.subplot(2, 3, 2) |
| 102 |
pl.imshow(ot_sinkhorn.coupling_, interpolation="nearest") |
| 103 |
pl.xticks([]) |
| 104 |
pl.yticks([]) |
| 105 |
pl.title("Optimal coupling\nSinkhornTransport") |
| 106 |
|
| 107 |
pl.subplot(2, 3, 3) |
| 108 |
pl.imshow(ot_lpl1.coupling_, interpolation="nearest") |
| 109 |
pl.xticks([]) |
| 110 |
pl.yticks([]) |
| 111 |
pl.title("Optimal coupling\nSinkhornLpl1Transport") |
| 112 |
|
| 113 |
pl.subplot(2, 3, 4) |
| 114 |
ot.plot.plot2D_samples_mat(Xs, Xt, ot_emd.coupling_, c=[0.5, 0.5, 1]) |
| 115 |
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples") |
| 116 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples") |
| 117 |
pl.xticks([]) |
| 118 |
pl.yticks([]) |
| 119 |
pl.title("Main coupling coefficients\nEMDTransport") |
| 120 |
|
| 121 |
pl.subplot(2, 3, 5) |
| 122 |
ot.plot.plot2D_samples_mat(Xs, Xt, ot_sinkhorn.coupling_, c=[0.5, 0.5, 1]) |
| 123 |
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples") |
| 124 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples") |
| 125 |
pl.xticks([]) |
| 126 |
pl.yticks([]) |
| 127 |
pl.title("Main coupling coefficients\nSinkhornTransport") |
| 128 |
|
| 129 |
pl.subplot(2, 3, 6) |
| 130 |
ot.plot.plot2D_samples_mat(Xs, Xt, ot_lpl1.coupling_, c=[0.5, 0.5, 1]) |
| 131 |
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples") |
| 132 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples") |
| 133 |
pl.xticks([]) |
| 134 |
pl.yticks([]) |
| 135 |
pl.title("Main coupling coefficients\nSinkhornLpl1Transport") |
| 136 |
pl.tight_layout() |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
pl.figure(4, figsize=(10, 4)) |
| 145 |
pl.subplot(1, 3, 1) |
| 146 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.5) |
| 147 |
pl.scatter( |
| 148 |
transp_Xs_emd[:, 0], |
| 149 |
transp_Xs_emd[:, 1], |
| 150 |
c=ys, |
| 151 |
marker="+", |
| 152 |
label="Transp samples", |
| 153 |
s=30, |
| 154 |
) |
| 155 |
pl.title("Transported samples\nEmdTransport") |
| 156 |
pl.legend(loc=0) |
| 157 |
pl.xticks([]) |
| 158 |
pl.yticks([]) |
| 159 |
|
| 160 |
pl.subplot(1, 3, 2) |
| 161 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.5) |
| 162 |
pl.scatter( |
| 163 |
transp_Xs_sinkhorn[:, 0], |
| 164 |
transp_Xs_sinkhorn[:, 1], |
| 165 |
c=ys, |
| 166 |
marker="+", |
| 167 |
label="Transp samples", |
| 168 |
s=30, |
| 169 |
) |
| 170 |
pl.title("Transported samples\nSinkhornTransport") |
| 171 |
pl.xticks([]) |
| 172 |
pl.yticks([]) |
| 173 |
|
| 174 |
pl.subplot(1, 3, 3) |
| 175 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.5) |
| 176 |
pl.scatter( |
| 177 |
transp_Xs_lpl1[:, 0], |
| 178 |
transp_Xs_lpl1[:, 1], |
| 179 |
c=ys, |
| 180 |
marker="+", |
| 181 |
label="Transp samples", |
| 182 |
s=30, |
| 183 |
) |
| 184 |
pl.title("Transported samples\nSinkhornLpl1Transport") |
| 185 |
pl.xticks([]) |
| 186 |
pl.yticks([]) |
| 187 |
|
| 188 |
pl.tight_layout() |
| 189 |
pl.show() |