Esempio n. 1
0
def main():
    mode_str = 'RAGDS'

    k = 2
    reg = 0.1
    lr = 0.08
    beta = 0.9

    for film1 in scripts:
        for film2 in scripts:
            i = scripts.index(film1)
            j = scripts.index(film2)

            if i < j:
                X, a, words_X = measures[i]
                Y, b, words_Y = measures[j]

                algo = RiemmanAdaptive(reg=reg,
                                       step_size_0=None,
                                       max_iter=50,
                                       threshold=0.01,
                                       max_iter_sinkhorn=30,
                                       threshold_sinkhorn=1e-3,
                                       use_gpu=False)

                PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
                PRW.run(1, lr=lr, beta=beta)

                PRW_matrix[i, j] = PRW.get_value()
                PRW_matrix[j, i] = PRW_matrix[i, j]
                print('PRW (', film1, ',', film2, ') =', PRW_matrix[i, j])
                if film1 == 'DUNKIRK.txt' and film2 == 'INTERSTELLAR.txt':
                    plot_pushforwards_wordcloud(
                        PRW, words_X, words_Y, X, Y, a, b,
                        'cinema_%s_%s' % (film1[:-4], film2[:-4]), mode_str)

    # print latex scripts for table
    print()
    for i in range(Nb_scripts):
        print('%s ' % (abbreviations[i]), end=' ')
        tmp = np.array(PRW_matrix[i, :])
        tmp[i] = 1000
        min_val = min(tmp)
        for j in range(Nb_scripts):
            if PRW_matrix[i, j] == min_val:
                print('& \\textbf{%.3f} ' % (PRW_matrix[i, j]), end='')
            else:
                print('& %.3f ' % (PRW_matrix[i, j]), end='')
        print('\\\\ \hline')
    print()

    PRW_all = pd.DataFrame(PRW_matrix, index=scripts, columns=scripts)
    # Print the most similar movie to each movie
    for film in scripts:
        print(
            'The film most similar to', film[:-4].replace('_', ' '), 'is',
            PRW_all[film].loc[PRW_all[film] > 0].idxmin()[:-4].replace(
                '_', ' '))
Esempio n. 2
0
def main():
    mode_str = 'RAGAS'

    k = 2
    reg = 0.2
    lr = 0.08
    beta = 0.9

    for art1 in scripts:
        for art2 in scripts:
            i = scripts.index(art1)
            j = scripts.index(art2)
            if i < j:
                X, a, words_X = measures[i]
                Y, b, words_Y = measures[j]

                algo = RiemmanAdaptive(reg=reg, step_size_0=None, max_iter=30, threshold=0.01,
                                       max_iter_sinkhorn=30, threshold_sinkhorn=1e-3, use_gpu=False)

                PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
                PRW.run(1, lr=lr, beta=beta)

                PRW_matrix[i, j] = PRW.get_value()
                PRW_matrix[j, i] = PRW_matrix[i, j]
                print('PRW (', art1, ',', art2, ') =', PRW_matrix[i, j])
                if 'JC' in art1 and 'MV' in art2:
                    plot_pushforwards_wordcloud(PRW, words_X, words_Y, X, Y, a, b,
                                                'shakespeare_%s_%s' % (art1[:-4], art2[:-4]), mode_str)

    # print latex table of the PRW distances
    print()
    for i in range(Nb_scripts):
        print('%s ' % (scripts[i][:-4]), end=' ')
        tmp = np.array(PRW_matrix[i, :])
        tmp[i] = 1000
        min_val = min(tmp)
        for j in range(Nb_scripts):
            if PRW_matrix[i, j] == min_val:
                print('& \\textbf{%.3f} ' % (PRW_matrix[i, j]), end='')
            else:
                print('& %.3f ' % (PRW_matrix[i, j]), end='')
        print('\\\\ \hline')
    print()

    PRW_all = pd.DataFrame(PRW_matrix, index=scripts, columns=scripts)
    # Print the most similar movie to each movie
    for art in scripts:
        print('The art most similar to', art[:-4].replace('_', ' '), 'is',
              PRW_all[art].loc[PRW_all[art] > 0].idxmin()[:-4].replace('_', ' '))
