] not in edges_temp and edges[j][1] != edges[j][0]: edges_temp.append(edges[j]) # 找到节点数 for z in edges[j]: node_num = max(node_num, z) edges_list.append(edges_temp) node_num += 1 for edges in edges_list: graph = nx.DiGraph() graph.add_nodes_from([i for i in range(node_num)]) graph.add_edges_from(edges) graphs.append(graph) if 'enron_large' in basepath: model = DeepWalk(graph, walk_length=10, num_walks=80, workers=1) model.train(window_size=5, iter=3) else: model = DeepWalk(graph, walk_length=10, num_walks=800, workers=1) model.train(window_size=5, iter=3) embeddings = model.get_embeddings() embeddings_list.append(embeddings) pred_edges = [] emb_matrix = np.zeros([node_num, 128]) for node, emb in embeddings.items(): node = int(node) emb = emb.reshape((1, emb.shape[0]))
def get_embeddings(self, inst, th=1): G = nx.parse_edgelist(self._compose_edge_list(inst.dist_mat, th), create_using=nx.DiGraph(), nodetype=None, data=[('weight', float)]) if self._embedding == 'deepwalk': model = DeepWalk(G, walk_length=10, num_walks=80, workers=1) model.train(window_size=5, iter=3) elif self._embedding == 'node2vec': model = Node2Vec(G, walk_length=10, num_walks=80, p=0.25, q=4, workers=1) # init model model.train(window_size=5, iter=3) # train model elif self._embedding == 'line': model = LINE(G, embedding_size=128, order='second') # init model,order can be ['first','second','all'] model.train(batch_size=1024, epochs=50, verbose=2) # train model elif self._embedding == 'sdne': model = SDNE(G, hidden_size=[256, 128]) # init model model.train(batch_size=3000, epochs=40, verbose=2) # train model elif self._embedding == 'struc2vec': model = Struc2Vec(G, 10, 80, workers=4, verbose=40, ) # init model model.train(window_size=5, iter=3) # train model else: return self._normalise(inst) ebds = model.get_embeddings() coords = [] for i in range(inst.n): coords.append(ebds[str(i)]) return np.array(coords)