コード例 #1
0
ファイル: demo_frac.py プロジェクト: mhhuang95/PRW_RBCD
    time_AGRDD = np.zeros(n_ep)

    for i in range(n_ep):
        
        U0 = InitialStiefel(d, k)
        a,b,X,Y = fragmented_hypercube(n,d,dim)
        
        gamma = 0.001
        eta = 0.2
    
        params = {'eta':eta, 'tau':gamma/eta, 'max_iter':2000, 'threshold':0.1, 'verbose':True}
        algo1 = RiemannianGradientAscentSinkhorn(**params)
        algo1.run_RGAS(a, b, X, Y, k, U0)
        algo1.run_RAGAS(a, b, X, Y, k, U0)


        params = {'eta':eta, 'tau':gamma, 'max_iter':2000, 'threshold':0.1, 'verbose':True}
        algo2 = RiemannianBlockCoordinateDescent(**params)
        algo2.run_RBCD(a, b, X, Y, k, U0)
        algo2.run_RABCD(a, b, X, Y, k, U0)
    

#         Compute Wasserstein
        algo3 = ProjectedGradientAscent(reg=eta, step_size_0=gamma, max_iter=1, max_iter_sinkhorn=50, threshold=0.001, threshold_sinkhorn=1e-04, use_gpu=False)
        W_ = SubspaceRobustWasserstein(X, Y, a, b, algo3, k=d)
        W_.run()
        print(W_.get_value())



