-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_walks.py
247 lines (196 loc) · 9.5 KB
/
random_walks.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 19 16:20:09 2020
@author: georgia
"""
import random
import numpy as np
import os
from collections import defaultdict
from joblib import Parallel, delayed, load, dump
from tqdm import tqdm
class RandomWalk:
FIRST_TRAVEL_KEY = 'first_travel_key'
PROBABILITIES_KEY = 'probabilities'
NEIGHBORS_KEY = 'neighbors'
WEIGHT_KEY = 'weight'
NUM_WALKS_KEY = 'num_walks'
WALK_LENGTH_KEY = 'walk_length'
P_KEY = 'p'
Q_KEY = 'q'
def __init__(self, graph, walk_length=80, num_walks=10, p=1, q=1, weight_key='weight',
workers=1, sampling_strategy=None, quiet=False, temp_folder=None):
"""
Initiates the Node2Vec object, precomputes walking probabilities and generates the walks.
:param graph: Input graph
:type graph: Networkx Graph
:param walk_length: Number of nodes in each walk (default: 80)
:type walk_length: int
:param num_walks: Number of walks per node (default: 10)
:type num_walks: int
:param p: Return hyper parameter (default: 1)
:type p: float
:param q: Inout parameter (default: 1)
:type q: float
:param weight_key: On weighted graphs, this is the key for the weight attribute (default: 'weight')
:type weight_key: str
:param workers: Number of workers for parallel execution (default: 1)
:type workers: int
:param sampling_strategy: Node specific sampling strategies, supports setting node specific 'q', 'p', 'num_walks' and 'walk_length'.
Use these keys exactly. If not set, will use the global ones which were passed on the object initialization
:param temp_folder: Path to folder with enough space to hold the memory map of self.d_graph (for big graphs); to be passed joblib.Parallel.temp_folder
:type temp_folder: str
"""
self.graph = graph
self.walk_length = walk_length
self.num_walks = num_walks
self.p = p
self.q = q
self.weight_key = weight_key
self.workers = workers
self.quiet = quiet
self.d_graph = defaultdict(dict)
if sampling_strategy is None:
self.sampling_strategy = {}
else:
self.sampling_strategy = sampling_strategy
self.temp_folder, self.require = None, None
if temp_folder:
if not os.path.isdir(temp_folder):
raise NotADirectoryError(
"temp_folder does not exist or is not a directory. ({})".format(temp_folder))
self.temp_folder = temp_folder
self.require = "sharedmem"
self._precompute_probabilities()
self.walks = self._generate_walks()
def _precompute_probabilities(self):
"""
Precomputes transition probabilities for each node.
"""
d_graph = self.d_graph
first_travel_done = set()
nodes_generator = self.graph.nodes() if self.quiet \
else tqdm(self.graph.nodes(), desc='Computing transition probabilities')
for source in nodes_generator:
# Init probabilities dict for first travel
if self.PROBABILITIES_KEY not in d_graph[source]:
d_graph[source][self.PROBABILITIES_KEY] = dict()
for current_node in self.graph.neighbors(source):
# Init probabilities dict
if self.PROBABILITIES_KEY not in d_graph[current_node]:
d_graph[current_node][self.PROBABILITIES_KEY] = dict()
unnormalized_weights = list()
first_travel_weights = list()
d_neighbors = list()
# Calculate unnormalized weights
for destination in self.graph.neighbors(current_node):
p = self.sampling_strategy[current_node].get(self.P_KEY,
self.p) if current_node in self.sampling_strategy else self.p
q = self.sampling_strategy[current_node].get(self.Q_KEY,
self.q) if current_node in self.sampling_strategy else self.q
if destination == source: # Backwards probability
ss_weight = self.graph[current_node][destination].get(
self.weight_key, 1) * 1 / p
# If the neighbor is connected to the source
elif destination in self.graph[source]:
ss_weight = self.graph[current_node][destination].get(
self.weight_key, 1)
else:
ss_weight = self.graph[current_node][destination].get(
self.weight_key, 1) * 1 / q
# Assign the unnormalized sampling strategy weight, normalize during random walk
unnormalized_weights.append(ss_weight)
if current_node not in first_travel_done:
first_travel_weights.append(
self.graph[current_node][destination].get(self.weight_key, 1))
d_neighbors.append(destination)
# Normalize
unnormalized_weights = np.array(unnormalized_weights)
d_graph[current_node][self.PROBABILITIES_KEY][
source] = unnormalized_weights / unnormalized_weights.sum()
if current_node not in first_travel_done:
unnormalized_weights = np.array(first_travel_weights)
d_graph[current_node][self.FIRST_TRAVEL_KEY] = unnormalized_weights / \
unnormalized_weights.sum()
first_travel_done.add(current_node)
# Save neighbors
d_graph[current_node][self.NEIGHBORS_KEY] = d_neighbors
def _generate_walks(self):
"""
Generates the random walks which will be used as the skip-gram input.
:return: List of walks. Each walk is a list of nodes.
"""
def flatten(l): return [item for sublist in l for item in sublist]
# Split num_walks for each worker
num_walks_lists = np.array_split(range(self.num_walks), self.workers)
walk_results = Parallel(n_jobs=self.workers, temp_folder=self.temp_folder, require=self.require)(
delayed(parallel_generate_walks)(self.d_graph,
self.walk_length,
len(num_walks),
idx,
self.sampling_strategy,
self.NUM_WALKS_KEY,
self.WALK_LENGTH_KEY,
self.NEIGHBORS_KEY,
self.PROBABILITIES_KEY,
self.FIRST_TRAVEL_KEY,
self.quiet) for
idx, num_walks
in enumerate(num_walks_lists, 1))
walks = flatten(walk_results)
return walks
def parallel_generate_walks(d_graph, global_walk_length, num_walks, cpu_num, sampling_strategy=None,
num_walks_key=None, walk_length_key=None, neighbors_key=None, probabilities_key=None,
first_travel_key=None, quiet=False):
"""
Generates the random walks which will be used as the skip-gram input.
:return: List of walks. Each walk is a list of nodes.
"""
walks = list()
if not quiet:
pbar = tqdm(total=num_walks,
desc='Generating walks (CPU: {})'.format(cpu_num))
for n_walk in range(num_walks):
# Update progress bar
if not quiet:
pbar.update(1)
# Shuffle the nodes
shuffled_nodes = list(d_graph.keys())
random.shuffle(shuffled_nodes)
# Start a random walk from every node
for source in shuffled_nodes:
# Skip nodes with specific num_walks
if source in sampling_strategy and \
num_walks_key in sampling_strategy[source] and \
sampling_strategy[source][num_walks_key] <= n_walk:
continue
# Start walk
walk = [source]
# Calculate walk length
if source in sampling_strategy:
walk_length = sampling_strategy[source].get(
walk_length_key, global_walk_length)
else:
walk_length = global_walk_length
# Perform walk
while len(walk) < walk_length:
walk_options = d_graph[walk[-1]].get(neighbors_key, None)
# Skip dead end nodes
if not walk_options:
break
if len(walk) == 1: # For the first step
probabilities = d_graph[walk[-1]][first_travel_key]
walk_to = np.random.choice(
walk_options, size=1, p=probabilities)[0]
else:
probabilities = d_graph[walk[-1]
][probabilities_key][walk[-2]]
walk_to = np.random.choice(
walk_options, size=1, p=probabilities)[0]
walk.append(walk_to)
walk = list(map(int, walk)) # Convert all to strings
walks.append(walk)
if not quiet:
pbar.close()
return walks