| 1 |
|
| 2 |
""" |
| 3 |
============================================ |
| 4 |
OTDA unsupervised vs semi-supervised setting |
| 5 |
============================================ |
| 6 |
|
| 7 |
This example introduces a semi supervised domain adaptation in a 2D setting. |
| 8 |
It explicit the problem of semi supervised domain adaptation and introduces |
| 9 |
some optimal transport 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 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
ot_sinkhorn_un = ot.da.SinkhornTransport(reg_e=1e-1) |
| 45 |
ot_sinkhorn_un.fit(Xs=Xs, Xt=Xt) |
| 46 |
transp_Xs_sinkhorn_un = ot_sinkhorn_un.transform(Xs=Xs) |
| 47 |
|
| 48 |
|
| 49 |
ot_sinkhorn_semi = ot.da.SinkhornTransport(reg_e=1e-1) |
| 50 |
ot_sinkhorn_semi.fit(Xs=Xs, Xt=Xt, ys=ys, yt=yt) |
| 51 |
transp_Xs_sinkhorn_semi = ot_sinkhorn_semi.transform(Xs=Xs) |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
pl.figure(1, figsize=(10, 10)) |
| 71 |
pl.subplot(2, 2, 1) |
| 72 |
pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples") |
| 73 |
pl.xticks([]) |
| 74 |
pl.yticks([]) |
| 75 |
pl.legend(loc=0) |
| 76 |
pl.title("Source samples") |
| 77 |
|
| 78 |
pl.subplot(2, 2, 2) |
| 79 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples") |
| 80 |
pl.xticks([]) |
| 81 |
pl.yticks([]) |
| 82 |
pl.legend(loc=0) |
| 83 |
pl.title("Target samples") |
| 84 |
|
| 85 |
pl.subplot(2, 2, 3) |
| 86 |
pl.imshow(ot_sinkhorn_un.cost_, interpolation="nearest") |
| 87 |
pl.xticks([]) |
| 88 |
pl.yticks([]) |
| 89 |
pl.title("Cost matrix - unsupervised DA") |
| 90 |
|
| 91 |
pl.subplot(2, 2, 4) |
| 92 |
pl.imshow(ot_sinkhorn_semi.cost_, interpolation="nearest") |
| 93 |
pl.xticks([]) |
| 94 |
pl.yticks([]) |
| 95 |
pl.title("Cost matrix - semi-supervised DA") |
| 96 |
|
| 97 |
pl.tight_layout() |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
pl.figure(2, figsize=(8, 4)) |
| 108 |
|
| 109 |
pl.subplot(1, 2, 1) |
| 110 |
pl.imshow(ot_sinkhorn_un.coupling_, interpolation="nearest") |
| 111 |
pl.xticks([]) |
| 112 |
pl.yticks([]) |
| 113 |
pl.title("Optimal coupling\nUnsupervised DA") |
| 114 |
|
| 115 |
pl.subplot(1, 2, 2) |
| 116 |
pl.imshow(ot_sinkhorn_semi.coupling_, interpolation="nearest") |
| 117 |
pl.xticks([]) |
| 118 |
pl.yticks([]) |
| 119 |
pl.title("Optimal coupling\nSemi-supervised DA") |
| 120 |
|
| 121 |
pl.tight_layout() |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pl.figure(4, figsize=(8, 4)) |
| 130 |
pl.subplot(1, 2, 1) |
| 131 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.5) |
| 132 |
pl.scatter( |
| 133 |
transp_Xs_sinkhorn_un[:, 0], |
| 134 |
transp_Xs_sinkhorn_un[:, 1], |
| 135 |
c=ys, |
| 136 |
marker="+", |
| 137 |
label="Transp samples", |
| 138 |
s=30, |
| 139 |
) |
| 140 |
pl.title("Transported samples\nEmdTransport") |
| 141 |
pl.legend(loc=0) |
| 142 |
pl.xticks([]) |
| 143 |
pl.yticks([]) |
| 144 |
|
| 145 |
pl.subplot(1, 2, 2) |
| 146 |
pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.5) |
| 147 |
pl.scatter( |
| 148 |
transp_Xs_sinkhorn_semi[:, 0], |
| 149 |
transp_Xs_sinkhorn_semi[:, 1], |
| 150 |
c=ys, |
| 151 |
marker="+", |
| 152 |
label="Transp samples", |
| 153 |
s=30, |
| 154 |
) |
| 155 |
pl.title("Transported samples\nSinkhornTransport") |
| 156 |
pl.xticks([]) |
| 157 |
pl.yticks([]) |
| 158 |
|
| 159 |
pl.tight_layout() |
| 160 |
pl.show() |