-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalker.py
159 lines (132 loc) · 5.14 KB
/
walker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import multiprocessing
import numpy as np
import random
from utils import alias_sample, create_alias_table
class RandomWalker:
def __init__(self, G, p=1, q=1, types=1):
"""
:param G:
:param p: Return parameter,controls the likelihood of immediately revisiting a node in the walk.
:param q: In-out parameter,allows the search to differentiate between “inward” and “outward” nodes
:param types: choose word2vec or autoencoder
"""
self.G = G
self.p = p
self.q = q
self.types = types
def deepwalk_walk(self, walk_length, start_node):
walk = [start_node]
while len(walk) < walk_length:
cur = walk[-1]
cur_nbrs = list(self.G.neighbors(cur))
if len(cur_nbrs) > 0:
walk.append(random.choice(cur_nbrs))
else:
break
return walk
def node2vec_walk(self, walk_length, start_node):
alias_nodes = self.alias_nodes
alias_edges = self.alias_edges
walk = [start_node]
while len(walk) < walk_length:
cur = walk[-1]
cur_nbrs = list(self.G.neighbors(cur))
if len(cur_nbrs) > 0:
if len(walk) == 1:
walk.append(
cur_nbrs[alias_sample(alias_nodes[cur][0], alias_nodes[cur][1])])
else:
prev = walk[-2]
edge = (prev, cur)
next_node = cur_nbrs[alias_sample(alias_edges[edge][0],
alias_edges[edge][1])]
walk.append(next_node)
else:
break
return walk
def simulate_walks(self, num_walks=100, walk_length=40, workers=5):
G = self.G
proceeding = []
num_nodes = int(len(G.adj) / workers)
block = []
for i in range(workers - 1):
block.append((int(i * num_nodes), int((i + 1) * num_nodes)))
block.append((int((workers - 1) * num_nodes), len(G.adj)))
pool = multiprocessing.Pool(processes=workers)
for i, (start, end) in enumerate(block):
proceeding.append(pool.apply_async(self._simulate_walks, (start, end, num_walks, walk_length)))
pool.close()
pool.join()
if self.types == 1:
corpus = []
for p in proceeding:
corpus.extend(p.get())
return corpus
walk_structure = np.zeros_like(self.G.adj)
for p in proceeding:
walk_structure += p.get()
walk_structure = np.where(walk_structure > 0, 1, 0)
return walk_structure
def _simulate_walks(self, start, end, num_walks, walk_length,):
walks = []
nodes = [node for node in range(start, end)]
for _ in range(num_walks):
random.shuffle(nodes)
for v in nodes:
if self.p == 1 and self.q == 1:
walks.append(self.deepwalk_walk(
walk_length=walk_length, start_node=v))
else:
walks.append(self.node2vec_walk(
walk_length=walk_length, start_node=v))
if self.types == 1:
for i, walk in enumerate(walks):
walks[i] = list(map(str, walk))
return walks
walk_structure = np.zeros_like(self.G.adj)
for walk in walks:
walk_structure[walk[0]][walk[1:]] = 1
return np.array(walk_structure)
def get_alias_edge(self, t, v):
"""
compute unnormalized transition probability between nodes v and its neighbors give the previous visited node t.
:param t:
:param v:
:return:
"""
G = self.G
p = self.p
q = self.q
unnormalized_probs = []
for x in G.neighbors(v):
weight = G.adj[v][x] # w_vx
if x == t: # d_tx == 0
unnormalized_probs.append(weight/p)
elif G.has_edge(x, t): # d_tx == 1
unnormalized_probs.append(weight)
else: # d_tx > 1
unnormalized_probs.append(weight/q)
norm_const = sum(unnormalized_probs)
normalized_probs = [
float(u_prob)/norm_const for u_prob in unnormalized_probs]
return create_alias_table(normalized_probs)
def preprocess_transition_probs(self):
"""
Preprocessing of transition probabilities for guiding the random walks.
"""
G = self.G
alias_nodes = {}
for node in G.nodes:
unnormalized_probs = [G.adj[node][nbr]
for nbr in G.neighbors(node)]
norm_const = sum(unnormalized_probs)
normalized_probs = [
float(u_prob)/norm_const for u_prob in unnormalized_probs]
alias_nodes[node] = create_alias_table(normalized_probs)
alias_edges = {}
for edge in G.edges:
edge = tuple(edge)
alias_edges[edge] = self.get_alias_edge(edge[0], edge[1])
self.alias_nodes = alias_nodes
self.alias_edges = alias_edges
return