コード例 #2
0
ファイル: exp_hc_pointn.py プロジェクト: mhhuang95/PRW_RBCD
def main():

    d = 20  # Total dimension
    k = 2  # k* = 2 and compute SRW with k = 2
    nb_exp = 100  # Do 500 experiments
    ns = [25, 50, 100, 250, 500,
          1000]  # Compute SRW between measures with 'n' points for 'n' in 'ns'

    values = np.zeros((3, len(ns), nb_exp))
    values_subspace = np.zeros((3, len(ns), nb_exp))

    proj = np.zeros((d, d))  # Real optimal subspace
    proj[0, 0] = 1
    proj[1, 1] = 1

    eta = 0.2
    tau = 0.001
    verb = True

    if 1 == 1:

        for indn in range(len(ns)):
            n = ns[indn]
            # Sample nb_exp times
            for t in range(nb_exp):
                a, b, X, Y = fragmented_hypercube(n, d, dim=2)

                U0 = np.zeros((d, k))
                U0[:k, :] = np.eye(k)

                algo = RiemannianBlockCoordinateDescent(eta=eta,
                                                        tau=None,
                                                        max_iter=3000,
                                                        threshold=0.1,
                                                        verbose=verb)
                PRW = ProjectedRobustWasserstein(X, Y, a, b, algo, k)
                PRW.run('RBCD', tau, U0)
                values[0, indn, t] = np.abs(8 - PRW.get_value())
                values_subspace[0, indn,
                                t] = np.linalg.norm(PRW.get_Omega() - proj)

                algo1 = RiemannianGradientAscentSinkhorn(eta=eta,
                                                         tau=None,
                                                         max_iter=3000,
                                                         threshold=0.1,
                                                         sink_threshold=1e-4,
                                                         verbose=verb)
                PRW1 = ProjectedRobustWasserstein(X, Y, a, b, algo1, k)
                PRW1.run('RGAS', tau / eta, U0)
                values[1, indn, t] = np.abs(8 - PRW1.get_value())
                values_subspace[1, indn,
                                t] = np.linalg.norm(PRW1.get_Omega() - proj)

                # Compute Wasserstein
                algo2 = ProjectedGradientAscent(reg=eta,
                                                step_size_0=tau,
                                                max_iter=1,
                                                max_iter_sinkhorn=50,
                                                threshold=0.001,
                                                threshold_sinkhorn=1e-04,
                                                use_gpu=False)
                W_ = SubspaceRobustWasserstein(X, Y, a, b, algo2, k=d)
                W_.run()
                values[2, indn, t] = np.abs(8 - W_.get_value())
                values_subspace[2, indn,
                                t] = np.linalg.norm(W_.get_Omega() - proj)
                print(W_.get_value())

        with open('./results/exp1_hypercube_value.pkl', 'wb') as f:
            pickle.dump([values, values_subspace], f)

    else:
        with open('./results/exp1_hypercube_value.pkl', 'rb') as f:
            values, values_subspace = pickle.load(f)

        print('n =', n, '/', np.mean(values[indn, :]), '/',
              np.mean(values1[indn, :]))

    captions = ['PRW (RBCD)', 'PRW (RGAS)']

    line = ['o-', 'o--', '-']
    plt.figure(figsize=(12, 8))
    for t in range(3):
        values_mean = np.mean(values[t, :, :], axis=1)
        values_min = np.min(values[t, :, :], axis=1)
        values_10 = np.percentile(values[t, :, :], 10, axis=1)
        values_25 = np.percentile(values[t, :, :], 25, axis=1)
        values_75 = np.percentile(values[t, :, :], 75, axis=1)
        values_90 = np.percentile(values[t, :, :], 90, axis=1)
        values_max = np.max(values[t, :, :], axis=1)

        mean, = plt.semilogy(ns,
                             values_mean,
                             line[t],
                             lw=4,
                             ms=11,
                             label=captions[t])
        col = mean.get_color()
        plt.fill_between(ns, values_25, values_75, facecolor=col, alpha=0.3)
        plt.fill_between(ns, values_10, values_90, facecolor=col, alpha=0.2)

    plt.xlabel('Number of points n', fontsize=25)
    plt.ylabel('MEE', fontsize=25)
    plt.legend(loc='best', fontsize=25)
    plt.title('Mean estimation error', fontsize=30)

    plt.xticks(ns, fontsize=20)
    plt.yticks(np.array([0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10.0]), fontsize=20)
    plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0f'))
    plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
    plt.grid(ls=':')
    plt.savefig('figs/exp1_hypercube_value_1.png')
    plt.show()
    plt.close()
    plt.clf()

    plt.figure(figsize=(12, 8))
    for t in range(3):
        values_subspace_mean = np.mean(values_subspace[t, :, :], axis=1)
        values_subspace_min = np.min(values_subspace[t, :, :], axis=1)
        values_subspace_10 = np.percentile(values_subspace[t, :, :],
                                           10,
                                           axis=1)
        values_subspace_25 = np.percentile(values_subspace[t, :, :],
                                           25,
                                           axis=1)
        values_subspace_75 = np.percentile(values_subspace[t, :, :],
                                           75,
                                           axis=1)
        values_subspace_90 = np.percentile(values_subspace[t, :, :],
                                           90,
                                           axis=1)
        values_subspace_max = np.max(values_subspace[t, :, :], axis=1)

        mean, = plt.loglog(ns,
                           values_subspace_mean,
                           line[t],
                           lw=4,
                           ms=11,
                           label=captions[t])
        col = mean.get_color()
        plt.fill_between(ns,
                         values_subspace_25,
                         values_subspace_75,
                         facecolor=col,
                         alpha=0.3)
        plt.fill_between(ns,
                         values_subspace_10,
                         values_subspace_90,
                         facecolor=col,
                         alpha=0.2)
        plt.fill_between(ns,
                         values_subspace_min,
                         values_subspace_max,
                         facecolor=col,
                         alpha=0.15)

    plt.xlabel('Number of points n', fontsize=25)
    plt.ylabel('$||\Omega^* - \widehat\Omega||_F$', fontsize=25)
    plt.legend(loc='best', fontsize=25)
    plt.title('Mean subspace estimation error', fontsize=30)
    plt.xticks(ns, fontsize=20)
    plt.yticks(np.array(range(1, 8)) / 10, fontsize=20)
    plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.0f'))
    plt.gca().yaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
    plt.grid(ls=':')
    plt.savefig('figs/exp1_hypercube_value_2.png')
