Debian Codegraph @

examples/domain-adaptation/plot_otda_semi_supervised.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
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 # Authors: Remi Flamary <[email protected]>
17 # Stanislas Chambon <[email protected]>
18 #
19 # License: MIT License
20
21 # sphinx_gallery_thumbnail_number = 3
22
23 import matplotlib.pylab as pl
24 import ot
25
26
27 ##############################################################################
28 # Generate data
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 # Transport source samples onto target samples
40 # --------------------------------------------
41
42
43 # unsupervised domain adaptation
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 # semi-supervised domain adaptation
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 # semi supervised DA uses available labeled target samples to modify the cost
54 # matrix involved in the OT problem. The cost of transporting a source sample
55 # of class A onto a target sample of class B != A is set to infinite, or a
56 # very large value
57
58 # note that in the present case we consider that all the target samples are
59 # labeled. For daily applications, some target sample might not have labels,
60 # in this case the element of yt corresponding to these samples should be
61 # filled with -1.
62
63 # Warning: we recall that -1 cannot be used as a class label
64
65
66 ##############################################################################
67 # Fig 1 : plots source and target samples + matrix of pairwise distance
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 # the optimal coupling in the semi-supervised DA case will exhibit " shape
100 # similar" to the cost matrix, (block diagonal matrix)
101
102
103 ##############################################################################
104 # Fig 2 : plots optimal couplings for the different methods
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 # Fig 3 : plot transported samples
126 # --------------------------------
127
128 # display transported samples
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()