Пример #1
0
def main():

    k = 2
    reg = 0.1
    tau = 0.001

    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]
                
                U0 = InitialStiefel(300, 2)

                algo = RiemannianBlockCoordinateDescent(eta=reg, tau=tau, max_iter=1000, threshold=0.001,use_gpu=False, verbose=False)

                PRW = ProjectedRobustWasserstein(X, Y, a, b, algo, k)
                PRW.run('RABCD',tau,  U0)

                PRW_matrix[i, j] = PRW.get_value()
                PRW_matrix[j, i] = PRW_matrix[i, j]
                print('RBCD  PRW (', art1, ',', art2, ') =', PRW_matrix[i, j])
                
                
                algo1 = RiemannianGradientAscentSinkhorn(eta=reg, tau=tau/reg, max_iter=1000, threshold=0.001,
                                        sink_threshold=1e-9, use_gpu=False, verbose=True)

                PRW1 = ProjectedRobustWasserstein(X, Y, a, b, algo1, k)
                PRW1.run('RAGAS', tau/reg, U0)

                PRW1_matrix[i, j] = PRW1.get_value()
                PRW1_matrix[j, i] = PRW1_matrix[i, j]
                print('RGAS PRW (', art1, ',', art2, ') =', PRW1_matrix[i, j])
Пример #2
0
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
def main():

    noise_level = 1
    d = 20  # Total dimension
    n = 100  # Number of points for each measure
    l = 5  # Dimension of Wishart
    nb_exp = 100  # Number of experiments
    k = list(range(1, d + 1))  # Compute SRW for all dimension parameter k

    # Save the values
    no_noise = np.zeros((nb_exp, d))
    noise = np.zeros((nb_exp, d))

    eta = 1
    tau = 0.0005
    verb = True

    if 1 == 1:
        for t in range(nb_exp):  # Fore each experiment
            print(t)

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

            mean_1 = 0. * np.random.randn(d)
            mean_2 = 0. * np.random.randn(d)

            cov_1 = np.random.randn(d, l)
            cov_1 = cov_1.dot(cov_1.T)
            cov_2 = np.random.randn(d, l)
            cov_2 = cov_2.dot(cov_2.T)

            # Draw measures
            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)

            vals = []
            for k in range(1, d + 1):
                algo = RiemannianBlockCoordinateDescent(eta=eta,
                                                        tau=tau,
                                                        max_iter=5000,
                                                        threshold=0.1,
                                                        verbose=verb)
                PRW = ProjectedRobustWasserstein(X, Y, a, b, algo, k)
                PRW.run('RBCD', tau=tau)
                vals.append(PRW.get_value())
            no_noise[t, :] = np.sort(vals)

            vals = []
            for k in range(1, d + 1):
                algoe = RiemannianBlockCoordinateDescent(eta=eta,
                                                         tau=tau,
                                                         max_iter=5000,
                                                         threshold=0.1,
                                                         verbose=verb)
                PRWe = ProjectedRobustWasserstein(Xe, Ye, a, b, algoe, k)
                PRWe.run('RBCD', tau=0.0005)
                vals.append(PRWe.get_value())
            noise[t, :] = np.sort(vals)

            no_noise[t, :] /= no_noise[t, (d - 1)]
            noise[t, :] /= noise[t, (d - 1)]
        with open('./results/exp2_noise_12.pkl', 'wb') as f:
            pickle.dump([no_noise, noise], f)

    else:

        with open('./results/exp2_noise_12.pkl', 'rb') as f:
            no_noise, noise = pickle.load(f)

    captions = ['PRW']
    plt.figure(figsize=(12, 8))

    no_noise_t = no_noise[:, :]
    no_noise_mean = np.mean(no_noise_t, axis=0)
    no_noise_min = np.min(no_noise_t, axis=0)
    no_noise_10 = np.percentile(no_noise_t, 10, axis=0)
    no_noise_25 = np.percentile(no_noise_t, 25, axis=0)
    no_noise_75 = np.percentile(no_noise_t, 75, axis=0)
    no_noise_90 = np.percentile(no_noise_t, 90, axis=0)
    no_noise_max = np.max(no_noise_t, axis=0)

    noise_t = noise[:, :]
    noise_mean = np.mean(noise_t, axis=0)
    noise_min = np.min(noise_t, axis=0)
    noise_10 = np.percentile(noise_t, 10, axis=0)
    noise_25 = np.percentile(noise_t, 25, axis=0)
    noise_75 = np.percentile(noise_t, 75, axis=0)
    noise_90 = np.percentile(noise_t, 90, axis=0)
    noise_max = np.max(noise_t, axis=0)

    plotnonoise, = plt.plot(range(d),
                            no_noise_mean,
                            'C1',
                            label='Without Noise',
                            lw=6)
    col_nonoise = plotnonoise.get_color()
    plt.fill_between(range(d),
                     no_noise_25,
                     no_noise_75,
                     facecolor=col_nonoise,
                     alpha=0.3)
    plt.fill_between(range(d),
                     no_noise_10,
                     no_noise_90,
                     facecolor=col_nonoise,
                     alpha=0.2)
    plt.fill_between(range(d),
                     no_noise_min,
                     no_noise_max,
                     facecolor=col_nonoise,
                     alpha=0.15)

    plotnoise, = plt.plot(range(d), noise_mean, 'C2', label='With Noise', lw=6)
    col_noise = plotnoise.get_color()
    plt.fill_between(range(d),
                     noise_25,
                     noise_75,
                     facecolor=col_noise,
                     alpha=0.3)
    plt.fill_between(range(d),
                     noise_10,
                     noise_90,
                     facecolor=col_noise,
                     alpha=0.2)
    plt.fill_between(range(d),
                     noise_min,
                     noise_max,
                     facecolor=col_noise,
                     alpha=0.15)

    plt.xlabel('Dimension', fontsize=25)
    plt.ylabel('Normalized %s value' % (captions[0]), fontsize=25)
    plt.legend(loc='best', fontsize=20)

    plt.yticks(fontsize=20)
    plt.xticks(range(d), range(1, d + 1), fontsize=20)
    plt.ylim(0.1)

    plt.legend(loc='best', fontsize=25)
    plt.title('%s distance with different dimensions' % (captions[0], ),
              fontsize=30)
    plt.grid(ls=':')
    # plt.savefig('figs/exp2_noise_%d.png' % (0,))
    plt.show()
    plt.close()
    plt.clf()
