def main(graph_file_name, path_file_name): g = Graph() print("Reading...") g.read_edgelist(filename=graph_file_name) walker_class = walker.BasicWalker(g, workers=8) sentences = walker_class.simulate_walks( num_walks=10, walk_length=10) fw = codecs.open(path_file_name, "w", "utf-8") for path in sentences: for i in range(0, len(path)): if i < len(path) - 1: fw.write(str(path[i] + " ")) else: fw.write(str(path[i] + "\n")) fw.close()
def __init__(self, graph, path_length, num_paths, dim, p=1.0, q=1.0, dw=False, **kwargs): kwargs["workers"] = kwargs.get("workers", 1) if dw: kwargs["hs"] = 1 p = 1.0 q = 1.0 self.graph = graph if dw: self.walker = walker.BasicWalker(graph, workers=kwargs["workers"]) else: self.walker = walker.Walker(graph, p=p, q=q, workers=kwargs["workers"]) # self.walker = walker.QuantumWalker(graph, p=p, q=q, workers=kwargs["workers"]) print("Preprocess transition probs...") self.walker.preprocess_transition_probs() sentences = self.walker.simulate_walks(num_walks=num_paths, walk_length=path_length) kwargs["sentences"] = sentences kwargs["min_count"] = kwargs.get("min_count", 0) kwargs["size"] = kwargs.get("size", dim) kwargs["sg"] = 1 self.size = kwargs["size"] print("Learning representation...") word2vec = Word2Vec(**kwargs) self.vectors = {} self.embeddings = np.zeros((graph.node_size, dim)) for word in graph.G.nodes(): self.vectors[word] = word2vec[word] self.embeddings[graph.look_up_dict[word]] = word2vec[word] del word2vec
def __init__(self, graph, path_length, num_paths, dim, p=1.0, q=1.0, dw=0, **kwargs): kwargs["workers"] = kwargs.get("workers", 1) if dw: kwargs["hs"] = 1 p = 1.0 q = 1.0 self.graph = graph if dw == 1: self.walker = walker.BasicWalker(graph, workers=kwargs["workers"]) elif dw == 2: self.walker = walker.MHWalker(graph, workers=kwargs["workers"]) else: self.walker = walker.Walker(graph, p=p, q=q, workers=kwargs["workers"]) print("Preprocess transition probs...") self.walker.preprocess_transition_probs() sentences = self.walker.simulate_walks(num_walks=num_paths, walk_length=path_length) kwargs["sentences"] = sentences kwargs["min_count"] = kwargs.get("min_count", 0) kwargs["size"] = kwargs.get("size", dim) kwargs["sg"] = 1 self.size = kwargs["size"] print("Learning representation...") word2vec = Word2Vec(**kwargs) self.vectors = {} for word in graph.G.nodes(): self.vectors[word] = word2vec.wv[word] del word2vec
def __init__(self, graph, path_length, num_paths, dim, p=1.0, q=1.0, dw=False, **kwargs): kwargs["workers"] = kwargs.get("workers", 5) if dw: kwargs["hs"] = 0 p = 1.0 q = 1.0 t1 = time.time() self.graph = graph if dw: self.walker = walker.BasicWalker(graph, workers=kwargs["workers"],walk_length=path_length) t2 = time.time() self.walker.preprocess_transition_probs() t3 = time.time() print("prob time: "+str(t3-t2)) sentences = self.walker.simulate_walks(num_walks=num_paths, walk_length=path_length) else: self.walker = walker.Walker( graph, p=p, q=q, workers=kwargs["workers"]) t2 = time.time() self.walker.preprocess_transition_probs() t3 = time.time() print("prob time: "+str(t3-t2)) sentences = self.walker.simulate_walks(num_walks=num_paths, walk_length=path_length) t2 = time.time() print("walking time: "+str(t2-t1)) kwargs["sentences"] = sentences kwargs["min_count"] = kwargs.get("min_count", 0) kwargs["size"] = kwargs.get("size", dim) kwargs["sg"] = 1 self.size = kwargs["size"] # print("Learning representation...") word2vec = Word2Vec(**kwargs) self.vectors = {} for word in graph.G.nodes(): self.vectors[word] = word2vec.wv[word] del word2vec