Esempio n. 3
0
            # print('SRW')
            algo = FrankWolfe(reg=reg, step_size_0=None, max_iter=max_iter, max_iter_sinkhorn=max_iter_sinkhorn,
                              threshold=threshold, threshold_sinkhorn=threshold_sinkhorn, use_gpu=False)
            SRW = SubspaceRobustWasserstein(X, Y, a, b, algo, k)
            tic = time.time()
            SRW.run()
            tac = time.time()
            times_SRW[t, ind_d] = tac - tic

            # print('PRW(1)')
            algo = RiemmanAdaptive(reg=reg, step_size_0=None, max_iter=max_iter, max_iter_sinkhorn=max_iter_sinkhorn,
                                   threshold=threshold, threshold_sinkhorn=threshold_sinkhorn, use_gpu=False)
            PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
            tic = time.time()
            PRW.run(0, lr=0.01, beta=None)
            tac = time.time()
            times_PRW_1[t, ind_d] = tac - tic

            # print('PRW(2)')
            algo = RiemmanAdaptive(reg=reg, step_size_0=None, max_iter=max_iter, max_iter_sinkhorn=max_iter_sinkhorn,
                                   threshold=threshold, threshold_sinkhorn=threshold_sinkhorn, use_gpu=False)
            PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
            tic = time.time()
            PRW.run(1, lr=0.01, beta=0.8)
            tac = time.time()
            times_PRW_2[t, ind_d] = tac - tic

    print(times_SRW.shape, times_W.shape)

    with open('./results/exp4_computation_time.pkl', 'wb') as f:
Esempio n. 4
0
                if d >= 250:
                    reg = 0.5
                if lr > 0.01:
                    reg *= 10

                #print('PRW(1)', lr)
                algo = RiemmanAdaptive(reg=reg,
                                       step_size_0=None,
                                       max_iter=max_iter,
                                       max_iter_sinkhorn=max_iter_sinkhorn,
                                       threshold=threshold,
                                       threshold_sinkhorn=threshold_sinkhorn,
                                       use_gpu=False)
                PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
                tic = time.time()
                PRW.run(0, lr=lr, beta=None)
                tac = time.time()
                times_PRW[0, ind_lr, t, ind_d] = tac - tic

                #print('PRW(2)',lr)
                algo = RiemmanAdaptive(reg=reg,
                                       step_size_0=None,
                                       max_iter=max_iter,
                                       max_iter_sinkhorn=max_iter_sinkhorn,
                                       threshold=threshold,
                                       threshold_sinkhorn=threshold_sinkhorn,
                                       use_gpu=False)
                PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
                tic = time.time()
                PRW.run(1, lr=lr, beta=0.8)
                tac = time.time()
Esempio n. 5
0
        n = ns[indn]
        # Sample nb_exp times
        for t in range(nb_exp):
            a, b, X, Y = fragmented_hypercube(n, d, dim=2)

            # Riemann Gradient
            algo = RiemmanAdaptive(reg=0.2,
                                   step_size_0=None,
                                   max_iter=30,
                                   threshold=1e-3,
                                   max_iter_sinkhorn=30,
                                   threshold_sinkhorn=1e-03,
                                   use_gpu=False)
            PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)

            Omega, pi, maxmin_values = PRW.run(0, lr=0.01, beta=None)
            values[0, indn, t] = np.abs(8 - PRW.get_value())
            values_subspace[0, indn, t] = np.linalg.norm(Omega - proj)

            # Riemann Adaptive Gradient
            algo = RiemmanAdaptive(reg=0.2,
                                   step_size_0=None,
                                   max_iter=30,
                                   threshold=1e-3,
                                   max_iter_sinkhorn=30,
                                   threshold_sinkhorn=1e-03,
                                   use_gpu=False)
            PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)

            Omega, pi, maxmin_values = PRW.run(1, lr=0.01, beta=0.8)
            values[1, indn, t] = np.abs(8 - PRW.get_value())
