本文整理汇总了Python中ot.emd方法的典型用法代码示例。如果您正苦于以下问题:Python ot.emd方法的具体用法?Python ot.emd怎么用?Python ot.emd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ot
的用法示例。
在下文中一共展示了ot.emd方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_emd_emd2
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_emd_emd2():
# test emd and emd2 for simple identity
n = 100
rng = np.random.RandomState(0)
x = rng.randn(n, 2)
u = ot.utils.unif(n)
M = ot.dist(x, x)
G = ot.emd(u, u, M)
# check G is identity
np.testing.assert_allclose(G, np.eye(n) / n)
# check constraints
np.testing.assert_allclose(u, G.sum(1)) # cf convergence sinkhorn
np.testing.assert_allclose(u, G.sum(0)) # cf convergence sinkhorn
w = ot.emd2(u, u, M)
# check loss=0
np.testing.assert_allclose(w, 0)
示例2: test_wass_1d
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_wass_1d():
# test emd1d gives similar results as emd
n = 20
m = 30
rng = np.random.RandomState(0)
u = rng.randn(n, 1)
v = rng.randn(m, 1)
M = ot.dist(u, v, metric='sqeuclidean')
G, log = ot.emd([], [], M, log=True)
wass = log["cost"]
wass1d = ot.wasserstein_1d(u, v, [], [], p=2.)
# check loss is similar
np.testing.assert_allclose(np.sqrt(wass), wass1d)
示例3: opt_transport
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def opt_transport(supply, demand, costs):
""" A wrapper for the EMD computation using the Optimal Transport (ot) package.
if emd_only is False, it only returns the emd value. Else it returns the transport
matrix and the minimum value of the objective.
"""
supply = supply.astype(np.float64)
demand = demand.astype(np.float64)
tot_supply = supply.sum()
tot_demand = demand.sum()
# assert tot_supply == tot_demand
supply = supply / tot_supply
demand = demand / tot_demand
# Now solve the problem
T = ot.emd(supply, demand, costs)
T = tot_supply * T
min_val = np.sum(T * costs)
emd = min_val/tot_supply
return T, min_val, emd
# Various utilities for global optimisation of *cheap* functions =========================
# Random samplning
示例4: test_emd_dimension_mismatch
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_emd_dimension_mismatch():
# test emd and emd2 for dimension mismatch
n_samples = 100
n_features = 2
rng = np.random.RandomState(0)
x = rng.randn(n_samples, n_features)
a = ot.utils.unif(n_samples + 1)
M = ot.dist(x, x)
np.testing.assert_raises(AssertionError, ot.emd, a, a, M)
np.testing.assert_raises(AssertionError, ot.emd2, a, a, M)
示例5: test_emd_1d_emd2_1d
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_emd_1d_emd2_1d():
# test emd1d gives similar results as emd
n = 20
m = 30
rng = np.random.RandomState(0)
u = rng.randn(n, 1)
v = rng.randn(m, 1)
M = ot.dist(u, v, metric='sqeuclidean')
G, log = ot.emd([], [], M, log=True)
wass = log["cost"]
G_1d, log = ot.emd_1d(u, v, [], [], metric='sqeuclidean', log=True)
wass1d = log["cost"]
wass1d_emd2 = ot.emd2_1d(u, v, [], [], metric='sqeuclidean', log=False)
wass1d_euc = ot.emd2_1d(u, v, [], [], metric='euclidean', log=False)
# check loss is similar
np.testing.assert_allclose(wass, wass1d)
np.testing.assert_allclose(wass, wass1d_emd2)
# check loss is similar to scipy's implementation for Euclidean metric
wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,)))
np.testing.assert_allclose(wass_sp, wass1d_euc)
# check constraints
np.testing.assert_allclose(np.ones((n,)) / n, G.sum(1))
np.testing.assert_allclose(np.ones((m,)) / m, G.sum(0))
# check G is similar
np.testing.assert_allclose(G, G_1d)
# check AssertionError is raised if called on non 1d arrays
u = np.random.randn(n, 2)
v = np.random.randn(m, 2)
with pytest.raises(AssertionError):
ot.emd_1d(u, v, [], [])
示例6: test_emd_1d_emd2_1d_with_weights
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_emd_1d_emd2_1d_with_weights():
# test emd1d gives similar results as emd
n = 20
m = 30
rng = np.random.RandomState(0)
u = rng.randn(n, 1)
v = rng.randn(m, 1)
w_u = rng.uniform(0., 1., n)
w_u = w_u / w_u.sum()
w_v = rng.uniform(0., 1., m)
w_v = w_v / w_v.sum()
M = ot.dist(u, v, metric='sqeuclidean')
G, log = ot.emd(w_u, w_v, M, log=True)
wass = log["cost"]
G_1d, log = ot.emd_1d(u, v, w_u, w_v, metric='sqeuclidean', log=True)
wass1d = log["cost"]
wass1d_emd2 = ot.emd2_1d(u, v, w_u, w_v, metric='sqeuclidean', log=False)
wass1d_euc = ot.emd2_1d(u, v, w_u, w_v, metric='euclidean', log=False)
# check loss is similar
np.testing.assert_allclose(wass, wass1d)
np.testing.assert_allclose(wass, wass1d_emd2)
# check loss is similar to scipy's implementation for Euclidean metric
wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,)), w_u, w_v)
np.testing.assert_allclose(wass_sp, wass1d_euc)
# check constraints
np.testing.assert_allclose(w_u, G.sum(1))
np.testing.assert_allclose(w_v, G.sum(0))
示例7: test_warnings
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_warnings():
n = 100 # nb bins
m = 100 # nb bins
mean1 = 30
mean2 = 50
# bin positions
x = np.arange(n, dtype=np.float64)
y = np.arange(m, dtype=np.float64)
# Gaussian distributions
a = gauss(n, m=mean1, s=5) # m= mean, s= std
b = gauss(m, m=mean2, s=10)
# loss matrix
M = ot.dist(x.reshape((-1, 1)), y.reshape((-1, 1))) ** (1. / 2)
print('Computing {} EMD '.format(1))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
print('Computing {} EMD '.format(1))
ot.emd(a, b, M, numItermax=1)
assert "numItermax" in str(w[-1].message)
assert len(w) == 1
a[0] = 100
print('Computing {} EMD '.format(2))
ot.emd(a, b, M)
assert "infeasible" in str(w[-1].message)
assert len(w) == 2
a[0] = -1
print('Computing {} EMD '.format(2))
ot.emd(a, b, M)
assert "infeasible" in str(w[-1].message)
assert len(w) == 3
示例8: EMD_at_k
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def EMD_at_k(k, ideal_desc_labels, sys_corresponding_scores, group_div_cost=np.e, margin_to_non_rele=100.0, rele_gain_base=4.0):
if k>len(ideal_desc_labels):
return 0.0
cost_mat = eval_cost_mat_group(ideal_desc_labels, group_div_cost=group_div_cost, margin_to_non_rele=margin_to_non_rele, rele_gain_base=rele_gain_base)
ideal_histogram = np_stable_softmax_e(ideal_desc_labels)
sys_historgram = np_stable_softmax_e(sys_corresponding_scores)
# %% EMD
G0 = ot.emd(a=sys_historgram, b=ideal_histogram, M=cost_mat)
emd_value = np.sum(G0 * cost_mat)
return emd_value
示例9: EMD_at_k
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def EMD_at_k(k, ideal_desc_labels, sys_corresponding_scores, group_div_cost=np.e, margin_to_non_rele=100.0, rele_gain_base=4.0):
if k>len(ideal_desc_labels):
return 0.0
cost_mat = eval_cost_mat_group(ideal_desc_labels, group_div_cost=group_div_cost, margin_to_non_rele=margin_to_non_rele, rele_gain_base=rele_gain_base)
ideal_histogram = np_stable_softmax_e(ideal_desc_labels)
sys_historgram = np_stable_softmax_e(sys_corresponding_scores)
# %% EMD
G0 = ot.emd(a=sys_historgram, b=ideal_histogram, M=cost_mat)
emd_value = np.sum(G0 * cost_mat)
return emd_value
示例10: test_emd2_multi
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_emd2_multi():
n = 500 # nb bins
# bin positions
x = np.arange(n, dtype=np.float64)
# Gaussian distributions
a = gauss(n, m=20, s=5) # m= mean, s= std
ls = np.arange(20, 500, 20)
nb = len(ls)
b = np.zeros((n, nb))
for i in range(nb):
b[:, i] = gauss(n, m=ls[i], s=10)
# loss matrix
M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))
# M/=M.max()
print('Computing {} EMD '.format(nb))
# emd loss 1 proc
ot.tic()
emd1 = ot.emd2(a, b, M, 1)
ot.toc('1 proc : {} s')
# emd loss multipro proc
ot.tic()
emdn = ot.emd2(a, b, M)
ot.toc('multi proc : {} s')
np.testing.assert_allclose(emd1, emdn)
# emd loss multipro proc with log
ot.tic()
emdn = ot.emd2(a, b, M, log=True, return_matrix=True)
ot.toc('multi proc : {} s')
for i in range(len(emdn)):
emd = emdn[i]
log = emd[1]
cost = emd[0]
check_duality_gap(a, b[:, i], M, log['G'], log['u'], log['v'], cost)
emdn[i] = cost
emdn = np.array(emdn)
np.testing.assert_allclose(emd1, emdn)
示例11: test_dual_variables
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def test_dual_variables():
n = 500 # nb bins
m = 600 # nb bins
mean1 = 300
mean2 = 400
# bin positions
x = np.arange(n, dtype=np.float64)
y = np.arange(m, dtype=np.float64)
# Gaussian distributions
a = gauss(n, m=mean1, s=5) # m= mean, s= std
b = gauss(m, m=mean2, s=10)
# loss matrix
M = ot.dist(x.reshape((-1, 1)), y.reshape((-1, 1))) ** (1. / 2)
print('Computing {} EMD '.format(1))
# emd loss 1 proc
ot.tic()
G, log = ot.emd(a, b, M, log=True)
ot.toc('1 proc : {} s')
ot.tic()
G2 = ot.emd(b, a, np.ascontiguousarray(M.T))
ot.toc('1 proc : {} s')
cost1 = (G * M).sum()
# Check symmetry
np.testing.assert_array_almost_equal(cost1, (M * G2.T).sum())
# Check with closed-form solution for gaussians
np.testing.assert_almost_equal(cost1, np.abs(mean1 - mean2))
# Check that both cost computations are equivalent
np.testing.assert_almost_equal(cost1, log['cost'])
check_duality_gap(a, b, M, G, log['u'], log['v'], log['cost'])
constraint_violation = log['u'][:, None] + log['v'][None, :] - M
assert constraint_violation.max() < 1e-8
示例12: jdot_krr
# 需要导入模块: import ot [as 别名]
# 或者: from ot import emd [as 别名]
def jdot_krr(X,y,Xtest,gamma_g=1, numIterBCD = 10, alpha=1,lambd=1e1,
method='emd',reg=1,ktype='linear'):
# Initializations
n = X.shape[0]
ntest = Xtest.shape[0]
wa=np.ones((n,))/n
wb=np.ones((ntest,))/ntest
# original loss
C0=cdist(X,Xtest,metric='sqeuclidean')
#print np.max(C0)
C0=C0/np.median(C0)
# classifier
g = classif.KRRClassifier(lambd)
# compute kernels
if ktype=='rbf':
Kt=sklearn.metrics.pairwise.rbf_kernel(Xtest,Xtest,gamma=gamma_g)
else:
Kt=sklearn.metrics.pairwise.linear_kernel(Xtest,Xtest)
C = alpha*C0#+ cdist(y,ypred,metric='sqeuclidean')
k=0
while (k<numIterBCD):# and not changeLabels:
k=k+1
if method=='sinkhorn':
G = ot.sinkhorn(wa,wb,C,reg)
if method=='emd':
G= ot.emd(wa,wb,C)
Yst=ntest*G.T.dot(y)
g.fit(Kt,Yst)
ypred=g.predict(Kt)
# function cost
fcost = cdist(y,ypred,metric='sqeuclidean')
C=alpha*C0+fcost
return g,np.sum(G*(fcost))