コード例 #3
0
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 = FrankWolfe(reg=0.1,
                              step_size_0=None,
                              max_iter=50,
                              threshold=0.01,
                              max_iter_sinkhorn=30,
                              threshold_sinkhorn=1e-3,
                              use_gpu=True)
            SRW = SubspaceRobustWasserstein(X, Y, a, b, algo, k=2)
            SRW.run()
            SRW.plot_convergence()
            SRW_matrix[i, j] = SRW.get_value()
            SRW_matrix[j, i] = SRW_matrix[i, j]
            print('SRW (', film1, ',', film2, ') =', SRW_matrix[i, j])
            if film2 == 'KILL_BILL_VOLUME_1.txt' and film1 == 'INTERSTELLAR.txt':
                plot_pushforwards_wordcloud(SRW, words_X, words_Y)

# Plot the metric MDS projection of the SRW values
SRW_all = pd.DataFrame(SRW_matrix, index=scripts, columns=scripts)

from sklearn.manifold import MDS
embedding = MDS(n_components=2, dissimilarity='precomputed')
dis = SRW_all - SRW_all[SRW_all > 0].min().min()
dis.values[[np.arange(dis.shape[0])] * 2] = 0
コード例 #4
0
    lst_w = []
    for epsilon in ind:
        # Add noise of level epsilon
        noiseX = np.random.randn(n,d)
        noiseY = np.random.randn(n,d)
        Xe = X + epsilon*noiseX
        Ye = Y + epsilon*noiseY
        
        # Choice of step size
        ones = np.ones((n,n))
        C = np.diag(np.diag(Xe.dot(Xe.T))).dot(ones) + ones.dot(np.diag(np.diag(Ye.dot(Ye.T)))) - 2*Xe.dot(Ye.T)
        step_size_0 = 1./np.max(C)
        
        # Compute SRW
        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()
        
        lst_rsw.append(SRW_.get_value())
        lst_w.append(W_.get_value())
    
    SRW[t,:] = np.array(lst_rsw)
    W[t,:] = np.array(lst_w)

# Relative change
SRW_percent = np.abs(SRW-np.array([SRW[:,0],]*len(ind)).transpose())/np.array([SRW[:,0],]*len(ind)).transpose()
コード例 #5
0
    X = np.random.multivariate_normal(mean_1, cov_1, size=n)
    Y = np.random.multivariate_normal(mean_2, cov_2, size=n)

    # Add noise
    Xe = X + noise_level * np.random.randn(n, d)
    Ye = Y + noise_level * np.random.randn(n, d)

    # Compute SRW begtween X and Y
    algo = ProjectedGradientAscent(reg=0.,
                                   step_size_0=0.01,
                                   max_iter=30,
                                   threshold=0.01,
                                   max_iter_sinkhorn=30,
                                   threshold_sinkhorn=10e-04,
                                   use_gpu=False)
    SRW = SubspaceRobustWasserstein(X, Y, a, b, algo, k)
    SRW.run()
    no_noise[t, :] = np.sort(list(SRW.get_value().values()))

    # Compute SRW begtween Xe and Ye
    algo = ProjectedGradientAscent(reg=0.,
                                   step_size_0=0.01,
                                   max_iter=30,
                                   threshold=0.01,
                                   max_iter_sinkhorn=30,
                                   threshold_sinkhorn=10e-04,
                                   use_gpu=False)
    SRWe = SubspaceRobustWasserstein(Xe, Ye, a, b, algo, k)
    SRWe.run()
    noise[t, :] = np.sort(list(SRWe.get_value().values()))
コード例 #6
0
    print(t)
    
    for ind_d in range(nb_ds):
        d = ds[ind_d]
        print(d)
        
        a,b,X,Y = fragmented_hypercube(n,d,dim=2)
        
        if d>=250:
            reg=0.5
        if d>=1000:
            reg=1.
        
        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=True)
        SRW = SubspaceRobustWasserstein(X, Y, a, b, algo, k)
        tic = time.time()
        SRW.run()
        tac = time.time()
        times_SRW[t,ind_d] = tac-tic
        print(SRW.get_value(), len(SRW.get_minmax_values()), cp.abs(min(SRW.get_minmax_values())-SRW.get_value())/SRW.get_value())
        
        print('W')
        tic = time.time()
        ones = cp.ones((n,n))
        C = cp.diag(cp.diag(X.dot(X.T))).dot(ones) + ones.dot(cp.diag(cp.diag(Y.dot(Y.T)))) - 2*X.dot(Y.T)
        OT_plan = sinkhorn_knopp_gpu(a, b, C, reg, numItermax=max_iter_sinkhorn, stopThr=threshold_sinkhorn)
        tac = time.time()
        times_W[t,ind_d] = tac-tic

