-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_Metapath2Vec.py
More file actions
233 lines (174 loc) · 7.58 KB
/
graph_Metapath2Vec.py
File metadata and controls
233 lines (174 loc) · 7.58 KB
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
from graph_pattern_common import GRAPH_REDUCTION_FACTOR, GRAPH_METAPATH2VEC_MODEL
import numpy as np
import pandas as pd
import time
from math import ceil
import multiprocessing
from stellargraph.data import UniformRandomMetaPathWalk #EdgeSplitter
from degree_corrected_edgesplitter import DegreeCorrectedEdgeSplitter
from gensim.models import Word2Vec
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegressionCV
from sklearn.metrics import roc_auc_score
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from stellargraph import StellarGraph
from typing import Tuple, Callable
import dill
# == THIS SECTION COMES MORE OR LESS VERBATIM FROM A TUTORIAL ==
# StellarGraph metapath2vec tutorial - https://stellargraph.readthedocs.io/en/latest/demos/link-prediction/metapath2vec-link-prediction.html
def stt_Metapath2Vec(graph: StellarGraph) -> Tuple[Pipeline,Callable,Callable]:
# Config options
dimensions = 128
num_walks = 1
walk_length = 25
context_window_size = 4
epochs = 4
workers = multiprocessing.cpu_count()
# This section controls the random walks, ensuring they follow meaningful patterns.
# TODO: Continue to tinker with this section for better results
user_metapaths = [
["user", "product", "user"],
["product", "user", "product"],
["product", "product"],
]
# BEGIN HELPER FUNCTIONS
def metapath2vec_embedding(graph, name):
rw = UniformRandomMetaPathWalk(graph)
walks = rw.run(
graph.nodes(), n=num_walks, length=walk_length, metapaths=user_metapaths
)
print(f"Number of random walks for '{name}': {len(walks)}")
model = Word2Vec(
walks,
vector_size=dimensions,
window=context_window_size,
min_count=0,
sg=1,
workers=workers,
epochs=epochs,
)
def get_embedding(u):
return model.wv[u]
return get_embedding
# 1. link embeddings
def link_examples_to_features(link_examples, transform_node, binary_operator):
return [
binary_operator(transform_node(src), transform_node(dst))
for src, dst in link_examples
]
# 2. training classifier
def train_link_prediction_model(
link_examples, link_labels, get_embedding, binary_operator
):
clf = link_prediction_classifier()
link_features = link_examples_to_features(
link_examples, get_embedding, binary_operator
)
clf.fit(link_features, link_labels)
return clf
def link_prediction_classifier(max_iter=2000):
lr_clf = LogisticRegressionCV(Cs=10, cv=10, scoring="roc_auc", max_iter=max_iter)
return Pipeline(steps=[("sc", StandardScaler()), ("clf", lr_clf)])
# 3. and 4. evaluate classifier
def evaluate_link_prediction_model(
clf, link_examples_test, link_labels_test, get_embedding, binary_operator
):
link_features_test = link_examples_to_features(
link_examples_test, get_embedding, binary_operator
)
score = evaluate_roc_auc(clf, link_features_test, link_labels_test)
return score
def evaluate_roc_auc(clf, link_features, link_labels):
predicted = clf.predict_proba(link_features)
# check which class corresponds to positive links
positive_column = list(clf.classes_).index(1)
return roc_auc_score(link_labels, predicted[:, positive_column])
def operator_l1(u, v):
return np.abs(u - v)
def operator_l2(u, v):
return (u - v) ** 2
def run_link_prediction(binary_operator):
clf = train_link_prediction_model(
examples_train, labels_train, embedding_train, binary_operator
)
score = evaluate_link_prediction_model(
clf,
examples_model_selection,
labels_model_selection,
embedding_train,
binary_operator,
)
return {
"classifier": clf,
"binary_operator": binary_operator,
"embedding": embedding_train,
"score": score,
}
# END HELPER FUNCTIONS
start = time.time()
print("\033[91m{}\033[00m".format(f"Sampling for Metapath2Vec (est. ~{ceil(300/GRAPH_REDUCTION_FACTOR)} seconds)"))
#TODO: Replace split with degree-corrected method
edge_splitter_test = DegreeCorrectedEdgeSplitter(graph)
# Randomly sample a fraction p=0.1 of all positive links, and same number of negative links, from graph, and obtain the
# reduced graph graph_test with the sampled links removed:
graph_test, examples_test, labels_test = edge_splitter_test.train_test_split(
p=0.1, method="global", edge_label="review"
)
end = time.time()
print("\033[93m{}\033[00m".format(f"\tSampling time: {int(end-start)}s"))
start = time.time()
print("\033[91m{}\033[00m".format(f"Building training set for Metapath2Vec (est. ~{ceil(300/GRAPH_REDUCTION_FACTOR)} seconds)"))
edge_splitter_train = DegreeCorrectedEdgeSplitter(graph_test, graph)
graph_train, examples, labels = edge_splitter_train.train_test_split(
p=0.1, method="global", edge_label="review"
)
(
examples_train,
examples_model_selection,
labels_train,
labels_model_selection,
) = train_test_split(examples, labels, train_size=0.75, test_size=0.25)
end = time.time()
print("\033[93m{}\033[00m".format(f"\tBuilding time: {int(end-start)}s"))
start = time.time()
print("\033[91m{}\033[00m".format(f"Embedding training set for Metapath2Vec (est. ~{ceil(6000/GRAPH_REDUCTION_FACTOR)} seconds)"))
embedding_train = metapath2vec_embedding(graph_train, "Train Graph")
end = time.time()
print("\033[93m{}\033[00m".format(f"\tEmbedding time: {int(end-start)}s"))
binary_operators = [operator_l1, operator_l2]
start = time.time()
print("\033[91m{}\033[00m".format(f"Comparing binary operators for Metapath2Vec (est. ~{ceil(600/GRAPH_REDUCTION_FACTOR)} seconds)"))
results = [run_link_prediction(op) for op in binary_operators]
best_result = max(results, key=lambda result: result["score"])
print(f"Best result from '{best_result['binary_operator'].__name__}'")
op_table = pd.DataFrame(
[(result["binary_operator"].__name__, result["score"]) for result in results],
columns=("name", "ROC AUC score"),
).set_index("name")
print(op_table)
end = time.time()
print("\033[93m{}\033[00m".format(f"\tComparison time: {int(end-start)}s"))
start = time.time()
print("\033[91m{}\033[00m".format(f"Embedding testing set for Metapath2Vec (est. ~{ceil(1800/GRAPH_REDUCTION_FACTOR)} seconds)"))
embedding_test = metapath2vec_embedding(graph_test, "Test Graph")
end = time.time()
print("\033[93m{}\033[00m".format(f"\tEmbedding time: {int(end-start)}s"))
start = time.time()
print("\033[91m{}\033[00m".format(f"Evaluating testing set for Metapath2Vec (est. ~X minutes)"))
test_score = evaluate_link_prediction_model(
best_result["classifier"],
examples_test,
labels_test,
embedding_test,
best_result["binary_operator"],
)
print(
f"ROC AUC score on test set using '{best_result['binary_operator'].__name__}': {test_score}"
)
end = time.time()
print("\033[93m{}\033[00m".format(f"\tEvaluation time: {int(end-start)}s"))
with open(GRAPH_METAPATH2VEC_MODEL, 'wb') as file:
dill.dump(best_result,file)
return best_result['classifier'], best_result['binary_operator'], best_result['embedding']
# == END TUTORIAL SECTION ==