Debian Codegraph @

examples/domain-adaptation/plot_otda_mapping.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 ===========================================
4 OT mapping estimation for domain adaptation
5 ===========================================
6
7 This example presents how to use MappingTransport to estimate at the same
8 time both the coupling transport and approximate the transport map with either
9 a linear or a kernelized mapping as introduced in [8].
10
11 [8] M. Perrot, N. Courty, R. Flamary, A. Habrard,
12 "Mapping estimation for discrete optimal transport",
13 Neural Information Processing Systems (NIPS), 2016.
14 """
15
16 # Authors: Remi Flamary <[email protected]>
17 # Stanislas Chambon <[email protected]>
18 #
19 # License: MIT License
20
21 # sphinx_gallery_thumbnail_number = 2
22
23 import numpy as np
24 import matplotlib.pylab as pl
25 import ot
26
27
28 ##############################################################################
29 # Generate data
30 # -------------
31
32 n_source_samples = 100
33 n_target_samples = 100
34 theta = 2 * np.pi / 20
35 noise_level = 0.1
36
37 Xs, ys = ot.datasets.make_data_classif("gaussrot", n_source_samples, nz=noise_level)
38 Xs_new, _ = ot.datasets.make_data_classif("gaussrot", n_source_samples, nz=noise_level)
39 Xt, yt = ot.datasets.make_data_classif(
40 "gaussrot", n_target_samples, theta=theta, nz=noise_level
41 )
42
43 # one of the target mode changes its variance (no linear mapping)
44 Xt[yt == 2] *= 3
45 Xt = Xt + 4
46
47 ##############################################################################
48 # Plot data
49 # ---------
50
51 pl.figure(1, (10, 5))
52 pl.clf()
53 pl.scatter(Xs[:, 0], Xs[:, 1], c=ys, marker="+", label="Source samples")
54 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples")
55 pl.legend(loc=0)
56 pl.title("Source and target distributions")
57
58
59 ##############################################################################
60 # Instantiate the different transport algorithms and fit them
61 # -----------------------------------------------------------
62
63 # MappingTransport with linear kernel
64 ot_mapping_linear = ot.da.MappingTransport(
65 kernel="linear", mu=1e0, eta=1e-8, bias=True, max_iter=20, verbose=True
66 )
67
68 ot_mapping_linear.fit(Xs=Xs, Xt=Xt)
69
70 # for original source samples, transform applies barycentric mapping
71 transp_Xs_linear = ot_mapping_linear.transform(Xs=Xs)
72
73 # for out of source samples, transform applies the linear mapping
74 transp_Xs_linear_new = ot_mapping_linear.transform(Xs=Xs_new)
75
76
77 # MappingTransport with gaussian kernel
78 ot_mapping_gaussian = ot.da.MappingTransport(
79 kernel="gaussian", eta=1e-5, mu=1e-1, bias=True, sigma=1, max_iter=10, verbose=True
80 )
81 ot_mapping_gaussian.fit(Xs=Xs, Xt=Xt)
82
83 # for original source samples, transform applies barycentric mapping
84 transp_Xs_gaussian = ot_mapping_gaussian.transform(Xs=Xs)
85
86 # for out of source samples, transform applies the gaussian mapping
87 transp_Xs_gaussian_new = ot_mapping_gaussian.transform(Xs=Xs_new)
88
89
90 ##############################################################################
91 # Plot transported samples
92 # ------------------------
93
94 pl.figure(2)
95 pl.clf()
96 pl.subplot(2, 2, 1)
97 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.2)
98 pl.scatter(
99 transp_Xs_linear[:, 0],
100 transp_Xs_linear[:, 1],
101 c=ys,
102 marker="+",
103 label="Mapped source samples",
104 )
105 pl.title("Bary. mapping (linear)")
106 pl.legend(loc=0)
107
108 pl.subplot(2, 2, 2)
109 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.2)
110 pl.scatter(
111 transp_Xs_linear_new[:, 0],
112 transp_Xs_linear_new[:, 1],
113 c=ys,
114 marker="+",
115 label="Learned mapping",
116 )
117 pl.title("Estim. mapping (linear)")
118
119 pl.subplot(2, 2, 3)
120 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.2)
121 pl.scatter(
122 transp_Xs_gaussian[:, 0],
123 transp_Xs_gaussian[:, 1],
124 c=ys,
125 marker="+",
126 label="barycentric mapping",
127 )
128 pl.title("Bary. mapping (kernel)")
129
130 pl.subplot(2, 2, 4)
131 pl.scatter(Xt[:, 0], Xt[:, 1], c=yt, marker="o", label="Target samples", alpha=0.2)
132 pl.scatter(
133 transp_Xs_gaussian_new[:, 0],
134 transp_Xs_gaussian_new[:, 1],
135 c=ys,
136 marker="+",
137 label="Learned mapping",
138 )
139 pl.title("Estim. mapping (kernel)")
140 pl.tight_layout()
141
142 pl.show()