times_SRW = cp.asnumpy(times_SRW)
コード例 #7
0
ファイル: exp4_computation_time.py プロジェクト: tyDLin/PRW
            reg = 0.2
            if d >= 250:
                reg = 0.5

            # print('W')
            tic = time.time()
            ones = np.ones((n, n))
            C = np.diag(np.diag(X.dot(X.T))).dot(ones) + ones.dot(np.diag(np.diag(Y.dot(Y.T)))) - 2 * X.dot(Y.T)
            OT_plan = sinkhorn_knopp(a, b, C, reg, numItermax=max_iter_sinkhorn, stopThr=threshold_sinkhorn)
            tac = time.time()
            times_W[t, ind_d] = tac - tic

            # 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)')
コード例 #8
0
def main():
    d = 20  # Total dimension
    n = 100  # Number of points in each measure
    k = 5  # Dimension of the Wishart (i.e. of support of the measures)
    nb_exp = 100  # Number of experiments to run
    reg = 0.  # No regularization
    # max_iter = 1000 # Maximum number of iterations (the bigger the more precise)
    # thr = 1e-5 # Stopping threshold (not attained here since we are in unregularized SRW)

    a = (1. / n) * np.ones(n)
    b = (1. / n) * np.ones(n)

    mean_1 = np.zeros(d)
    mean_2 = np.zeros(d)

    # Noise levels to test
    ind = [0., 0.01, 0.1, 1, 2, 4, 7, 10]

    PRW = np.zeros((nb_exp, len(ind)))
    PRW1 = np.zeros((nb_exp, len(ind)))
    W = np.zeros((nb_exp, len(ind)))

    for t in range(nb_exp):
        print(t)
        cov_1 = np.random.randn(d, k)
        cov_1 = cov_1.dot(cov_1.T)
        cov_2 = np.random.randn(d, k)
        cov_2 = cov_2.dot(cov_2.T)

        # Draw the measures
        X = np.random.multivariate_normal(mean_1, cov_1, size=n)
        Y = np.random.multivariate_normal(mean_2, cov_2, size=n)

        verb = True

        lst_rsw = []
        lst_rsw1 = []
        lst_w = []
        for epsilon in ind:
            # Add noise of level epsilon
            noiseX = np.random.randn(n, d)
            noiseY = np.random.randn(n, d)
            Xe = X + epsilon * noiseX
            Ye = Y + epsilon * noiseY

            if epsilon < 4:
                eta = 2
                stepsize = 0.01
                thre = 0.1
            else:
                eta = 10
                stepsize = 0.002
                thre = 0.1

            algo = RiemannianBlockCoordinateDescent(eta=eta,
                                                    tau=stepsize,
                                                    max_iter=5000,
                                                    threshold=thre,
                                                    verbose=verb)
            PRW_ = ProjectedRobustWasserstein(Xe, Ye, a, b, algo, k=10)
            PRW_.run('RBCD', tau=stepsize)

            algo1 = RiemannianGradientAscentSinkhorn(eta=eta,
                                                     tau=stepsize / eta,
                                                     max_iter=5000,
                                                     sink_threshold=1e-4,
                                                     threshold=thre,
                                                     verbose=verb)
            PRW1_ = ProjectedRobustWasserstein(Xe, Ye, a, b, algo1, k=10)
            PRW1_.run('RGAS', tau=stepsize / eta)

            # Choice of step size
            ones = np.ones((n, n))
            C = np.diag(np.diag(Xe.dot(Xe.T))).dot(ones) + ones.dot(
                np.diag(np.diag(Ye.dot(Ye.T)))) - 2 * Xe.dot(Ye.T)
            step_size_0 = 1. / np.max(C)

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

            lst_rsw.append(PRW_.get_value())
            lst_rsw1.append(PRW1_.get_value())
            lst_w.append(W_.get_value())

        PRW[t, :] = np.array(lst_rsw)
        PRW1[t, :] = np.array(lst_rsw1)
        W[t, :] = np.array(lst_w)

    # Relative change
    PRW_percent = np.abs(PRW - np.array([
        PRW[:, 0],
    ] * len(ind)).transpose()) / np.array([
        PRW[:, 0],
    ] * len(ind)).transpose()
    PRW1_percent = np.abs(PRW1 - np.array([
        PRW1[:, 0],
    ] * len(ind)).transpose()) / np.array([
        PRW1[:, 0],
    ] * len(ind)).transpose()
    W_percent = np.abs(W - np.array([
        W[:, 0],
    ] * len(ind)).transpose()) / np.array([
        W[:, 0],
    ] * len(ind)).transpose()

    PRW_percent = PRW_percent[:, 1:]
    PRW1_percent = PRW1_percent[:, 1:]
    W_percent = W_percent[:, 1:]

    PRW_mean = np.mean(PRW_percent, axis=0)
    PRW_min = np.min(PRW_percent, axis=0)
    PRW_10 = np.percentile(PRW_percent, 10, axis=0)
    PRW_25 = np.percentile(PRW_percent, 25, axis=0)
    PRW_75 = np.percentile(PRW_percent, 75, axis=0)
    PRW_90 = np.percentile(PRW_percent, 90, axis=0)
    PRW_max = np.max(PRW_percent, axis=0)

    PRW1_mean = np.mean(PRW1_percent, axis=0)
    PRW1_min = np.min(PRW1_percent, axis=0)
    PRW1_10 = np.percentile(PRW1_percent, 10, axis=0)
    PRW1_25 = np.percentile(PRW1_percent, 25, axis=0)
    PRW1_75 = np.percentile(PRW1_percent, 75, axis=0)
    PRW1_90 = np.percentile(PRW1_percent, 90, axis=0)
    PRW1_max = np.max(PRW1_percent, axis=0)

    W_mean = np.mean(W_percent, axis=0)
    W_min = np.min(W_percent, axis=0)
    W_10 = np.percentile(W_percent, 10, axis=0)
    W_25 = np.percentile(W_percent, 25, axis=0)
    W_75 = np.percentile(W_percent, 75, axis=0)
    W_90 = np.percentile(W_percent, 90, axis=0)
    W_max = np.max(W_percent, axis=0)

    # PLOT
    import matplotlib.ticker as ticker
    plt.figure(figsize=(12, 8))

    plotW, = plt.loglog(ind[1:],
                        W_mean,
                        'o-',
                        label='Wasserstein',
                        lw=5,
                        ms=10)
    col_W = plotW.get_color()
    plt.fill_between(ind[1:], W_25, W_75, facecolor=col_W, alpha=0.3)
    plt.fill_between(ind[1:], W_10, W_90, facecolor=col_W, alpha=0.2)

    plotPRW, = plt.loglog(ind[1:], PRW_mean, 'o-', label='RBCD', lw=5, ms=10)
    col_PRW = plotPRW.get_color()
    plt.fill_between(ind[1:], PRW_25, PRW_75, facecolor=col_PRW, alpha=0.3)
    plt.fill_between(ind[1:], PRW_10, PRW_90, facecolor=col_PRW, alpha=0.2)

    plotPRW1, = plt.loglog(ind[1:],
                           PRW1_mean,
                           'o--',
                           label='RGAS',
                           lw=5,
                           ms=10)
    col_PRW1 = plotPRW1.get_color()
    plt.fill_between(ind[1:], PRW1_25, PRW1_75, facecolor=col_PRW1, alpha=0.3)
    plt.fill_between(ind[1:], PRW1_10, PRW1_90, facecolor=col_PRW1, alpha=0.2)

    plt.xlabel('Noise level (log scale)', fontsize=25)
    plt.ylabel('Relative error (log scale)', fontsize=25)

    plt.yticks(fontsize=20)
    plt.xticks(ind[1:], fontsize=20)
    plt.gca().xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.2g'))

    plt.legend(loc=2, fontsize=18)
    plt.grid(ls=':')
    plt.savefig('figs/exp2_noise_level.png')
