How to use the node2vec.src.main.node2vec_main function in node2vec

To help you get started, we’ve selected a few node2vec examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github JiaxuanYou / graph-generation / main_0.py View on Github external
if os.path.isdir("logs"):
        shutil.rmtree("logs")
    # configure("logs/logs_toy", flush_secs=1)

    # clean saving directory
    if not os.path.exists("saves"):
        os.makedirs("saves")

    # Generate Graph
    # G = nx.karate_club_graph()
    G = nx.LCF_graph(14, [5, -5], 7)
    graphdataset = GraphDataset(G, shuffle_neighbour=False)
    # run node2vec
    for edge in G.edges():
        G[edge[0]][edge[1]]['weight'] = 1
    embedding = nv.node2vec_main(G, args)
    print(embedding.shape)

    embedding_size = embedding.shape[0] + 3

    embedding = torch.from_numpy(embedding).float().cuda()
    # normalize
    embedding = embedding / torch.mean(torch.norm(embedding, 2, 1))
    print(embedding)


    ##### parallel
    mp.set_start_method('spawn')
    decoder = DecoderRNN_step(input_size=input_size, hidden_size=64, embedding_size=embedding_size,
                                          n_layers=n_layers, is_bidirection=False, embedding_init_flag=True, embedding_init=embedding).cuda()
github JiaxuanYou / graph-generation / main_1.py View on Github external
configure("logs/logs_toy", flush_secs=1)

# clean saving directory
if not os.path.exists("saves"):
    os.makedirs("saves")

# Generate Graph
G = nx.karate_club_graph()
# G = nx.LCF_graph(14,[5,-5],7)
# G = nx.LCF_graph(20,[-9,-9],10)

graphdataset = GraphDataset(G, shuffle_neighbour = True)
# run node2vec
for edge in G.edges():
    G[edge[0]][edge[1]]['weight'] = 1
embedding = nv.node2vec_main(G, args)
# print(embedding)
print('embedding.shape', embedding.shape)

embedding_dist = np.zeros((embedding.shape[0],embedding.shape[0]))

G_adj = np.asarray(nx.to_numpy_matrix(G))
# print(np.std(G_adj,axis=0)/np.mean(G_adj,axis=0))
G_adj_sum = np.repeat(np.sum(G_adj, axis = 1, keepdims=True), G_adj.shape[0] ,axis=1)

alpha = 10
print('alpha', alpha)
for i in range(embedding_dist.shape[0]):
    for j in range(embedding_dist.shape[1]):
        if i!=j:
            embedding_dist[i][j] = np.exp(embedding[i] @ embedding[j].transpose() * alpha)