Ejemplo n.º 1
0
def average_pearsonr(mpdists, gpdists):
    """Computes the correlation factor on a per-node basis and then takes the
    average.
    """
    mpdists = squareform1(mpdists)
    gpdists = squareform1(gpdists)

    mpdists.sub_(mpdists.mean(axis=1))
    gpdists.sub_(gpdists.mean(axis=1))
    rs_num = (mpdists * gpdists).sum(axis=1)
    rs_denom = (mpdists.norm(dim=1) * gpdists.norm(dim=1))

    return (rs_num / rs_denom).mean()
Ejemplo n.º 2
0
def main():
    torch.set_default_dtype(torch.float64)

    save_dir = os.path.join(args.output_dir, args.manifold)
    check_mkdir(save_dir, increment=False)

    # generate the samples on the manifold, uniformly around "origin"
    man = build_manifold(args.manifold)[0]
    if args.use_rs:
        samples = gen_samples_rs(man, args.num_nodes, args.radius)
    elif args.use_gen2:
        samples = gen_samples2(man, args.num_nodes, args.radius)
    else:
        samples = gen_samples(man, args.num_nodes, args.radius)
    torch.save(samples, os.path.join(save_dir, 'xs.pt'))
    dists = squareform1(man.pdist(samples))
    plot_distances(dists, save_dir)

    # create the graph
    g = nx.Graph()
    g.add_edges_from(np.argwhere(dists.numpy() < args.radius))
    g.remove_edges_from(nx.selfloop_edges(g))

    # plots
    plot_degree_distribution(g, save_dir)
    plot_points(man, samples, args.radius, save_dir)
Ejemplo n.º 3
0
def grid_fn(fn, man, radius, output_dir):
    # sample
    xs = fn(man, args.num_nodes, radius)
    pdists = man.pdist(xs)

    # save and plot the pdists
    path = os.path.join(output_dir, 'pdists.npy')
    np.save(path, pdists.numpy())
    path = os.path.join(output_dir, 'pdists.pdf')
    plot_distances(pdists, path, axvline=radius, xmax=2 * radius)

    # save and plot the distances from zero too
    zeros = man.zero(len(xs))
    zero_dists = man.dist(zeros, xs)
    path = os.path.join(output_dir, 'zero_dists.npy')
    np.save(path, zero_dists)
    path = os.path.join(output_dir, 'zero_dists.pdf')
    plot_distances(zero_dists, path, xmax=radius)

    pdists = squareform1(pdists).numpy()
    thresholds = np.linspace(radius / 10, radius, args.num_thresholds)
    for threshold in thresholds:
        # create the graph
        g = nx.Graph()
        g.add_edges_from(np.argwhere(pdists < threshold))
        g.remove_edges_from(nx.selfloop_edges(g))

        # save it
        save_dir = os.path.join(output_dir, f'{threshold:.2f}')
        check_mkdir(save_dir, increment=False)
        torch.save(xs, os.path.join(save_dir, 'xs.pt'))
        nx.write_edgelist(g, os.path.join(save_dir, 'graph.edges.gz'))
Ejemplo n.º 4
0
def main():
    torch.set_default_dtype(torch.float64)

    save_dir = os.path.join(args.output_dir, args.manifold)
    check_mkdir(save_dir, increment=False)
    man = build_manifold(args.manifold)[0]

    # generate the samples on the manifold, uniformly around "origin"
    if args.use_rs:
        samples = gen_samples_rs(man, args.num_nodes, args.radius,
                                 np.sqrt(args.curvature_r_squared))
    else:
        samples = gen_samples_exp(man, args.num_nodes, args.radius)
    plot_points(man, samples, args.radius, save_dir)

    # the pairwise distances
    torch.save(samples, os.path.join(save_dir, 'xs.pt'))
    dists = squareform1(pdists(man, samples))
    plot_distances(dists, save_dir)

    # create the graph
    g = nx.Graph()
    g.add_edges_from(np.argwhere(dists.numpy() < args.radius))
    g.remove_edges_from(nx.selfloop_edges(g))
    # save it
    filename = '{}_n{}_r{:.2f}'.format(args.manifold, args.num_nodes,
                                       args.radius)
    filename = filename.replace('.', 'p') + '.edges.gz'
    nx.write_edgelist(g, os.path.join(save_dir, filename))

    # plots
    plot_degree_distribution(g, save_dir)
    plot_graph_distances(g, save_dir)