Пример #4
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
    tau = 0.004

    PRW_matrix = np.zeros((10, 10))
    PRW1_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)

            U0 = InitialStiefel(d, k)

            algo1 = RiemannianBlockCoordinateDescent(eta=reg,
                                                     tau=tau,
                                                     max_iter=5000,
                                                     threshold=0.1,
                                                     use_gpu=False)
            PRW1 = ProjectedRobustWasserstein(X, Y, a, b, algo1, k)
            PRW1.run('RBCD', tau, U0)
            PRW1_matrix[j, i] = PRW1.get_value() / 1000.0
            PRW1_matrix[i, j] = PRW1.get_time()
            print('PRW (', i, ',', j, ') =', PRW1_matrix[j, i])

            # Compute PRW
            algo = RiemannianGradientAscentSinkhorn(eta=reg,
                                                    tau=tau / reg,
                                                    max_iter=5000,
                                                    threshold=0.1,
                                                    sink_threshold=1e-9,
                                                    use_gpu=False)
            PRW = ProjectedRobustWasserstein(X, Y, a, b, algo, k)
            PRW.run('RGAS', tau / reg, U0)
            PRW_matrix[j, i] = PRW.get_value() / 1000.0
            PRW_matrix[i, j] = PRW.get_time()
            print('PRW (', i, ',', j, ') =', PRW_matrix[j, i])

    for i in range(10):
        print('%s ' % (i), end=' ')
        for j in range(10):
            print('& %.2f' % (PRW_matrix[i, j]),
                  '/%.2f' % (PRW1_matrix[i, j]),
                  end='')
        print('\\\\ \hline')
    print()
