def get_embeds(g): d = {} for n in g.nodes: d[n] = str(n) g1 = nx.relabel_nodes(g, d) graph_model = DeepWalk(g1, num_walks=args.num_walks, walk_length=args.walk_len, workers=args.cpu if args.cpu > 0 else cpu_count()) graph_model.train(window_size=args.win, iter=args.emb_iters, embed_size=action_dim) embs = {} emb1 = graph_model.get_embeddings() for n in emb1.keys(): embs[int(n)] = emb1[n] return embs
X, Y = read_node_label('../data/wiki_labels.txt') emb_list = [] for k in X: emb_list.append(embeddings[k]) emb_list = np.array(emb_list) model = TSNE(n_components=2) node_pos = model.fit_transform(emb_list) color_idx = {} for i in range(len(X)): color_idx.setdefault(Y[i][0], []) color_idx[Y[i][0]].append(i) for c, idx in color_idx.items(): plt.scatter(node_pos[idx, 0], node_pos[idx, 1], label=c) plt.legend() plt.show() if __name__ == '__main__': G = nx.read_edgelist('../data/Wiki_edgelist.txt', create_using=nx.DiGraph(), nodetype=None, data=[("weight", int)]) print(len(G.nodes())) model = DeepWalk(G, walk_length=10, num_walks=80, workers=1) model.train(w_size=5, iter_num=3) embeddings = model.get_embedding_all() evaluate_embeddings(embeddings) plot_embeddings(embeddings)
from deepwalk import DeepWalk random.seed(616) edges = np.load('../tencent/train_edges.npy') G = nx.Graph() for i in range(169209): G.add_node(i) G.add_edges_from(edges) deepwalk = DeepWalk(G, emb_size=128, length_walk=50, num_walks=10, window_size=10, num_iters=2) w2v = deepwalk.train(workers=4, is_loadmodel=False, is_loaddata=False) pos_test = np.load('../tencent/test_edges.npy') neg_test = np.load('../tencent/test_edges_false.npy') y_true = [True] * pos_test.shape[0] + [False] * neg_test.shape[0] X = np.vstack([pos_test, neg_test]) print('Testing...') y_score = [] for u, v in X: y_score.append(w2v.wv.similarity(str(u), str(v))) auc_test = roc_auc_score(y_true, y_score) print('Tencent, test AUC:', auc_test)
Author : haxu dateļ¼ 2019/4/3 ------------------------------------------------- Change Activity: 2019/4/3: ------------------------------------------------- """ __author__ = 'haxu' import networkx as nx from deepwalk import DeepWalk from classify import read_node_label, Classifier from sklearn.linear_model import LogisticRegression if __name__ == '__main__': G = nx.read_edgelist('../data/Wiki_edgelist.txt', create_using=nx.DiGraph(), nodetype=None, data=[('weight', int)]) model = DeepWalk(G, walk_length=30, num_walks=80, workers=4) model.train(window_size=5, iter=3) embeddings = model.get_embeddings() X, Y = read_node_label('../data/wiki_labels.txt') tr_frac = 0.8 clf = Classifier(embeddings=embeddings, clf=LogisticRegression()) clf.split_train_evaluate(X, Y, tr_frac)