| 1 |
|
| 2 |
""" |
| 3 |
====================================================== |
| 4 |
OT with Laplacian regularization for domain adaptation |
| 5 |
====================================================== |
| 6 |
|
| 7 |
This example introduces a domain adaptation in a 2D setting and OTDA |
| 8 |
approach with Laplacian regularization. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import matplotlib.pylab as pl |
| 17 |
import ot |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
n_source_samples = 150 |
| 24 |
n_target_samples = 150 |
| 25 |
|
| 26 |
Xs, ys = ot.datasets.make_data_classif("3gauss", n_source_samples) |
| 27 |
Xt, yt = ot.datasets.make_data_classif("3gauss2", n_target_samples) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
ot_emd = ot.da.EMDTransport() |
| 36 |
ot_emd.fit(Xs=Xs, Xt=Xt) |
| 37 |
|
| 38 |
|
| 39 |
ot_sinkhorn = ot.da.SinkhornTransport(reg_e=0.01) |
| 40 |
ot_sinkhorn.fit(Xs=Xs, Xt=Xt) |
| 41 |
|
| 42 |
|
| 43 |
ot_emd_laplace = ot.da.EMDLaplaceTransport(reg_lap=100, reg_src=1) |
| 44 |
ot_emd_laplace.fit(Xs=Xs, Xt=Xt) |
| 45 |
|
| 46 |
|
| 47 |
transp_Xs_emd = ot_emd.transform(Xs=Xs) |
| 48 |
transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=Xs) |
| 49 |
transp_Xs_emd_laplace = ot_emd_laplace.transform(Xs=Xs) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
pl.figure(1, figsize=(10, 5)) |
| 56 |
pl.subplot(1, 2, 1) |
| 57 |
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples") |
| 58 |
pl.xticks([]) |
| 59 |
pl.yticks([]) |
| 60 |
pl.legend(loc=0) |
| 61 |
pl.title("Source samples") |
| 62 |
|
| 63 |
pl.subplot(1, 2, 2) |
| 64 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples") |
| 65 |
pl.xticks([]) |
| 66 |
pl.yticks([]) |
| 67 |
pl.legend(loc=0) |
| 68 |
pl.title("Target samples") |
| 69 |
pl.tight_layout() |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
param_img = {"interpolation": "nearest"} |
| 77 |
|
| 78 |
pl.figure(2, figsize=(15, 8)) |
| 79 |
pl.subplot(2, 3, 1) |
| 80 |
pl.imshow(ot_emd.coupling_, **param_img) |
| 81 |
pl.xticks([]) |
| 82 |
pl.yticks([]) |
| 83 |
pl.title("Optimal coupling\nEMDTransport") |
| 84 |
|
| 85 |
pl.figure(2, figsize=(15, 8)) |
| 86 |
pl.subplot(2, 3, 2) |
| 87 |
pl.imshow(ot_sinkhorn.coupling_, **param_img) |
| 88 |
pl.xticks([]) |
| 89 |
pl.yticks([]) |
| 90 |
pl.title("Optimal coupling\nSinkhornTransport") |
| 91 |
|
| 92 |
pl.subplot(2, 3, 3) |
| 93 |
pl.imshow(ot_emd_laplace.coupling_, **param_img) |
| 94 |
pl.xticks([]) |
| 95 |
pl.yticks([]) |
| 96 |
pl.title("Optimal coupling\nEMDLaplaceTransport") |
| 97 |
|
| 98 |
pl.subplot(2, 3, 4) |
| 99 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3) |
| 100 |
pl.scatter( |
| 101 |
transp_Xs_emd[:, 0], |
| 102 |
transp_Xs_emd[:, 1], |
| 103 |
c=ys, |
| 104 |
marker="+", |
| 105 |
label="Transp samples", |
| 106 |
s=30, |
| 107 |
) |
| 108 |
pl.xticks([]) |
| 109 |
pl.yticks([]) |
| 110 |
pl.title("Transported samples\nEmdTransport") |
| 111 |
pl.legend(loc="lower left") |
| 112 |
|
| 113 |
pl.subplot(2, 3, 5) |
| 114 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3) |
| 115 |
pl.scatter( |
| 116 |
transp_Xs_sinkhorn[:, 0], |
| 117 |
transp_Xs_sinkhorn[:, 1], |
| 118 |
c=ys, |
| 119 |
marker="+", |
| 120 |
label="Transp samples", |
| 121 |
s=30, |
| 122 |
) |
| 123 |
pl.xticks([]) |
| 124 |
pl.yticks([]) |
| 125 |
pl.title("Transported samples\nSinkhornTransport") |
| 126 |
|
| 127 |
pl.subplot(2, 3, 6) |
| 128 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.3) |
| 129 |
pl.scatter( |
| 130 |
transp_Xs_emd_laplace[:, 0], |
| 131 |
transp_Xs_emd_laplace[:, 1], |
| 132 |
c=ys, |
| 133 |
marker="+", |
| 134 |
label="Transp samples", |
| 135 |
s=30, |
| 136 |
) |
| 137 |
pl.xticks([]) |
| 138 |
pl.yticks([]) |
| 139 |
pl.title("Transported samples\nEMDLaplaceTransport") |
| 140 |
pl.tight_layout() |
| 141 |
|
| 142 |
pl.show() |