Esempio n. 6
0
def main():
    feat_path = './results/exp5_mnist_feats.pkl'

    ################################################
    ## Generate MNIST features of dim (128,)
    ################################################
    # get_feats(feat_path)

    ################################################
    ## Open MNIST features of dim (128,)
    ################################################

    with open(feat_path, 'rb') as f:
        feats = pickle.load(f)

    for feat in feats:
        print(feat.shape)

    reg = 8
    lr = 0.01
    beta = 0.8

    SRW_matrix = np.zeros((10, 10))
    PRW_matrix = np.zeros((10, 10))

    d = 128  # dimension of MNIST features
    k = 2

    for i in range(10):
        for j in range(i + 1, 10):
            assert i < j

            X = feats[i]
            Y = feats[j]

            na = X.shape[0]
            nb = Y.shape[0]

            a = (1. / na) * np.ones(na)
            b = (1. / nb) * np.ones(nb)
            # print(na,nb)

            # Compute SRW
            algo = FrankWolfe(reg=reg,
                              step_size_0=None,
                              max_iter=30,
                              threshold=0.01,
                              max_iter_sinkhorn=30,
                              threshold_sinkhorn=1e-3,
                              use_gpu=False)
            SRW_ = SubspaceRobustWasserstein(X, Y, a, b, algo, k)
            SRW_.run()
            SRW_matrix[i, j] = SRW_.get_value() / 1000.0
            SRW_matrix[j, i] = SRW_matrix[i, j]
            print('SRW (', i, ',', j, ') =', SRW_matrix[i, j])

            # Compute PRW
            algo = RiemmanAdaptive(reg=reg,
                                   step_size_0=None,
                                   max_iter=30,
                                   threshold=0.01,
                                   max_iter_sinkhorn=30,
                                   threshold_sinkhorn=1e-3,
                                   use_gpu=False)

            PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
            PRW.run(1, lr=lr, beta=beta)

            PRW_matrix[i, j] = PRW.get_value() / 1000.0
            PRW_matrix[j, i] = PRW_matrix[i, j]
            print('PRW (', i, ',', j, ') =', PRW_matrix[i, j])

    # print latex scripts for table
    print("SRW")
    for i in range(10):
        print('%s ' % (i), end=' ')
        tmp = np.array(SRW_matrix[i, :])
        tmp[i] = 1000
        min_val = min(tmp)
        for j in range(10):
            if SRW_matrix[i, j] == min_val:
                print('& \\textbf{%.2f} ' % (SRW_matrix[i, j]), end='')
            else:
                print('& %.2f ' % (SRW_matrix[i, j]), end='')
        print('\\\\ \hline')
    print()

    print("PRW")
    for i in range(10):
        print('%s ' % (i), end=' ')
        tmp = np.array(PRW_matrix[i, :])
        tmp[i] = 1000
        min_val = min(tmp)
        for j in range(10):
            if PRW_matrix[i, j] == min_val:
                print('& \\textbf{%.2f} ' % (PRW_matrix[i, j]), end='')
            else:
                print('& %.2f ' % (PRW_matrix[i, j]), end='')
        print('\\\\ \hline')
    print()
Esempio n. 7
0
            algo = ProjectedGradientAscent(reg=reg, step_size_0=step_size_0, max_iter=max_iter, max_iter_sinkhorn=50,
                                           threshold=thr, threshold_sinkhorn=1e-04, use_gpu=False)
            SRW_ = SubspaceRobustWasserstein(Xe, Ye, a, b, algo, k=k)
            SRW_.run()

            # Compute Wasserstein
            algo = ProjectedGradientAscent(reg=reg, step_size_0=step_size_0, max_iter=1, max_iter_sinkhorn=50,
                                           threshold=0.05, threshold_sinkhorn=1e-04, use_gpu=False)
            W_ = SubspaceRobustWasserstein(Xe, Ye, a, b, algo, k=d)
            W_.run()

            # Compute PRW(RGAS)
            algo = RiemmanAdaptive(reg=reg, step_size_0=None, max_iter=max_iter, threshold=thr,
                                   max_iter_sinkhorn=50, threshold_sinkhorn=1e-04, use_gpu=False)
            PR1 = ProjectionRobustWasserstein(Xe, Ye, a, b, algo, k)
            PR1.run(0, lr=step_size_0, beta=None)
            #print('RGAS')


            lst_rsw.append(SRW_.get_value())
            lst_w.append(W_.get_value())
            lst_prw.append(PR1.get_value())

        SRW[t, :] = np.array(lst_rsw)
        W[t, :] = np.array(lst_w)
        PRW[t, :] = np.array(lst_prw)

    with open('./results/exp2_noise_level.pkl', 'wb') as f:
        pickle.dump([SRW, W, PRW], f)

else:
for indn in range(len(ns)):
    n = ns[indn]

    a, b, X, Y = fragmented_hypercube(n, d, dim=2)

    # Compute SRW
    algo = ProjectedGradientAscent(reg=0, step_size_0=0.01, max_iter=15, max_iter_sinkhorn=30,
                                   threshold=0.05, threshold_sinkhorn=1e-04, use_gpu=False)
    SRW_ = SubspaceRobustWasserstein(X, Y, a, b, algo, k=k)
    SRW_.run()
    SRW_.plot_transport_plan(path='figs/exp1_plan_%s_%d.png' % ('SRW', n),
                             method_name='SRW')

    # Compute Wasserstein
    algo = ProjectedGradientAscent(reg=0, step_size_0=0.01, max_iter=1, max_iter_sinkhorn=30,
                                   threshold=0.05, threshold_sinkhorn=1e-04, use_gpu=False)
    W_ = SubspaceRobustWasserstein(X, Y, a, b, algo, k=d)
    W_.run()
    W_.plot_transport_plan(path='figs/exp1_plan_%s_%d.png' % ('W', n),
                           method_name='W')

    # Riemann Gradient
    algo = RiemmanAdaptive(reg=0.1, step_size_0=None, max_iter=30, threshold=0.01,
                           max_iter_sinkhorn=30,
                           threshold_sinkhorn=1e-04, use_gpu=False)
    PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, k)
    #PRW.run(0, lr=0.2, beta=None)
    PRW.run(1, lr=0.01, beta=0.8)
    PRW.plot_transport_plan('figs/exp1_plan_%s_%d.png' % ('PRW', n))