コード例 #9
0
ファイル: exp_gauss_time.py プロジェクト: mhhuang95/PRW_RBCD
def plot_n_equal_d():
    
    ds = [15, 25, 50, 100, 250] # Number of points in the measures

    nb_ds = len(ds)
    k = 5 # Dimension parameter
    max_iter = 5000 # Maximum number of iterations
    max_iter_sinkhorn = 1000 # Maximum number of iterations in Sinkhorn
    threshold = 0.1 # Stopping threshold
    threshold_sinkhorn = 1e-10 # Stopping threshold in Sinkhorn
    nb_exp = 5 # Number of experiments
    
    tau = 0.01
    tau_adap = 0.05

    times_RBCD = np.zeros((nb_exp, nb_ds))
    times_RGAS = np.zeros((nb_exp, nb_ds))
    times_RABCD = np.zeros((nb_exp, nb_ds))
    times_RAGAS = np.zeros((nb_exp, nb_ds))
    times_SRW = np.zeros((nb_exp, nb_ds))


    for t in range(nb_exp):
        print(t)
        for ind_d in range(nb_ds):
            d = ds[ind_d]
            n = 10 * d
            print(d, n)
            
            a = (1./n) * np.ones(n)
            b = (1./n) * np.ones(n)

            mean_1 = np.zeros(d)
            mean_2 = np.zeros(d)
            cov_1 = np.random.randn(d,k)
            cov_1 = cov_1.dot(cov_1.T)
            cov_2 = np.random.randn(d,k)
            cov_2 = cov_2.dot(cov_2.T)

            # Draw the measures
            X = np.random.multivariate_normal(mean_1, cov_1, size=n)
            Y = np.random.multivariate_normal(mean_2, cov_2, size=n)
            
            reg = 10
            
            if d>=50:
                tau_adap = 0.1
                reg=20
                
            if d>=250:
                tau_adap = 0.2
                reg=50

            U0 = InitialStiefel(d, k)

            print('RBCD')
            RBCD = RiemannianBlockCoordinateDescent(eta=reg, tau=tau, max_iter=max_iter, threshold=threshold, verbose=True)
            PRW = ProjectedRobustWasserstein(X, Y, a, b, RBCD, k)
            PRW.run( 'RBCD',tau, U0)
            times_RBCD[t,ind_d] = PRW.running_time
            print('RABCD')
            PRW.run( 'RABCD',tau_adap, U0)
            times_RABCD[t,ind_d] = PRW.running_time


            print('RGAS')
            RGAS = RiemannianGradientAscentSinkhorn(eta=reg, tau = tau/reg, max_iter=max_iter, threshold=threshold, 
                                                    sink_threshold=threshold_sinkhorn, verbose=True)
            PRW1 = ProjectedRobustWasserstein(X, Y, a, b, RGAS, k)
            PRW1.run('RGAS',tau/reg, U0)
            times_RGAS[t,ind_d] = PRW1.running_time
            print('RAGAS')
            PRW1.run('RAGAS',tau_adap/reg, U0)
            times_RAGAS[t,ind_d] = PRW1.running_time
            
            print('FWSRW')
            algo = FrankWolfe(reg=reg, step_size_0=tau, max_iter=max_iter, max_iter_sinkhorn=max_iter_sinkhorn,
                              threshold=(0.1*tau)**2, 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("exp_gauss_n_equal_10d")
            
    times_RBCD_mean = np.mean(times_RBCD, axis=0)
    times_RABCD_mean = np.mean(times_RABCD, axis=0)
    times_RGAS_mean = np.mean(times_RGAS, axis=0)
    times_RAGAS_mean = np.mean(times_RAGAS, axis=0)
    times_SRW_mean = np.mean(times_SRW, axis=0)
    
    
    print('RBCD &', "%.2f &" %times_RBCD_mean[0], "%.2f &" %times_RBCD_mean[1],"%.2f &" %times_RBCD_mean[2], "%.2f &" %times_RBCD_mean[3], "%.2f "% times_RBCD_mean[4], "\\ \hline")
    print('RABCD &', "%.2f &" %times_RABCD_mean[0], "%.2f &" %times_RABCD_mean[1],"%.2f &" %times_RABCD_mean[2], "%.2f &" %times_RABCD_mean[3], "%.2f "% times_RABCD_mean[4], "\\ \hline")
    print('RGAS &', "%.2f &" %times_RGAS_mean[0], "%.2f &" %times_RGAS_mean[1],"%.2f &" %times_RGAS_mean[2], "%.2f &" %times_RGAS_mean[3], "%.2f "% times_RGAS_mean[4], "\\ \hline")
    print('RAGAS &', "%.2f &" %times_RAGAS_mean[0], "%.2f &" %times_RAGAS_mean[1],"%.2f &" %times_RAGAS_mean[2], "%.2f &" %times_RAGAS_mean[3], "%.2f "% times_RAGAS_mean[4], "\\ \hline")
    print('SRW &',  "%.2f &" %times_SRW_mean[0], "%.2f &" %times_SRW_mean[1],"%.2f &" %times_SRW_mean[2], "%.2f &" %times_SRW_mean[3], "%.2f "% times_SRW_mean[4], "\\ \hline")
コード例 #10
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()
コード例 #11
0
proj = np.zeros((d, d))  # Real optimal subspace
proj[0, 0] = 1
proj[1, 1] = 1

np.random.seed(357)


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,
proj = cp.zeros((d, d))  # Real optimal subspace
proj[0, 0] = 1
proj[1, 1] = 1
for indn in range(len(ns)):
    n = ns[indn]
    # Sample nb_exp times
    for t in range(nb_exp):
        FW = FrankWolfe(reg=0.2,
                        step_size_0=None,
                        max_iter=15,
                        threshold=0.01,
                        max_iter_sinkhorn=30,
                        threshold_sinkhorn=10e-04,
                        use_gpu=False)
        a, b, X, Y = disk_annulus(n, d, dim=2)
        SRW_FW = SubspaceRobustWasserstein(X, Y, a, b, FW, k)
        SRW_FW.run()
        values[indn, t] = np.abs(REAL_VALUE - SRW_FW.get_value())
        values_subspace[indn, t] = cp.linalg.norm(SRW_FW.get_Omega() - proj)

    print('n =', n, '/', np.mean(values[indn, :]))

values_mean = np.mean(values, axis=1)
values_min = np.min(values, axis=1)
values_10 = np.percentile(values, 10, axis=1)
values_25 = np.percentile(values, 25, axis=1)
values_75 = np.percentile(values, 75, axis=1)
values_90 = np.percentile(values, 90, axis=1)
values_max = np.max(values, axis=1)

import matplotlib.ticker as ticker
コード例 #13
0
ファイル: exp_hc_time.py プロジェクト: mhhuang95/PRW_RBCD
def plot_fix_n():
    
    ds = [25, 50, 100, 250, 500] # Dimensions
    nb_ds = len(ds)
    n = 100 # Number of points in the measures
    k = 2 # Dimension parameter
    max_iter = 2000 # Maximum number of iterations
    max_iter_sinkhorn = 1000 # Maximum number of iterations in Sinkhorn
    threshold = 0.1 # Stopping threshold
    threshold_sinkhorn = 1e-9 # Stopping threshold in Sinkhorn
    nb_exp = 100 # Number of experiments
    
    tau = 0.001

    times_RBCD = np.zeros((nb_exp, nb_ds))
    times_RGAS = np.zeros((nb_exp, nb_ds))
    times_RABCD = np.zeros((nb_exp, nb_ds))
    times_RAGAS = np.zeros((nb_exp, nb_ds))
    times_SRW = np.zeros((nb_exp, nb_ds))
      
    for t in range(nb_exp):
        print(t)
        reg = 0.2
        for ind_d in range(nb_ds):
            d = ds[ind_d]

            a,b,X,Y = fragmented_hypercube(n,d,dim=2)
            
            if d>=250:
                reg=0.5

            U0 = InitialStiefel(d, k)

            print('RBCD')
            RBCD = RiemannianBlockCoordinateDescent(eta=reg, tau=tau, max_iter=max_iter, threshold=threshold, verbose=False)
            PRW = ProjectedRobustWasserstein(X, Y, a, b, RBCD, k)
            PRW.run( 'RBCD',tau, U0)
            times_RBCD[t,ind_d] = PRW.running_time
            print('RABCD')
            PRW.run( 'RABCD',tau, U0)
            times_RABCD[t,ind_d] = PRW.running_time


            RGAS = RiemannianGradientAscentSinkhorn(eta=reg, tau = tau/reg, max_iter=max_iter, threshold=threshold, 
                                                    sink_threshold=1e-8, verbose=False)
            PRW1 = ProjectedRobustWasserstein(X, Y, a, b, RGAS, k)
            PRW1.run('RGAS',tau/reg, U0)
            times_RGAS[t,ind_d] = PRW1.running_time
            print('RAGAS')
            PRW1.run('RAGAS',tau/reg, U0)
            times_RAGAS[t,ind_d] = PRW1.running_time
            
            print('FWSRW')
            algo = FrankWolfe(reg=reg, step_size_0=tau, max_iter=max_iter, max_iter_sinkhorn=max_iter_sinkhorn,
                              threshold=(0.1*tau)**2, 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("exp_hypercubic_fix_n")
    
    times_RBCD_mean = np.mean(times_RBCD, axis=0)
    times_RABCD_mean = np.mean(times_RABCD, axis=0)
    times_RGAS_mean = np.mean(times_RGAS, axis=0)
    times_RAGAS_mean = np.mean(times_RAGAS, axis=0)
    times_SRW_mean = np.mean(times_SRW, axis=0)
    
    
    print('RBCD &', "%.2f &" %times_RBCD_mean[0], "%.2f &" %times_RBCD_mean[1],"%.2f &" %times_RBCD_mean[2], "%.2f &" %times_RBCD_mean[3], "%.2f "% times_RBCD_mean[4],"\\ \hline")
    print('RABCD &', "%.2f &" %times_RABCD_mean[0], "%.2f &" %times_RABCD_mean[1],"%.2f &" %times_RABCD_mean[2], "%.2f &" %times_RABCD_mean[3], "%.2f "% times_RABCD_mean[4], "\\ \hline")
    print('RGAS &', "%.2f &" %times_RGAS_mean[0], "%.2f &" %times_RGAS_mean[1],"%.2f &" %times_RGAS_mean[2], "%.2f &" %times_RGAS_mean[3], "%.2f "% times_RGAS_mean[4], "\\ \hline")
    print('RAGAS &', "%.2f &" %times_RAGAS_mean[0], "%.2f &" %times_RAGAS_mean[1],"%.2f &" %times_RAGAS_mean[2], "%.2f &" %times_RAGAS_mean[3], "%.2f "% times_RAGAS_mean[4], "\\ \hline")
    print('SRW &',  "%.2f &" %times_SRW_mean[0], "%.2f &" %times_SRW_mean[1],"%.2f &" %times_SRW_mean[2], "%.2f &" %times_SRW_mean[3], "%.2f "% times_SRW_mean[4], "\\ \hline")
コード例 #14
0
values = np.zeros((4, nb_exp, d))
for dim_index in range(4):
    dim = dims[dim_index]
    print('----')
    for t in range(nb_exp):
        print(dim, t)
        a, b, X, Y = disk_annulus(n, d, dim)
        FW = FrankWolfe(reg=0.2,
                        step_size_0=None,
                        max_iter=15,
                        threshold=0.01,
                        max_iter_sinkhorn=50,
                        threshold_sinkhorn=10e-4,
                        use_gpu=True)
        SRW = SubspaceRobustWasserstein(X, Y, a, b, FW, k)
        SRW.run()
        values[dim_index, t, :] = np.sort(list(SRW.get_value().values()))

values_mean = np.mean(values, axis=1)
values_min = np.min(values, axis=1)
values_10 = np.percentile(values, 10, axis=1)
values_25 = np.percentile(values, 25, axis=1)
values_75 = np.percentile(values, 75, axis=1)
values_90 = np.percentile(values, 90, axis=1)
values_max = np.max(values, axis=1)

plt.figure(figsize=(17, 6))
col = []
for dim_index in range(4):
    mean, = plt.plot(range(1, d + 1),