summaryrefslogtreecommitdiff
path: root/examples/plot_stochastic.py
blob: 742f8d9ef83670511dc88476d16dc22e2c12d991 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
==========================
Stochastic examples
==========================

This example is designed to show how to use the stochatic optimization
algorithms for descrete and semicontinous measures from the POT library.

"""

# Author: Kilian Fatras <kilian.fatras@gmail.com>
#
# License: MIT License

import matplotlib.pylab as pl
import numpy as np
import ot
import ot.plot


#############################################################################
# COMPUTE TRANSPORTATION MATRIX FOR SEMI-DUAL PROBLEM
#############################################################################
#############################################################################
# DISCRETE CASE:
#
# Sample two discrete measures for the discrete case
# ---------------------------------------------
#
# Define 2 discrete measures a and b, the points where are defined the source
# and the target measures and finally the cost matrix c.

n_source = 7
n_target = 4
reg = 1
numItermax = 1000

a = ot.utils.unif(n_source)
b = ot.utils.unif(n_target)

rng = np.random.RandomState(0)
X_source = rng.randn(n_source, 2)
Y_target = rng.randn(n_target, 2)
M = ot.dist(X_source, Y_target)

#############################################################################
#
# Call the "SAG" method to find the transportation matrix in the discrete case
# ---------------------------------------------
#
# Define the method "SAG", call ot.solve_semi_dual_entropic and plot the
# results.

method = "SAG"
sag_pi = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method,
                                                numItermax)
print(sag_pi)

#############################################################################
# SEMICONTINOUS CASE:
#
# Sample one general measure a, one discrete measures b for the semicontinous
# case
# ---------------------------------------------
#
# Define one general measure a, one discrete measures b, the points where
# are defined the source and the target measures and finally the cost matrix c.

n_source = 7
n_target = 4
reg = 1
numItermax = 1000
log = True

a = ot.utils.unif(n_source)
b = ot.utils.unif(n_target)

rng = np.random.RandomState(0)
X_source = rng.randn(n_source, 2)
Y_target = rng.randn(n_target, 2)
M = ot.dist(X_source, Y_target)

#############################################################################
#
# Call the "ASGD" method to find the transportation matrix in the semicontinous
# case
# ---------------------------------------------
#
# Define the method "ASGD", call ot.solve_semi_dual_entropic and plot the
# results.

method = "ASGD"
asgd_pi, log_asgd = ot.stochastic.solve_semi_dual_entropic(a, b, M, reg, method,
                                                           numItermax, log=log)
print(log_asgd['alpha'], log_asgd['beta'])
print(asgd_pi)

#############################################################################
#
# Compare the results with the Sinkhorn algorithm
# ---------------------------------------------
#
# Call the Sinkhorn algorithm from POT

sinkhorn_pi = ot.sinkhorn(a, b, M, reg)
print(sinkhorn_pi)


##############################################################################
# PLOT TRANSPORTATION MATRIX
##############################################################################

##############################################################################
# Plot SAG results
# ----------------

pl.figure(4, figsize=(5, 5))
ot.plot.plot1D_mat(a, b, sag_pi, 'semi-dual : OT matrix SAG')
pl.show()


##############################################################################
# Plot ASGD results
# -----------------

pl.figure(4, figsize=(5, 5))
ot.plot.plot1D_mat(a, b, asgd_pi, 'semi-dual : OT matrix ASGD')
pl.show()


##############################################################################
# Plot Sinkhorn results
# ---------------------

pl.figure(4, figsize=(5, 5))
ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn')
pl.show()


#############################################################################
# COMPUTE TRANSPORTATION MATRIX FOR DUAL PROBLEM
#############################################################################
#############################################################################
# SEMICONTINOUS CASE:
#
# Sample one general measure a, one discrete measures b for the semicontinous
# case
# ---------------------------------------------
#
# Define one general measure a, one discrete measures b, the points where
# are defined the source and the target measures and finally the cost matrix c.

n_source = 7
n_target = 4
reg = 1
numItermax = 100000
lr = 0.1
batch_size = 3
log = True

a = ot.utils.unif(n_source)
b = ot.utils.unif(n_target)

rng = np.random.RandomState(0)
X_source = rng.randn(n_source, 2)
Y_target = rng.randn(n_target, 2)
M = ot.dist(X_source, Y_target)

#############################################################################
#
# Call the "SGD" dual method to find the transportation matrix in the
# semicontinous case
# ---------------------------------------------
#
# Call ot.solve_dual_entropic and plot the results.

sgd_dual_pi, log_sgd = ot.stochastic.solve_dual_entropic(a, b, M, reg,
                                                         batch_size, numItermax,
                                                         lr, log=log)
print(log_sgd['alpha'], log_sgd['beta'])
print(sgd_dual_pi)

#############################################################################
#
# Compare the results with the Sinkhorn algorithm
# ---------------------------------------------
#
# Call the Sinkhorn algorithm from POT

sinkhorn_pi = ot.sinkhorn(a, b, M, reg)
print(sinkhorn_pi)

##############################################################################
# Plot  SGD results
# -----------------

pl.figure(4, figsize=(5, 5))
ot.plot.plot1D_mat(a, b, sgd_dual_pi, 'dual : OT matrix SGD')
pl.show()


##############################################################################
# Plot Sinkhorn results
# ---------------------

pl.figure(4, figsize=(5, 5))
ot.plot.plot1D_mat(a, b, sinkhorn_pi, 'OT matrix Sinkhorn')
pl.show()