-
Notifications
You must be signed in to change notification settings - Fork 528
add stochastic methods #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c8eda44
add problems solved in doc
e2d06ef
add problems solved in doc
18f9242
PEP8
3617b42
PEP8
055417e
pep8
74cfe5a
add sgd
e068b58
pep8
52134e9
change grad function names
7073e41
remove if in test and cleaned code
6777ffd
gave better step size ASGD & SAG
af5f726
fixed bug
e8cf3cc
pep8
9fecd51
fix math operator and log bugs
968ad58
add stochastic to docs
208ff46
fix stochastic to doc
b4bc861
Merge branch 'master' into stochastic_OT
kilianFatras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
""" | ||
========================== | ||
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 | ||
############################################################################# | ||
print("------------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 | ||
############################################################################# | ||
print("------------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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also
import ot.plot
because it is not imported by default