Debian Codegraph @

examples/domain-adaptation/plot_otda_jcpot.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ================================
4 OT for multi-source target shift
5 ================================
6
7 This example introduces a target shift problem with two 2D source and 1 target domain.
8
9 """
10
11 # Authors: Remi Flamary <[email protected]>
12 # Ievgen Redko <[email protected]>
13 #
14 # License: MIT License
15
16 import pylab as pl
17 import numpy as np
18 import ot
19 from ot.datasets import make_data_classif
20
21 ##############################################################################
22 # Generate data
23 # -------------
24 n = 50
25 sigma = 0.3
26 np.random.seed(1985)
27
28 p1 = 0.2
29 dec1 = [0, 2]
30
31 p2 = 0.9
32 dec2 = [0, -2]
33
34 pt = 0.4
35 dect = [4, 0]
36
37 xs1, ys1 = make_data_classif("2gauss_prop", n, nz=sigma, p=p1, bias=dec1)
38 xs2, ys2 = make_data_classif("2gauss_prop", n + 1, nz=sigma, p=p2, bias=dec2)
39 xt, yt = make_data_classif("2gauss_prop", n, nz=sigma, p=pt, bias=dect)
40
41 all_Xr = [xs1, xs2]
42 all_Yr = [ys1, ys2]
43 # %%
44
45 da = 1.5
46
47
48 def plot_ax(dec, name):
49 pl.plot([dec[0], dec[0]], [dec[1] - da, dec[1] + da], "k", alpha=0.5)
50 pl.plot([dec[0] - da, dec[0] + da], [dec[1], dec[1]], "k", alpha=0.5)
51 pl.text(dec[0] - 0.5, dec[1] + 2, name)
52
53
54 ##############################################################################
55 # Fig 1 : plots source and target samples
56 # ---------------------------------------
57
58 pl.figure(1)
59 pl.clf()
60 plot_ax(dec1, "Source 1")
61 plot_ax(dec2, "Source 2")
62 plot_ax(dect, "Target")
63 pl.scatter(
64 xs1[:, 0],
65 xs1[:, 1],
66 c=ys1,
67 s=35,
68 marker="x",
69 cmap="Set1",
70 vmax=9,
71 label="Source 1 ({:1.2f}, {:1.2f})".format(1 - p1, p1),
72 )
73 pl.scatter(
74 xs2[:, 0],
75 xs2[:, 1],
76 c=ys2,
77 s=35,
78 marker="+",
79 cmap="Set1",
80 vmax=9,
81 label="Source 2 ({:1.2f}, {:1.2f})".format(1 - p2, p2),
82 )
83 pl.scatter(
84 xt[:, 0],
85 xt[:, 1],
86 c=yt,
87 s=35,
88 marker="o",
89 cmap="Set1",
90 vmax=9,
91 label="Target ({:1.2f}, {:1.2f})".format(1 - pt, pt),
92 )
93 pl.title("Data")
94
95 pl.legend()
96 pl.axis("equal")
97 pl.axis("off")
98
99 ##############################################################################
100 # Instantiate Sinkhorn transport algorithm and fit them for all source domains
101 # ----------------------------------------------------------------------------
102 ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1, metric="sqeuclidean")
103
104
105 def print_G(G, xs, ys, xt):
106 for i in range(G.shape[0]):
107 for j in range(G.shape[1]):
108 if G[i, j] > 5e-4:
109 if ys[i]:
110 c = "b"
111 else:
112 c = "r"
113 pl.plot([xs[i, 0], xt[j, 0]], [xs[i, 1], xt[j, 1]], c, alpha=0.2)
114
115
116 ##############################################################################
117 # Fig 2 : plot optimal couplings and transported samples
118 # ------------------------------------------------------
119 pl.figure(2)
120 pl.clf()
121 plot_ax(dec1, "Source 1")
122 plot_ax(dec2, "Source 2")
123 plot_ax(dect, "Target")
124 print_G(ot_sinkhorn.fit(Xs=xs1, Xt=xt).coupling_, xs1, ys1, xt)
125 print_G(ot_sinkhorn.fit(Xs=xs2, Xt=xt).coupling_, xs2, ys2, xt)
126 pl.scatter(xs1[:, 0], xs1[:, 1], c=ys1, s=35, marker="x", cmap="Set1", vmax=9)
127 pl.scatter(xs2[:, 0], xs2[:, 1], c=ys2, s=35, marker="+", cmap="Set1", vmax=9)
128 pl.scatter(xt[:, 0], xt[:, 1], c=yt, s=35, marker="o", cmap="Set1", vmax=9)
129
130 pl.plot([], [], "r", alpha=0.2, label="Mass from Class 1")
131 pl.plot([], [], "b", alpha=0.2, label="Mass from Class 2")
132
133 pl.title("Independent OT")
134
135 pl.legend()
136 pl.axis("equal")
137 pl.axis("off")
138
139 ##############################################################################
140 # Instantiate JCPOT adaptation algorithm and fit it
141 # ----------------------------------------------------------------------------
142 otda = ot.da.JCPOTTransport(
143 reg_e=1, max_iter=1000, metric="sqeuclidean", tol=1e-9, verbose=True, log=True
144 )
145 otda.fit(all_Xr, all_Yr, xt)
146
147 ws1 = otda.proportions_.dot(otda.log_["D2"][0])
148 ws2 = otda.proportions_.dot(otda.log_["D2"][1])
149
150 pl.figure(3)
151 pl.clf()
152 plot_ax(dec1, "Source 1")
153 plot_ax(dec2, "Source 2")
154 plot_ax(dect, "Target")
155 print_G(ot.bregman.sinkhorn(ws1, [], otda.log_["M"][0], reg=1e-1), xs1, ys1, xt)
156 print_G(ot.bregman.sinkhorn(ws2, [], otda.log_["M"][1], reg=1e-1), xs2, ys2, xt)
157 pl.scatter(xs1[:, 0], xs1[:, 1], c=ys1, s=35, marker="x", cmap="Set1", vmax=9)
158 pl.scatter(xs2[:, 0], xs2[:, 1], c=ys2, s=35, marker="+", cmap="Set1", vmax=9)
159 pl.scatter(xt[:, 0], xt[:, 1], c=yt, s=35, marker="o", cmap="Set1", vmax=9)
160
161 pl.plot([], [], "r", alpha=0.2, label="Mass from Class 1")
162 pl.plot([], [], "b", alpha=0.2, label="Mass from Class 2")
163
164 pl.title(
165 "OT with prop estimation ({:1.3f},{:1.3f})".format(
166 otda.proportions_[0], otda.proportions_[1]
167 )
168 )
169
170 pl.legend()
171 pl.axis("equal")
172 pl.axis("off")
173
174 ##############################################################################
175 # Run oracle transport algorithm with known proportions
176 # ----------------------------------------------------------------------------
177 h_res = np.array([1 - pt, pt])
178
179 ws1 = h_res.dot(otda.log_["D2"][0])
180 ws2 = h_res.dot(otda.log_["D2"][1])
181
182 pl.figure(4)
183 pl.clf()
184 plot_ax(dec1, "Source 1")
185 plot_ax(dec2, "Source 2")
186 plot_ax(dect, "Target")
187 print_G(ot.bregman.sinkhorn(ws1, [], otda.log_["M"][0], reg=1e-1), xs1, ys1, xt)
188 print_G(ot.bregman.sinkhorn(ws2, [], otda.log_["M"][1], reg=1e-1), xs2, ys2, xt)
189 pl.scatter(xs1[:, 0], xs1[:, 1], c=ys1, s=35, marker="x", cmap="Set1", vmax=9)
190 pl.scatter(xs2[:, 0], xs2[:, 1], c=ys2, s=35, marker="+", cmap="Set1", vmax=9)
191 pl.scatter(xt[:, 0], xt[:, 1], c=yt, s=35, marker="o", cmap="Set1", vmax=9)
192
193 pl.plot([], [], "r", alpha=0.2, label="Mass from Class 1")
194 pl.plot([], [], "b", alpha=0.2, label="Mass from Class 2")
195
196 pl.title("OT with known proportion ({:1.1f},{:1.1f})".format(h_res[0], h_res[1]))
197
198 pl.legend()
199 pl.axis("equal")
200 pl.axis("off")
201 pl.show()