forked from B-Deprez/NetworkFraud_BiRank_M2V_SAGE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetapath2vec.py
More file actions
29 lines (25 loc) · 970 Bytes
/
metapath2vec.py
File metadata and controls
29 lines (25 loc) · 970 Bytes
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
from stellargraph.data import UniformRandomMetaPathWalk
from gensim.models import Word2Vec
import multiprocessing
def Metapath2vec(G, metapaths, dimensions = 128, num_walks = 1, walk_length = 100, context_window_size = 10):
rw = UniformRandomMetaPathWalk(G)
print('Going for a walk...')
walks = rw.run(
G.nodes(), n=num_walks, length=walk_length, metapaths=metapaths
)
print("Number of random walks: {}".format(len(walks)))
workers = multiprocessing.cpu_count()
model = Word2Vec(
walks,
window=context_window_size,
min_count=0,
sg=1,
workers=workers,
vector_size=dimensions
)
node_ids = model.wv.index_to_key # list of node IDs
node_embeddings = (
model.wv.vectors
) # numpy.ndarray of size number of nodes times embeddings dimensionality
node_targets = [G.node_type(node_id) for node_id in node_ids]
return node_ids, node_embeddings, node_targets