An error occurred while loading the file. Please try again.
-
Fize Jacques authoredfef9b4dd
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
import hashlib
import json
import glob
import pandas as pd
import networkx as nx
from tqdm import tqdm
cimport numpy as np
import numpy.distutils.system_info as sysinfo
from joblib import Parallel, delayed
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from sklearn.metrics.pairwise import cosine_similarity
from ..base cimport Base
cimport cython
class WeisfeilerLehmanMachine:
"""
Weisfeiler Lehman feature extractor class.
"""
def __init__(self, graph, features, iterations):
"""
Initialization method which executes feature extraction.
Parameters
----------
graph : nx.Graph
graph
features : dict
Feature hash table.
iterations : int
number of WL iteration
"""
self.iterations = iterations
self.graph = graph
self.features = features
self.nodes = self.graph.nodes()
self.extracted_features = [str(v) for k,v in features.items()]
self.do_recursions()
def do_a_recursion(self):
"""
The method does a single WL recursion.
Returns
-------
dict
The hash table with extracted WL features.
"""
new_features = {}
for node in self.nodes:
nebs = self.graph.neighbors(node)
degs = [self.features[neb] for neb in nebs]
features = "_".join([str(self.features[node])]+list(set(sorted([str(deg) for deg in degs]))))
hash_object = hashlib.md5(features.encode())
hashing = hash_object.hexdigest()
new_features[node] = hashing
self.extracted_features = self.extracted_features + list(new_features.values())
return new_features
def do_recursions(self):
"""
The method does a series of WL recursions.
"""
for iteration in range(self.iterations):
self.features = self.do_a_recursion()
def dataset_reader(graph):
"""
Function to extract features from a networkx graph
Parameters
----------
graph : nx.Graph
graph
Returns
-------
dict
Features hash table.
"""
features = dict(nx.degree(graph))
features = {k:v for k,v, in features.items()}
return graph, features
def feature_extractor(graph, ix, rounds):
"""
Function to extract WL features from a graph
Parameters
----------
graph : nx.Graph
graph
ix : int
index of the graph in the dataset
rounds : int
number of WL iterations
Returns
-------
TaggedDocument
random walks
"""
graph, features = dataset_reader(graph)
machine = WeisfeilerLehmanMachine(graph,features,rounds)
doc = TaggedDocument(words = machine.extracted_features , tags = ["g_{0}".format(ix)])
return doc
def generate_model(graphs, iteration = 2, dimensions = 64, min_count = 5, down_sampling = 0.0001, learning_rate = 0.0001, epochs = 10, workers = 4 ):
"""
Main function to read the graph list, extract features, learn the embedding and save it.
Parameters
----------
graphs : nx.Graph
Input graph
iteration : int, optional
number of iteration (the default is 2)
dimensions : int, optional
output vector dimension (the default is 64)
min_count : int, optional
min count parameter of Doc2vec model (the default is 5)
down_sampling : float, optional
Down sampling rate for frequent features. (the default is 0.0001)
learning_rate : float, optional
Initial learning rate (the default is 0.0001, which [default_description])
epochs : int, optional
Number of epochs (the default is 10)
workers : int, optional
Number of workers (the default is 4)
Returns
-------
[type]
[description]
"""
document_collections = Parallel(n_jobs = workers)(delayed(feature_extractor)(g, ix,iteration) for ix,g in tqdm(enumerate(graphs),desc="Extracting Features..."))
graphs=[nx.relabel_nodes(g,{node:str(node) for node in list(g.nodes)},copy=True) for g in graphs]
model = Doc2Vec(document_collections,
vector_size = dimensions,
window = 0,
min_count = min_count,
dm = 0,
sample = down_sampling,
workers = workers,
epochs = epochs,
alpha = learning_rate)
return model
cdef class Graph2Vec(Base):
"""
Based on :
graph2vec: Learning distributed representations of graphs.
Narayanan, Annamalai and Chandramohan, Mahinthan and Venkatesan, Rajasekar and Chen, Lihui and Liu, Yang
MLG 2017, 13th International Workshop on Mining and Learning with Graphs (MLGWorkshop 2017)
Orignal Code : https://github.com/benedekrozemberczki/graph2vec
Modified by : Jacques Fize
"""
def __init__(self):
Base.__init__(self,0,True)
@cython.boundscheck(False)
cpdef np.ndarray compare(self,list listgs, list selected):
# Selected is ignored
model = generate_model(listgs)
vector_matrix = model.docvecs.vectors_docs
cs = cosine_similarity(vector_matrix)
return cs