本文整理汇总了Python中ot.sinkhorn方法的典型用法代码示例。如果您正苦于以下问题:Python ot.sinkhorn方法的具体用法?Python ot.sinkhorn怎么用?Python ot.sinkhorn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ot
的用法示例。
在下文中一共展示了ot.sinkhorn方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sinkhorn
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_sinkhorn():
# test sinkhorn
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
# check constratints
np.testing.assert_allclose(
u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(
u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
示例2: test_sinkhorn_variants
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_sinkhorn_variants():
# test sinkhorn
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G0 = ot.sinkhorn(u, u, M, 1, method='sinkhorn', stopThr=1e-10)
Gs = ot.sinkhorn(u, u, M, 1, method='sinkhorn_stabilized', stopThr=1e-10)
Ges = ot.sinkhorn(
u, u, M, 1, method='sinkhorn_epsilon_scaling', stopThr=1e-10)
G_green = ot.sinkhorn(u, u, M, 1, method='greenkhorn', stopThr=1e-10)
# check values
np.testing.assert_allclose(G0, Gs, atol=1e-05)
np.testing.assert_allclose(G0, Ges, atol=1e-05)
np.testing.assert_allclose(G0, G_green, atol=1e-5)
print(G0, G_green)
示例3: test_sinkhorn_variants_log
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_sinkhorn_variants_log():
# test sinkhorn
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G0, log0 = ot.sinkhorn(u, u, M, 1, method='sinkhorn', stopThr=1e-10, log=True)
Gs, logs = ot.sinkhorn(u, u, M, 1, method='sinkhorn_stabilized', stopThr=1e-10, log=True)
Ges, loges = ot.sinkhorn(
u, u, M, 1, method='sinkhorn_epsilon_scaling', stopThr=1e-10, log=True)
G_green, loggreen = ot.sinkhorn(u, u, M, 1, method='greenkhorn', stopThr=1e-10, log=True)
# check values
np.testing.assert_allclose(G0, Gs, atol=1e-05)
np.testing.assert_allclose(G0, Ges, atol=1e-05)
np.testing.assert_allclose(G0, G_green, atol=1e-5)
print(G0, G_green)
示例4: test_barycenter_stabilization
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_barycenter_stabilization():
n_bins = 100 # nb bins
# Gaussian distributions
a1 = ot.datasets.make_1D_gauss(n_bins, m=30, s=10) # m= mean, s= std
a2 = ot.datasets.make_1D_gauss(n_bins, m=40, s=10)
# creating matrix A containing all distributions
A = np.vstack((a1, a2)).T
# loss matrix + normalization
M = ot.utils.dist0(n_bins)
M /= M.max()
alpha = 0.5 # 0<=alpha<=1
weights = np.array([1 - alpha, alpha])
# wasserstein
reg = 1e-2
bar_stable = ot.bregman.barycenter(A, M, reg, weights,
method="sinkhorn_stabilized",
stopThr=1e-8, verbose=True)
bar = ot.bregman.barycenter(A, M, reg, weights, method="sinkhorn",
stopThr=1e-8, verbose=True)
np.testing.assert_allclose(bar, bar_stable)
示例5: test_empirical_sinkhorn_divergence
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_empirical_sinkhorn_divergence():
# Test sinkhorn divergence
n = 10
a = ot.unif(n)
b = ot.unif(n)
X_s = np.reshape(np.arange(n), (n, 1))
X_t = np.reshape(np.arange(0, n * 2, 2), (n, 1))
M = ot.dist(X_s, X_t)
M_s = ot.dist(X_s, X_s)
M_t = ot.dist(X_t, X_t)
emp_sinkhorn_div = ot.bregman.empirical_sinkhorn_divergence(X_s, X_t, 1)
sinkhorn_div = (ot.sinkhorn2(a, b, M, 1) - 1 / 2 * ot.sinkhorn2(a, a, M_s, 1) - 1 / 2 * ot.sinkhorn2(b, b, M_t, 1))
emp_sinkhorn_div_log, log_es = ot.bregman.empirical_sinkhorn_divergence(X_s, X_t, 1, log=True)
sink_div_log_ab, log_s_ab = ot.sinkhorn2(a, b, M, 1, log=True)
sink_div_log_a, log_s_a = ot.sinkhorn2(a, a, M_s, 1, log=True)
sink_div_log_b, log_s_b = ot.sinkhorn2(b, b, M_t, 1, log=True)
sink_div_log = sink_div_log_ab - 1 / 2 * (sink_div_log_a + sink_div_log_b)
# check constratints
np.testing.assert_allclose(
emp_sinkhorn_div, sinkhorn_div, atol=1e-05) # cf conv emp sinkhorn
np.testing.assert_allclose(
emp_sinkhorn_div_log, sink_div_log, atol=1e-05) # cf conv emp sinkhorn
示例6: test_stabilized_vs_sinkhorn_multidim
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_stabilized_vs_sinkhorn_multidim():
# test if stable version matches sinkhorn
# for multidimensional inputs
n = 100
# Gaussian distributions
a = ot.datasets.make_1D_gauss(n, m=20, s=5) # m= mean, s= std
b1 = ot.datasets.make_1D_gauss(n, m=60, s=8)
b2 = ot.datasets.make_1D_gauss(n, m=30, s=4)
# creating matrix A containing all distributions
b = np.vstack((b1, b2)).T
M = ot.utils.dist0(n)
M /= np.median(M)
epsilon = 0.1
G, log = ot.bregman.sinkhorn(a, b, M, reg=epsilon,
method="sinkhorn_stabilized",
log=True)
G2, log2 = ot.bregman.sinkhorn(a, b, M, epsilon,
method="sinkhorn", log=True)
np.testing.assert_allclose(G, G2)
示例7: test_screenkhorn
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_screenkhorn():
# test screenkhorn
rng = np.random.RandomState(0)
n = 100
a = ot.unif(n)
b = ot.unif(n)
x = rng.randn(n, 2)
M = ot.dist(x, x)
# sinkhorn
G_sink = ot.sinkhorn(a, b, M, 1e-03)
# screenkhorn
G_screen = ot.bregman.screenkhorn(a, b, M, 1e-03, uniform=True, verbose=True)
# check marginals
np.testing.assert_allclose(G_sink.sum(0), G_screen.sum(0), atol=1e-02)
np.testing.assert_allclose(G_sink.sum(1), G_screen.sum(1), atol=1e-02)
示例8: gw_imports
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def gw_imports(use_gpu):
global bregman, gwggrad, gwloss, cdist
if use_gpu:
print('Using GPU in Gromov-Wasserstein computation')
global cm, pairwiseEuclideanGPU, init_matrix, cosine_distance_gpu, sinkhorn
import cudamat as cm
from ot.gpu import bregman
from ot.gpu.da import pairwiseEuclideanGPU
from gpu_utils import cdist
from gromov_gpu import gwggrad, gwloss, init_matrix, sinkhorn
else:
print('*NOT* Using GPU in Gromov-Wasserstein computation')
from ot import bregman
from ot.gromov import gwggrad, gwloss
示例9: test_smooth_ot_dual
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_smooth_ot_dual():
# get data
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
with pytest.raises(NotImplementedError):
Gl2, log = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type='none')
Gl2, log = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type='l2', log=True, stopThr=1e-10)
# check constratints
np.testing.assert_allclose(
u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(
u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn
# kl regyularisation
G = ot.smooth.smooth_ot_dual(u, u, M, 1, reg_type='kl', stopThr=1e-10)
# check constratints
np.testing.assert_allclose(
u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(
u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
np.testing.assert_allclose(G, G2, atol=1e-05)
示例10: test_smooth_ot_semi_dual
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_smooth_ot_semi_dual():
# get data
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
with pytest.raises(NotImplementedError):
Gl2, log = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type='none')
Gl2, log = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type='l2', log=True, stopThr=1e-10)
# check constratints
np.testing.assert_allclose(
u, Gl2.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(
u, Gl2.sum(0), atol=1e-05) # cf convergence sinkhorn
# kl regyularisation
G = ot.smooth.smooth_ot_semi_dual(u, u, M, 1, reg_type='kl', stopThr=1e-10)
# check constratints
np.testing.assert_allclose(
u, G.sum(1), atol=1e-05) # cf convergence sinkhorn
np.testing.assert_allclose(
u, G.sum(0), atol=1e-05) # cf convergence sinkhorn
G2 = ot.sinkhorn(u, u, M, 1, stopThr=1e-10)
np.testing.assert_allclose(G, G2, atol=1e-05)
示例11: test_gpu_old_doctests
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_gpu_old_doctests():
a = [.5, .5]
b = [.5, .5]
M = [[0., 1.], [1., 0.]]
G = ot.sinkhorn(a, b, M, 1)
np.testing.assert_allclose(G, np.array([[0.36552929, 0.13447071],
[0.13447071, 0.36552929]]))
示例12: test_gpu_sinkhorn
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_gpu_sinkhorn():
rng = np.random.RandomState(0)
for n_samples in [50, 100, 500, 1000]:
a = rng.rand(n_samples // 4, 100)
b = rng.rand(n_samples, 100)
wa = ot.unif(n_samples // 4)
wb = ot.unif(n_samples)
wb2 = np.random.rand(n_samples, 20)
wb2 /= wb2.sum(0, keepdims=True)
M = ot.dist(a.copy(), b.copy())
M2 = ot.gpu.dist(a.copy(), b.copy(), to_numpy=False)
reg = 1
G = ot.sinkhorn(wa, wb, M, reg)
G1 = ot.gpu.sinkhorn(wa, wb, M, reg)
np.testing.assert_allclose(G1, G, rtol=1e-10)
# run all on gpu
ot.gpu.sinkhorn(wa, wb, M2, reg, to_numpy=False, log=True)
# run sinkhorn for multiple targets
ot.gpu.sinkhorn(wa, wb2, M2, reg, to_numpy=False, log=True)
示例13: test_sinkhorn_empty
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_sinkhorn_empty():
# test sinkhorn
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G, log = ot.sinkhorn([], [], M, 1, stopThr=1e-10, verbose=True, log=True)
# check constratints
np.testing.assert_allclose(u, G.sum(1), atol=1e-05)
np.testing.assert_allclose(u, G.sum(0), atol=1e-05)
G, log = ot.sinkhorn([], [], M, 1, stopThr=1e-10,
method='sinkhorn_stabilized', verbose=True, log=True)
# check constratints
np.testing.assert_allclose(u, G.sum(1), atol=1e-05)
np.testing.assert_allclose(u, G.sum(0), atol=1e-05)
G, log = ot.sinkhorn(
[], [], M, 1, stopThr=1e-10, method='sinkhorn_epsilon_scaling',
verbose=True, log=True)
# check constratints
np.testing.assert_allclose(u, G.sum(1), atol=1e-05)
np.testing.assert_allclose(u, G.sum(0), atol=1e-05)
# test empty weights greenkhorn
ot.sinkhorn([], [], M, 1, method='greenkhorn', stopThr=1e-10, log=True)
示例14: test_implemented_methods
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_implemented_methods():
IMPLEMENTED_METHODS = ['sinkhorn', 'sinkhorn_stabilized']
ONLY_1D_methods = ['greenkhorn', 'sinkhorn_epsilon_scaling']
NOT_VALID_TOKENS = ['foo']
# test generalized sinkhorn for unbalanced OT barycenter
n = 3
rng = np.random.RandomState(42)
x = rng.randn(n, 2)
a = ot.utils.unif(n)
# make dists unbalanced
b = ot.utils.unif(n)
A = rng.rand(n, 2)
M = ot.dist(x, x)
epsilon = 1.
for method in IMPLEMENTED_METHODS:
ot.bregman.sinkhorn(a, b, M, epsilon, method=method)
ot.bregman.sinkhorn2(a, b, M, epsilon, method=method)
ot.bregman.barycenter(A, M, reg=epsilon, method=method)
with pytest.raises(ValueError):
for method in set(NOT_VALID_TOKENS):
ot.bregman.sinkhorn(a, b, M, epsilon, method=method)
ot.bregman.sinkhorn2(a, b, M, epsilon, method=method)
ot.bregman.barycenter(A, M, reg=epsilon, method=method)
for method in ONLY_1D_methods:
ot.bregman.sinkhorn(a, b, M, epsilon, method=method)
with pytest.raises(ValueError):
ot.bregman.sinkhorn2(a, b, M, epsilon, method=method)
示例15: test_sag_asgd_sinkhorn
# 需要导入模块: import ot [as 别名]
# 或者: from ot import sinkhorn [as 别名]
def test_sag_asgd_sinkhorn():
# test all algorithms
n = 15
reg = 1
nb_iter = 100000
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G_asgd = ot.stochastic.solve_semi_dual_entropic(u, u, M, reg, "asgd",
numItermax=nb_iter)
G_sag = ot.stochastic.solve_semi_dual_entropic(u, u, M, reg, "sag",
numItermax=nb_iter)
G_sinkhorn = ot.sinkhorn(u, u, M, reg)
# check constratints
np.testing.assert_allclose(
G_sag.sum(1), G_sinkhorn.sum(1), atol=1e-03)
np.testing.assert_allclose(
G_sag.sum(0), G_sinkhorn.sum(0), atol=1e-03)
np.testing.assert_allclose(
G_asgd.sum(1), G_sinkhorn.sum(1), atol=1e-03)
np.testing.assert_allclose(
G_asgd.sum(0), G_sinkhorn.sum(0), atol=1e-03)
np.testing.assert_allclose(
G_sag, G_sinkhorn, atol=1e-03) # cf convergence sag
np.testing.assert_allclose(
G_asgd, G_sinkhorn, atol=1e-03) # cf convergence asgd
#############################################################################
# COMPUTE TEST FOR DUAL PROBLEM
#############################################################################
#############################################################################
#
# TEST SGD algorithm
# ---------------------------------------------
# 2 identical discrete measures u defined on the same space with a
# regularization term, a batch_size and a number of iteration