Пример #5
0
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")
Пример #6
0
def main():

    n = 100 # Number of points for each measure
    d = 30 # Total dimension
    maxK = 30 # Compute SRW for all parameters 'k'
    nb_exp = 50 # Do 100 experiments
    kstars = [2, 4, 7, 10] # Plot for 'true' dimension k* = 2, 4, 7, 10
    
    eta = 0.2
    tau = 0.005
    verb = True

    values = np.zeros((2, len(kstars), maxK, nb_exp))
    
    if 1==1:
        for t in range(nb_exp):
            for kstar_index in range(len(kstars)):
                kstar = kstars[kstar_index]
                for kdim in range(1, maxK+1):

                    a,b,X,Y = fragmented_hypercube(n,d,kstar)

                    algo = RiemannianBlockCoordinateDescent(eta=eta, tau=None, max_iter=3000, threshold=0.1, verbose=verb)
                    PRW = ProjectedRobustWasserstein(X, Y, a, b, algo, kdim)
                    PRW.run('RBCD', tau)
                    values[0, kstar_index, kdim - 1, t] = np.abs(PRW.get_value())

                    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, kdim)
                    PRW1.run('RGAS', tau/eta)
                    values[1, kstar_index, kdim - 1, t] = np.abs(PRW1.get_value())
                    
        with open('./results/exp1_hc_dim_k.pkl', 'wb') as f:
            pickle.dump(values, f)
    else:
        with open('./results/exp1_hypercube_dim_k.pkl', 'rb') as f:
            values = pickle.load(f)

    colors = [['b', 'orange', 'g', 'r'], ['c', 'm', 'y', 'purple']]
    plt.figure(figsize=(20, 8))

    Xs = list(range(1, maxK + 1))
    line_styles = ['-', '--']
    captions = ['RBCD', 'RGAS']
    for t in range(2):
        for i, kstar in enumerate(kstars):
            values_mean = np.mean(values[t, i, :, :], axis=1)
            values_min = np.min(values[t, i, :, :], axis=1)
            values_max = np.max(values[t, i, :, :], axis=1)

            mean, = plt.plot(Xs, values_mean, ls=line_styles[t],
                             c=colors[t][i], lw=4, ms=20,
                             label='$k^*=%d$, %s' % (kstar,captions[t]))
            col = mean.get_color()
            plt.fill_between(Xs, values_min, values_max, facecolor=col, alpha=0.15)

    for i in range(len(kstars)):
        ks = kstars[i]
        vm1 = np.mean(values[0, i, ks, :], axis=0)
        vm2 = np.mean(values[1, i, ks, :], axis=0)
        print(vm1,vm2)
        tt = max(vm1,vm2)
        plt.plot([ks, ks], [0, tt], color=colors[0][i], linestyle='--')


    plt.xlabel('Dimension k', fontsize=25)
    plt.ylabel('PRW values', fontsize=25)
    plt.ylabel('$P_k^2(\hat\mu, \hat\\nu)$', fontsize=25)
    plt.xticks(Xs, fontsize=20)
    plt.yticks(np.arange(10, 70+1, 10), fontsize=20)
    plt.legend(loc='best', fontsize=18, ncol=2)
    plt.ylim(0)
    plt.title('$P_k^2(\hat\mu, \hat\\nu)$ depending on dimension k', fontsize=30)
    plt.minorticks_on()
    plt.grid(ls=':')
    plt.savefig('figs/exp1_dim_k.png')
Пример #7
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')
Пример #8
0
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")
Пример #9
0
def main():
    scripts = [
        'DUNKIRK.txt', 'GRAVITY.txt', 'INTERSTELLAR.txt',
        'KILL_BILL_VOLUME_1.txt', 'KILL_BILL_VOLUME_2.txt', 'THE_MARTIAN.txt',
        'TITANIC.txt'
    ]
    Nb_scripts = len(scripts)
    PRW_matrix = np.zeros((Nb_scripts, Nb_scripts))
    PRW1_matrix = np.zeros((Nb_scripts, Nb_scripts))
    PRW_matrix_adap = np.zeros((Nb_scripts, Nb_scripts))
    PRW1_matrix_adap = np.zeros((Nb_scripts, Nb_scripts))
    measures = []
    for film in scripts:
        measures.append(load_text('Data/movies/' + film))

    nb_exp = 10

    times_RBCD = np.zeros((Nb_scripts, Nb_scripts))
    times_RGAS = np.zeros((Nb_scripts, Nb_scripts))
    times_RABCD = np.zeros((Nb_scripts, Nb_scripts))
    times_RAGAS = np.zeros((Nb_scripts, Nb_scripts))

    for t in range(nb_exp):
        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]

                    U0 = InitialStiefel(300, 2)

                    reg = 0.1
                    tau = 0.1

                    RBCD = RiemannianBlockCoordinateDescent(eta=reg,
                                                            tau=tau,
                                                            max_iter=1000,
                                                            threshold=0.001)
                    PRW_ = ProjectedRobustWasserstein(X, Y, a, b, RBCD, k=2)
                    PRW_.run('RBCD', tau, U0)
                    PRW_matrix[i, j] = PRW_.get_value()
                    times_RBCD[i, j] = times_RBCD[i, j] + PRW_.get_time()
                    print('RBCD (', film1, ',', film2, ') =', PRW_matrix[i, j])

                    RGAS = RiemannianGradientAscentSinkhorn(eta=reg,
                                                            tau=tau / reg,
                                                            max_iter=1000,
                                                            threshold=0.001)
                    PRW1_ = ProjectedRobustWasserstein(X, Y, a, b, RGAS, k=2)
                    PRW1_.run('RGAS', tau / reg, U0)
                    PRW1_matrix[i, j] = PRW1_.get_value()
                    times_RGAS[i, j] = times_RBCD[i, j] + PRW1_.get_time()
                    print('RGAS (', film1, ',', film2, ') =', PRW1_matrix[i,
                                                                          j])