示例#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(
                '_', ' '))
示例#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('_', ' '))
示例#3
0
文件: exp2_noise.py 项目: tyDLin/PRW
        Xe = X + noise_level * np.random.randn(n, d)
        Ye = Y + noise_level * np.random.randn(n, d)

        # Compute PRW(Grad) without/with noise
        vals = []
        for ki in range(1, d + 1):
            algo = RiemmanAdaptive(reg=reg,
                                   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, ki)
            PRW.run(0, lr=0.01, beta=None)
            vals.append(PRW.get_value())
        no_noise[0, t, :] = np.sort(vals)

        vals = []
        for ki in range(1, d + 1):
            algo = RiemmanAdaptive(reg=reg,
                                   step_size_0=None,
                                   max_iter=30,
                                   threshold=0.01,
                                   max_iter_sinkhorn=30,
                                   threshold_sinkhorn=1e-04,
                                   use_gpu=False)
            PRW = ProjectionRobustWasserstein(Xe, Ye, a, b, algo, ki)
            PRW.run(0, lr=0.01, beta=None)
            vals.append(PRW.get_value())
        noise[0, t, :] = np.sort(vals)
示例#4
0
        # 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())
            values_subspace[1, indn, t] = np.linalg.norm(Omega - proj)
示例#5
0
文件: exp5_mnist.py 项目: tyDLin/PRW
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()
示例#6
0
    for t in range(nb_exp):
        print(t)
        for i, kstar in enumerate(kstars):
            for kdim in range(1, maxK + 1):
                a, b, X, Y = fragmented_hypercube(n, d, dim=kstar)

                algo = RiemmanAdaptive(reg=0.2,
                                       step_size_0=None,
                                       max_iter=30,
                                       threshold=1e-4,
                                       max_iter_sinkhorn=30,
                                       threshold_sinkhorn=1e-04,
                                       use_gpu=False)
                PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, kdim)
                PRW.run(0, lr=0.01, beta=None)
                values[0, i, kdim - 1, t] = np.abs(PRW.get_value())

                algo = RiemmanAdaptive(reg=0.2,
                                       step_size_0=None,
                                       max_iter=30,
                                       threshold=1e-4,
                                       max_iter_sinkhorn=30,
                                       threshold_sinkhorn=1e-04,
                                       use_gpu=False)
                PRW = ProjectionRobustWasserstein(X, Y, a, b, algo, kdim)
                PRW.run(1, lr=0.005, beta=0.8)
                values[1, i, kdim - 1, t] = np.abs(PRW.get_value())

    with open('./results/exp1_hypercube_dim_k.pkl', 'wb') as f:
        pickle.dump(values, f)
示例#7
0
            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:
    with open('./results/exp2_noise_level.pkl', 'rb') as f:
        SRW, W, PRW = pickle.load(f)

# Relative change
SRW_percent = np.abs(SRW - np.array([SRW[:, 0], ] * len(ind)).transpose()) / np.array(
    [SRW[:, 0], ] * len(ind)).transpose()