Exemplo n.º 1
0
def test_sinkhorn():
    true1 = 0
    calculated1 = sinkhorn(r1, c1, M1, lamda, tol, maxiter)[0]
    np.testing.assert_almost_equal(true1, calculated1, decimal=1)

    true2 = 10
    calculated2 = sinkhorn(r2, c2, M2, lamda, tol, maxiter)[0]
    np.testing.assert_almost_equal(true2, calculated2, decimal=1)
Exemplo n.º 2
0
from sinkhorn_663 import sinkhorn
from skh_cpp import sinkhorn_cpp
from sinkhorn_663 import sample_to_prob_vec
import numpy as np
import matplotlib.pyplot as plt

# set up
maxiter = 10000
tol = 1e-6
lamda = np.linspace(120, 180, 61)
skh_res = np.zeros(61)
log_skh_res = np.zeros(61)
test_samp1 = np.random.beta(a=2, b=5, size=2000)
test_samp2 = np.random.uniform(low=5, high=6, size=2000)
M, r, c = sample_to_prob_vec(test_samp1, test_samp2, sigma=0)
# compare
for i in range(61):
    skh_res[i] = sinkhorn(r, c, M, lamda[i], tol, maxiter)[0]
    log_skh_res[i] = sinkhorn(r, c, M, lamda[i], tol, maxiter,
                              log_domain=True)[0]
plt.figure(figsize=(14, 8))
plt.plot(lamda, skh_res, label="Sinkhorn")
plt.plot(lamda, log_skh_res, label="log-domain Sinkhorn")
plt.xlabel("$\lambda$", fontsize=18)
plt.ylabel("Sinkhorn result", fontsize=16)
plt.title("Sinkhorn and log-domain Sinkhorn results vs $\lambda$", fontsize=16)
plt.legend(fontsize=16)
plt.savefig('report/instability.png')
plt.close()
c2 = c2.reshape(-1, 1)
# set parameters
maxiter = 10000
tol = 1e-6
lamda = 20

print("input\n************************")
print("Source empirical measure:\n", r2)
print("Target empirical measures:\n", c2)
print("Cost matrix:\n", M2)
print("Maximum iteration number: ", maxiter)
print("Accuracy tolerance: ", tol)
print("Entropy regularization parameter: ", lamda)

# use default sinkhorn
dist1, iter1 = sinkhorn(r2, c2, M2, lamda, tol, maxiter)
# results print
print("\noutput: default sinkhorn\n************************")
print("Sinkhorn distances: ", dist1)
print("number of iteration taken: ", iter1)

# use paralyzed sinkhorn
dist2, iter2 = sinkhorn(r2, c2, M2, lamda, tol, maxiter, parallel=True)
# results print
print("\noutput: paralyzed sinkhorn\n************************")
print("Sinkhorn distances: ", dist2)
print("number of iteration taken: ", iter2)

# use log_domain sinkhorn
dist3 = sinkhorn(r2, c2, M2, lamda, tol, maxiter, log_domain=True)
# results print
Exemplo n.º 4
0
import matplotlib.pyplot as plt

# set up
maxiter = 10000
np.random.seed(1)
size = [64, 128, 256, 512, 1024, 2048]
nrep = 10
iter_num_1 = np.zeros((nrep, len(size)))
iter_num_10 = np.zeros((nrep, len(size)))
iter_num_50 = np.zeros((nrep, len(size)))
for j in range(nrep):
    for i in range(6):
        p_samp = np.random.beta(a=2, b=5, size=size[i])
        q_samp = np.random.normal(size=size[i])
        M, r, c = sample_to_prob_vec(p_samp, q_samp, sigma=0)
        iter_num_1[j, i] = sinkhorn(r, c, M, 1, 1e-11, maxiter)[1]
        iter_num_10[j, i] = sinkhorn(r, c, M, 10, 1e-11, maxiter)[1]
        iter_num_50[j, i] = sinkhorn(r, c, M, 50, 1e-11, maxiter)[1]

# mean
mean_iter_1 = np.mean(iter_num_1, axis=0)
mean_iter_10 = np.mean(iter_num_10, axis=0)
mean_iter_50 = np.mean(iter_num_50, axis=0)
# std
std_iter_1 = np.std(iter_num_1, axis=0)
std_iter_10 = np.std(iter_num_10, axis=0)
std_iter_50 = np.std(iter_num_50, axis=0)

plt.figure(figsize=(15, 10))
plt.plot(size, mean_iter_1, label="$\lambda = 1$", marker='o')
plt.fill_between(size, mean_iter_1 - std_iter_1, mean_iter_1 + std_iter_1, alpha=0.2)
Exemplo n.º 5
0
plane_idx = ['plane' + str(i) for i in range(1, 4)]
umbrella_idx = ['umbrella' + str(i) for i in range(1, 4)]
cat_idx = ['cat' + str(i) for i in range(1, 4)]
all_idx = plane_idx + umbrella_idx + cat_idx
# plot
fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(7, 7))
for p in range(9):
    i = p // 3
    j = p % 3
    axes[i, j].imshow(compare_img_mat[p], cmap='gray')
    axes[i, j].axis('off')
    axes[i, j].set_title(all_idx[p])
fig.suptitle('Nine Image Samples from silhouettes data')
plt.savefig('report/silhouettes_image_sample.png')
plt.close()

# use of functions in sinkhorn_663.image
M_img = cost_mat(pixel)
compare_img_flat = flatten(compare_img_mat)
maxiter = 10000
tol = 1e-4
lamda = 20

# use of sinkhorn
compare_result = np.zeros([9, 9])
for i in range(9):
    for j in range(9):
        compare_result[i, j] = sinkhorn(compare_img_flat[i], compare_img_flat[j], M_img, lamda, tol, maxiter)[0]
result_df = pd.DataFrame(compare_result, index=all_idx, columns=all_idx).round(2)
result_df.to_pickle("report/result_df")
print(result_df)