Ejemplo n.º 5
0
 def __call__(self, gdists, mdists, *, epoch=None, alpha=None):
     dists = squareform1(mdists)
     ms, bs, cs = self._sample_nodes(device=dists.device)
     am = dists.gather(dim=1, index=ms)
     ab = dists.gather(dim=1, index=bs)
     ac = dists.gather(dim=1, index=cs)
     bc = dists[bs, cs]
     reg_terms = am + 0.25 * bc - 0.5 * (ab + ac)
     return -self.lambda_reg * reg_terms.abs().sum()
Ejemplo n.º 6
0
def plot_distances(dists, output_dir):
    plt.hist(squareform1(dists), bins=20)
    plt.axvline(args.radius, color='red')
    plt.xlabel('Distance')
    plt.ylabel('Number of pairs')
    xmax = 2 * args.radius if not args.dist_limit else args.dist_limit
    plt.xlim(xmin=0, xmax=xmax)
    plt.gca().get_yaxis().set_major_formatter(
        plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
    plt.title('manifold = {}, radius = {}'.format(args.manifold, args.radius))
    plt.tight_layout()
    plt.savefig(os.path.join(output_dir, 'dists.pdf'))
    plt.close()
Ejemplo n.º 7
0
    def __call__(self, theta, ret_margs=False):
        n_nodes = nnm1d2_to_n(len(theta))
        theta.requires_grad_()  # Needed by `torch.grad` below!

        # Construct the Laplacian.
        L_off = squareform1(torch.exp(theta))
        L_diag = torch.diag(L_off.sum(1))
        L = L_diag - L_off
        L = L[1:, 1:]

        logz = tb.plogdet(L)
        if not ret_margs:
            return logz, None

        margs, = torch.autograd.grad(logz, theta, create_graph=True)
        with torch.no_grad():
            assert abs(margs.sum() - (n_nodes - 1)) < 1e-4
        return logz, margs
Ejemplo n.º 8
0
def main():
    torch.set_default_dtype(torch.float64)
    logging.getLogger().setLevel(logging.DEBUG)

    save_dir = os.path.join(args.output_dir, args.manifold)
    check_mkdir(save_dir, increment=False)
    man = build_manifold(args.manifold)[0]

    # generate the samples
    xs = random_walk_graph(man, args.num_nodes, args.radius)
    torch.save(xs, os.path.join(save_dir, 'xs.pt'))
    plot_points(man, xs, args.radius, save_dir)

    # the pairwise distances
    pdists = man.pdist(xs)
    xmax = 2 * args.radius if not args.dist_limit else args.dist_limit
    plot_distances(pdists, os.path.join(save_dir, 'pdists.pdf'), xmax)
    pdists = squareform1(pdists)

    # the distances from 0
    zeros = man.zero(len(xs))
    zero_dists = man.dist(zeros, xs)
    xmax = args.radius
    plot_distances(zero_dists, os.path.join(save_dir, 'zero_dists.pdf'), xmax)

    # create the graph
    g = nx.Graph()
    threshold = args.link_distance if args.link_distance else args.radius
    g.add_edges_from(np.argwhere(pdists.numpy() < threshold))
    g.remove_edges_from(nx.selfloop_edges(g))

    # save it
    filename = '{}_n{}_r{:.2f}_ld{:.2f}'.format(args.manifold, args.num_nodes,
                                                args.radius, threshold)
    filename = filename.replace('.', 'p') + '.edges.gz'
    nx.write_edgelist(g, os.path.join(save_dir, filename))

    # plots
    plot_degree_distribution(g, save_dir)
    plot_graph_distances(g, save_dir)
Ejemplo n.º 9
0
 def __init__(self, pdists):
     # We store the max-normalized squared distances.
     pdists = pdists.pow(2)
     pdists.div_(pdists.max())
     self.pdists = squareform1(pdists)