Example #1
0
def test_gromov():
    n_samples = 50  # nb samples

    mu_s = np.array([0, 0])
    cov_s = np.array([[1, 0], [0, 1]])

    xs = ot.datasets.get_2D_samples_gauss(n_samples, mu_s, cov_s)

    xt = xs[::-1].copy()

    p = ot.unif(n_samples)
    q = ot.unif(n_samples)

    C1 = ot.dist(xs, xs)
    C2 = ot.dist(xt, xt)

    C1 /= C1.max()
    C2 /= C2.max()

    G = ot.gromov_wasserstein(C1, C2, p, q, 'square_loss', epsilon=5e-4)

    # check constratints
    np.testing.assert_allclose(p, G.sum(1),
                               atol=1e-04)  # cf convergence gromov
    np.testing.assert_allclose(q, G.sum(0),
                               atol=1e-04)  # cf convergence gromov
Example #2
0
def GW_word_mapping(xs,
                    xt,
                    src_words,
                    tgt_words,
                    eps=1e-3,
                    metric='cosine',
                    loss='square_loss',
                    max_iter=1000,
                    tol=1e-9,
                    verbose=False):
    """
        All-in-one wrapper function to compute GW for word embedding mapping.
        Computes distance matrices, marginal distributions, solves GW problem and returns
        translations.
    """

    # 1. Compute distance matrices
    print('Computing distance matrices....', end='')
    C1 = sp.spatial.distance.cdist(xs, xs, metric=metric)
    C2 = sp.spatial.distance.cdist(xt, xt, metric=metric)
    print('Done!')

    # 2. Compute marginal distributions
    p = ot.unif(xs.shape[0])
    q = ot.unif(xt.shape[0])

    # 3. Solve GW problem
    print('Solving GW problem....')
    G = ot.gromov_wasserstein(C1,
                              C2,
                              p,
                              q,
                              loss,
                              max_iter=max_iter,
                              tol=tol,
                              epsilon=eps,
                              verbose=verbose)

    cost = np.sum(G * tensor_square_loss(C1, C2, G))

    # 4. Analyze solution
    print('cost(G): {:8.2f}'.format(cost))
    print('G is p-feasible: {}'.format(
        'Yes' if np.allclose(G.sum(1), p) else 'No'))
    print('G is q-feasible: {}'.format(
        'Yes' if np.allclose(G.sum(0), q) else 'No'))
    plt.figure()
    plt.imshow(G, cmap='jet')
    plt.colorbar()
    plt.show()

    # 5. Compute word translations from mapping
    mapping = translations_from_coupling(G, src_words, tgt_words, verbose=True)

    return mapping, G, cost
Example #3
0
def gromov_wasserstein(x_src, x_tgt, C2):
    N = x_src.shape[0]
    C1 = GWmatrix(x_src)
    M = ot.gromov_wasserstein(C1,
                              C2,
                              np.ones(N),
                              np.ones(N),
                              'square_loss',
                              epsilon=0.55,
                              max_iter=100,
                              tol=1e-4)
    return procrustes(np.dot(M, x_tgt), x_src)
Example #4
0
#C2 /= C2.max()

pl.subplot(121)
pl.imshow(C1)
pl.show()

pl.subplot(122)
pl.imshow(C2)
pl.show()

# In[345]:

p = ot.unif(n_samples)
q = ot.unif(n_samples)

gw = ot.gromov_wasserstein(C1, C2, p, q, 'square_loss', epsilon=5e-4)
gw_dist = ot.gromov_wasserstein2(C1, C2, p, q, 'square_loss', epsilon=5e-4)

print('Gromov-Wasserstein distances between the distribution: ' + str(gw_dist))

pl.figure()
pl.imshow(gw, cmap='jet')
pl.colorbar()
pl.show()

# In[364]:

for i in range(1):
    #def deep_learning(protein_train,protein_test,protein_target):
    from keras.models import Sequential
    from keras.layers.convolutional import Conv3D, Conv1D
Example #5
0
# %%
# It is clear that the optimization has converged and that we recover the
# ratio of the different classes in the SBM graph up to a permutation.

# %%
# Community clustering with uniform and estimated weights
# --------------------------------------------
# The GW OT  plan can be used to perform a clustering of the nodes of a graph
# when computing the GW with a simple template like C0 by labeling nodes in
# the original graph using by the index of the noe in the template receiving
# the most mass.
#
# We show here the result of such a clustering when using uniform weights on
# the template C0 and when using the optimal weights previously estimated.

T_unif = ot.gromov_wasserstein(C1, C0, ot.unif(n), ot.unif(3))
label_unif = T_unif.argmax(1)

T_est = ot.gromov_wasserstein(C1, C0, ot.unif(n), a0_est)
label_est = T_est.argmax(1)

pl.figure(3, (10, 5))
pl.clf()
pl.subplot(1, 2, 1)
plot_graph(x1, C1, color=label_unif)
pl.title("Graph clustering unif. weights")
pl.axis("off")
pl.subplot(1, 2, 2)
plot_graph(x1, C1, color=label_est)
pl.title("Graph clustering est. weights")
pl.axis("off")