forked from DeepGraphLearning/graphvite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplication.py
More file actions
1321 lines (1125 loc) · 50.8 KB
/
Copy pathapplication.py
File metadata and controls
1321 lines (1125 loc) · 50.8 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2019 MilaGraph. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: Zhaocheng Zhu
"""Implementation of applications"""
from __future__ import print_function, absolute_import, unicode_literals, division
import os
import re
import pickle
import logging
import multiprocessing
from collections import defaultdict
from future.builtins import str, map, range
from easydict import EasyDict
import numpy as np
from .. import lib, cfg, auto
from .. import graph, solver
from ..util import assert_in, monitor, SharedNDArray
logger = logging.getLogger(__name__)
class ApplicationMixin(object):
"""
General interface of graph applications.
Parameters:
dim (int): dimension of embeddings
gpus (list of int, optional): GPU ids, default is all GPUs
cpu_per_gpu (int, optional): number of CPU threads per GPU, default is all CPUs
gpu_memory_limit (int, optional): memory limit per GPU in bytes, default is all memory
float_type (dtype, optional): type of parameters
index_type (dtype, optional): type of graph indexes
"""
def __init__(self, dim, gpus=[], cpu_per_gpu=auto, gpu_memory_limit=auto,
float_type=cfg.float_type, index_type=cfg.index_type):
self.dim = dim
self.gpus = gpus
self.cpu_per_gpu = cpu_per_gpu
self.gpu_memory_limit = gpu_memory_limit
self.float_type = float_type
self.index_type = index_type
self.set_format()
def get_graph(self, **kwargs):
raise NotImplementedError
def get_solver(self, **kwargs):
raise NotImplementedError
def set_format(self, delimiters=" \t\r\n", comment="#"):
"""
Set the format for parsing input data.
Parameters:
delimiters (str, optional): string of delimiter characters
comment (str, optional): prefix of comment strings
"""
self.delimiters = delimiters
self.comment = comment
self.pattern = re.compile("[%s]" % self.delimiters)
@monitor.time
def load(self, **kwargs):
"""load(**kwargs)
Load a graph from file or Python object.
Arguments depend on the underlying graph type.
"""
self.graph = self.get_graph(**kwargs)
if "file_name" in kwargs or "vector_file" in "kwargs":
self.graph.load(delimiters=self.delimiters, comment=self.comment, **kwargs)
else:
self.graph.load(**kwargs)
@monitor.time
def build(self, **kwargs):
"""build(**kwargs)
Build the solver from the graph.
Arguments depend on the underlying solver type.
"""
self.solver = self.get_solver(**kwargs)
self.solver.build(self.graph, **kwargs)
@monitor.time
def train(self, **kwargs):
"""train(**kwargs)
Train embeddings with the solver.
Arguments depend on the underlying solver type.
"""
self.solver.train(**kwargs)
@monitor.time
def evaluate(self, task, **kwargs):
"""evaluate(task, **kwargs)
Evaluate the learned embeddings on a downstream task.
Arguments depend on the underlying graph type and the task.
Parameters:
task (str): name of task
Returns:
dict: metrics and their values
"""
func_name = task.replace(" ", "_")
if not hasattr(self, func_name):
raise ValueError("Unknown task `%s`" % task)
logger.info(lib.io.header(task))
result = getattr(self, func_name)(**kwargs)
if isinstance(result, dict):
for metric, value in sorted(result.items()):
logger.warning("%s: %g" % (metric, value))
return result
@monitor.time
def save(self, file_name):
"""
Save embeddings and name mappings in numpy format.
Parameters:
file_name (str): file name
"""
logger.warning("save embeddings and name mappings to `%s`" % file_name)
objects = EasyDict()
for name in dir(self.graph):
object = getattr(self.graph, name)
if isinstance(object, list) and len(object) > 0 and isinstance(object[0], str): # id2name
objects[name] = object
for name in dir(self.solver):
object = getattr(self.solver, name)
if isinstance(object, np.ndarray): # embedding
objects[name] = object
with open(file_name, "wb") as fout:
pickle.dump(objects, fout, protocol=pickle.HIGHEST_PROTOCOL)
def tokenize(self, str):
str = str.strip(self.delimiters)
comment_start = str.find(self.comment)
if comment_start != -1:
str = str[:comment_start]
return self.pattern.split(str)
def name_map(self, dicts, names):
assert len(dicts) == len(names), "The number of dictionaries and names must be equal"
indexes = [[] for _ in range(len(names))]
num_param = len(names)
num_sample = len(names[0])
for i in range(num_sample):
valid = True
for j in range(num_param):
if names[j][i] not in dicts[j]:
valid = False
break
if valid:
for j in range(num_param):
indexes[j].append(dicts[j][names[j][i]])
return indexes
def gpu_map(self, func, settings):
import torch
gpus = self.gpus if self.gpus else range(torch.cuda.device_count())
new_settings = []
for i, setting in enumerate(settings):
new_settings.append(setting + (gpus[i % len(gpus)],))
settings = new_settings
try:
start_method = multiprocessing.get_start_method()
# if there are other running processes, this could cause leakage of semaphores
multiprocessing.set_start_method("spawn", force=True)
pool = multiprocessing.Pool(len(gpus))
results = pool.map(func, settings, chunksize=1)
multiprocessing.set_start_method(start_method, force=True)
except AttributeError:
logger.info("Spawn mode is not supported by multiprocessing. Switch to serial execution.")
results = list(map(func, settings))
return results
class GraphApplication(ApplicationMixin):
"""
Node embedding application.
Given a graph, it embeds each node into a continuous vector representation.
The learned embeddings can be used for many downstream tasks.
e.g. **node classification**, **link prediction**, **node analogy**.
The similarity between node embeddings can be measured by cosine distance.
Supported Models:
- DeepWalk (`DeepWalk: Online Learning of Social Representations`_)
- LINE (`LINE: Large-scale Information Network Embedding`_)
- node2vec (`node2vec: Scalable Feature Learning for Networks`_)
.. _DeepWalk\: Online Learning of Social Representations:
https://arxiv.org/pdf/1403.6652.pdf
.. _LINE\: Large-scale Information Network Embedding:
https://arxiv.org/pdf/1503.03578.pdf
.. _node2vec\: Scalable Feature Learning for Networks:
https://www.kdd.org/kdd2016/papers/files/rfp0218-groverA.pdf
Parameters:
dim (int): dimension of embeddings
gpus (list of int, optional): GPU ids, default is all GPUs
cpu_per_gpu (int, optional): number of CPU threads per GPU, default is all CPUs
float_type (dtype, optional): type of parameters
index_type (dtype, optional): type of graph indexes
See also:
:class:`Graph <graphvite.graph.Graph>`,
:class:`GraphSolver <graphvite.solver.GraphSolver>`
"""
def get_graph(self, **kwargs):
return graph.Graph(self.index_type)
def get_solver(self, **kwargs):
if self.cpu_per_gpu == auto:
num_sampler_per_worker = auto
else:
num_sampler_per_worker = self.cpu_per_gpu - 1
return solver.GraphSolver(self.dim, self.float_type, self.index_type, self.gpus, num_sampler_per_worker,
self.gpu_memory_limit)
def node_classification(self, X=None, Y=None, file_name=None, portions=(0.02,), normalization=False, times=1,
patience=100):
"""
Evaluate node embeddings on node classification task.
Parameters:
X (list of str, optional): names of nodes
Y (list, optional): labels of nodes
file_name (str, optional): file of nodes & labels
portions (tuple of float, optional): how much data for training
normalization (bool, optional): normalize the embeddings or not
times (int, optional): number of trials
patience (int, optional): patience on loss convergence
Returns:
dict: macro-F1 & micro-F1 averaged over all trials
"""
import scipy.sparse as sp
self.solver.clear()
if file_name:
if not (X is None and Y is None):
raise ValueError("Evaluation data and file should not be provided at the same time")
X = []
Y = []
with open(file_name, "r") as fin:
for line in fin:
tokens = self.tokenize(line)
if len(tokens) == 0:
continue
x, y = tokens
X.append(x)
Y.append(y)
if X is None or Y is None:
raise ValueError("Either evaluataion data (X, Y) or a file name should be provided")
name2id = self.graph.name2id
class2id = {c:i for i, c in enumerate(np.unique(Y))}
new_X, new_Y = self.name_map((name2id, class2id), (X, Y))
logger.info("effective labels: %d / %d" % (len(new_X), len(X)))
X = np.asarray(new_X)
Y = np.asarray(new_Y)
labels = sp.coo_matrix((np.ones_like(X), (X, Y)), dtype=np.int32).todense()
indexes, _ = np.where(np.sum(labels, axis=1) > 0)
# discard non-labeled nodes
labels = labels[indexes]
vertex_embeddings = SharedNDArray(self.solver.vertex_embeddings[indexes])
settings = []
for portion in portions:
settings.append((vertex_embeddings, labels, portion, normalization, times, patience))
results = self.gpu_map(linear_classification, settings)
metrics = {}
for result in results:
metrics.update(result)
return metrics
def link_prediction(self, H=None, T=None, Y=None, file_name=None, filter_H=None, filter_T=None, filter_file=None):
"""
Evaluate node embeddings on link prediction task.
Parameters:
H (list of str, optional): names of head nodes
T (list of str, optional): names of tail nodes
Y (list of int, optional): labels of edges
file_name (str, optional): file of edges and labels (e.g. validation set)
filter_H (list of str, optional): names of head nodes to filter out
filter_T (list of str, optional): names of tail nodes to filter out
filter_file (str, optional): file of edges to filter out (e.g. training set)
Returns:
dict: AUC of link prediction
"""
import torch
from .network import LinkPredictor
self.solver.clear()
if file_name:
if not (H is None and T is None and Y is None):
raise ValueError("Evaluation data and file should not be provided at the same time")
H = []
T = []
Y = []
with open(file_name, "r") as fin:
for line in fin:
tokens = self.tokenize(line)
if len(tokens) == 0:
continue
h, t, y = tokens
H.append(h)
T.append(t)
Y.append(y)
if H is None or T is None or Y is None:
raise ValueError("Either evaluation data or file should be provided")
if filter_file:
if not (filter_H is None and filter_T is None):
raise ValueError("Filter data and file should not be provided at the same time")
filter_H = []
filter_T = []
with open(filter_file, "r") as fin:
for line in fin:
tokens = self.tokenize(line)
if len(tokens) == 0:
continue
h, t = tokens
filter_H.append(h)
filter_T.append(t)
elif filter_H is None:
filter_H = []
filter_T = []
name2id = self.graph.name2id
Y = [int(y) for y in Y]
new_H, new_T, new_Y = self.name_map((name2id, name2id, {0:0, 1:1}), (H, T, Y))
logger.info("effective edges: %d / %d" % (len(new_H), len(H)))
H = new_H
T = new_T
Y = new_Y
new_H, new_T = self.name_map((name2id, name2id), (filter_H, filter_T))
logger.info("effective filter edges: %d / %d" % (len(new_H), len(filter_H)))
filters = set(zip(new_H, new_T))
new_H = []
new_T = []
new_Y = []
for h, t, y in zip(H, T, Y):
if (h, t) not in filters:
new_H.append(h)
new_T.append(t)
new_Y.append(y)
logger.info("remaining edges: %d / %d" % (len(new_H), len(H)))
H = np.asarray(new_H)
T = np.asarray(new_T)
Y = np.asarray(new_Y)
vertex_embeddings = self.solver.vertex_embeddings
context_embeddings = self.solver.context_embeddings
model = LinkPredictor(self.solver.model, vertex_embeddings, context_embeddings)
model = model.cuda()
H = torch.as_tensor(H)
T = torch.as_tensor(T)
Y = torch.as_tensor(Y)
H = H.cuda()
T = T.cuda()
Y = Y.cuda()
score = model(H, T)
order = torch.argsort(score, descending=True)
Y = Y[order]
hit = torch.cumsum(Y, dim=0)
all = torch.sum(Y == 0) * torch.sum(Y == 1)
auc = torch.sum(hit[Y == 0]).item() / all.item()
return {
"AUC": auc
}
def linear_classification(args):
import torch
from torch import optim
from torch.nn import functional as F
from .network import NodeClassifier
def generate_one_vs_rest(indexes, labels):
new_indexes = []
new_labels = []
num_class = labels.shape[1]
for index, sample_labels in zip(indexes, labels):
for cls in np.where(sample_labels)[0]:
new_indexes.append(index)
new_label = np.zeros(num_class, dtype=np.int)
new_label[cls] = 1
new_labels.append(new_label)
return torch.as_tensor(new_indexes), torch.as_tensor(new_labels)
embeddings, labels, portion, normalization, times, patience, gpu = args
embeddings = np.asarray(embeddings)
num_sample, num_class = labels.shape
num_train = int(num_sample * portion)
macro_f1s = []
micro_f1s = []
for t in range(times):
samples = np.random.permutation(num_sample)
train_samples = samples[:num_train]
train_labels = np.asarray(labels[train_samples])
train_samples, train_labels = generate_one_vs_rest(train_samples, train_labels)
test_samples = torch.as_tensor(samples[num_train:])
test_labels = torch.as_tensor(labels[test_samples])
model = NodeClassifier(embeddings, num_class, normalization=normalization)
train_samples = train_samples.cuda(gpu)
train_labels = train_labels.cuda(gpu)
test_samples = test_samples.cuda(gpu)
test_labels = test_labels.cuda(gpu)
model = model.cuda(gpu)
# train
optimizer = optim.SGD(model.parameters(), lr=1, weight_decay=2e-5, momentum=0.9)
best_loss = float("inf")
best_epoch = -1
for epoch in range(100000):
optimizer.zero_grad()
logits = model(train_samples)
loss = F.binary_cross_entropy_with_logits(logits, train_labels.float())
loss.backward()
optimizer.step()
loss = loss.item()
if loss < best_loss:
best_epoch = epoch
best_loss = loss
if epoch == best_epoch + patience:
break
# test
logits = model(test_samples)
num_labels = test_labels.sum(dim=1, keepdim=True)
sorted, _ = logits.sort(dim=1, descending=True)
thresholds = sorted.gather(dim=1, index=num_labels-1)
predictions = (logits >= thresholds).int()
# compute metric
num_TP_per_class = (predictions & test_labels).sum(dim=0).float()
num_T_per_class = test_labels.sum(dim=0).float()
num_P_per_class = predictions.sum(dim=0).float()
macro_f1s.append((2 * num_TP_per_class / (num_T_per_class + num_P_per_class)).mean().item())
num_TP = (predictions & test_labels).sum().float()
num_T = test_labels.sum().float()
num_P = predictions.sum().float()
micro_f1s.append((2 * num_TP / (num_T + num_P)).item())
return {
"macro-F1@%g%%" % (portion * 100): np.mean(macro_f1s),
"micro-F1@%g%%" % (portion * 100): np.mean(micro_f1s)
}
class WordGraphApplication(ApplicationMixin):
"""
Word node embedding application.
Given a corpus, it embeds each word into a continuous vector representation.
The learned embeddings can be used for natural language processing tasks.
This can be viewed as a variant of the word2vec algorithm, with random walk augmentation support.
The similarity between node embeddings can be measured by cosine distance.
Supported Models:
- LINE (`LINE: Large-scale Information Network Embedding`_)
Parameters:
dim (int): dimension of embeddings
gpus (list of int, optional): GPU ids, default is all GPUs
cpu_per_gpu (int, optional): number of CPU threads per GPU, default is all CPUs
float_type (dtype, optional): type of parameters
index_type (dtype, optional): type of graph indexes
See also:
:class:`WordGraph <graphvite.graph.WordGraph>`,
:class:`GraphSolver <graphvite.solver.GraphSolver>`
"""
def get_graph(self, **kwargs):
return graph.WordGraph(self.index_type)
def get_solver(self, **kwargs):
if self.cpu_per_gpu == auto:
num_sampler_per_worker = auto
else:
num_sampler_per_worker = self.cpu_per_gpu - 1
return solver.GraphSolver(self.dim, self.float_type, self.index_type, self.gpus, num_sampler_per_worker,
self.gpu_memory_limit)
class KnowledgeGraphApplication(ApplicationMixin):
"""
Knowledge graph embedding application.
Given a knowledge graph, it embeds each entity and relation into a continuous vector representation respectively.
The learned embeddings can be used for analysis of knowledge graphs.
e.g. **entity prediction**, **link prediction**.
The likelihood of edges can be predicted by computing the score function over embeddings of triplets.
Supported Models:
- TransE (`Translating Embeddings for Modeling Multi-relational Data`_)
- DistMult (`Embedding Entities and Relations for Learning and Inference in Knowledge Bases`_)
- ComplEx (`Complex Embeddings for Simple Link Prediction`_)
- SimplE (`SimplE Embedding for Link Prediction in Knowledge Graphs`_)
- RotatE (`RotatE: Knowledge Graph Embedding by Relational Rotation in Complex Space`_)
.. _Translating Embeddings for Modeling Multi-relational Data:
http://papers.nips.cc/paper/5071-translating-embeddings-for-modeling-multi-relational-data.pdf
.. _Embedding Entities and Relations for Learning and Inference in Knowledge Bases:
https://arxiv.org/pdf/1412.6575.pdf
.. _Complex Embeddings for Simple Link Prediction:
http://proceedings.mlr.press/v48/trouillon16.pdf
.. _SimplE Embedding for Link Prediction in Knowledge Graphs:
https://papers.nips.cc/paper/7682-simple-embedding-for-link-prediction-in-knowledge-graphs.pdf
.. _RotatE\: Knowledge Graph Embedding by Relational Rotation in Complex Space:
https://arxiv.org/pdf/1902.10197.pdf
Parameters:
dim (int): dimension of embeddings
gpus (list of int, optional): GPU ids, default is all GPUs
cpu_per_gpu (int, optional): number of CPU threads per GPU, default is all CPUs
float_type (dtype, optional): type of parameters
index_type (dtype, optional): type of graph indexes
Note:
The implementation of TransE, DistMult and ComplEx, SimplE are slightly different from their original papers.
The loss function and the regularization term generally follow `this repo`_.
Self-adversarial negative sampling is also adopted in these models like RotatE.
.. _this repo: https://github.com/DeepGraphLearning/KnowledgeGraphEmbedding
See also:
:class:`KnowledgeGraph <graphvite.graph.KnowledgeGraph>`,
:class:`KnowledgeGraphSolver <graphvite.solver.KnowledgeGraphSolver>`
"""
SAMPLE_PER_DIMENSION = 7
MEMORY_SCALE_FACTOR = 1.5
def get_graph(self, **kwargs):
return graph.KnowledgeGraph(self.index_type)
def get_solver(self, **kwargs):
if self.cpu_per_gpu == auto:
num_sampler_per_worker = auto
else:
num_sampler_per_worker = self.cpu_per_gpu - 1
return solver.KnowledgeGraphSolver(self.dim, self.float_type, self.index_type, self.gpus, num_sampler_per_worker,
self.gpu_memory_limit)
def entity_prediction(self, H=None, R=None, T=None, file_name=None, save_file=None, target="tail", k=10,
backend=cfg.backend):
"""
Predict the distribution of missing entity or relation for triplets.
Parameters:
H (list of str, optional): names of head entities
R (list of str, optional): names of relations
T (list of str, optional): names of tail entities
file_name (str, optional): file of triplets (e.g. validation set)
save_file (str, optional): ``txt`` or ``pkl`` file to save predictions
k (int, optional): top-k recalls will be returned
target (str, optional): 'head' or 'tail'
backend (str, optional): 'graphvite' or 'torch'
Return:
list of list of tuple: top-k recalls for each triplet, if save file is not provided
"""
def torch_predict():
import torch
entity_embeddings = SharedNDArray(self.solver.entity_embeddings)
relation_embeddings = SharedNDArray(self.solver.relation_embeddings)
num_gpu = len(self.gpus) if self.gpus else torch.cuda.device_count()
work_load = (num_sample + num_gpu - 1) // num_gpu
settings = []
for i in range(num_gpu):
work_H = H[work_load * i: work_load * (i+1)]
work_R = R[work_load * i: work_load * (i+1)]
work_T = T[work_load * i: work_load * (i+1)]
settings.append((entity_embeddings, relation_embeddings, work_H, work_R, work_T,
None, None, target, k, self.solver.model, self.solver.margin))
results = self.gpu_map(triplet_prediction, settings)
return sum(results, [])
def graphvite_predict():
num_entity = len(entity2id)
batch_size = self.get_batch_size(num_entity)
recalls = []
for i in range(0, num_sample, batch_size):
batch_h = H[i: i + batch_size]
batch_r = R[i: i + batch_size]
batch_t = T[i: i + batch_size]
batch = self.generate_one_vs_rest(batch_h, batch_r, batch_t, num_entity, target)
scores = self.solver.predict(batch)
scores = scores.reshape(-1, num_entity)
indexes = np.argpartition(scores, num_entity - k, axis=-1)
for index, score in zip(indexes, scores):
index = index[-k:]
score = score[index]
order = np.argsort(score)[::-1]
recall = list(zip(index[order], score[order]))
recalls.append(recall)
return recalls
assert_in(["head", "tail"], target=target)
assert_in(["graphvite", "torch"], backend=backend)
if backend == "torch":
self.solver.clear()
if file_name:
if not (H is None and R is None and T is None):
raise ValueError("Evaluation data and file should not be provided at the same time")
H = []
R = []
T = []
with open(file_name, "r") as fin:
for line in fin:
tokens = self.tokenize(line)
if len(tokens) == 0:
continue
if len(tokens) == 3:
h, r, t = tokens
elif target == "head":
r, t = tokens
h = None
else:
h, r = tokens
t = None
H.append(h)
R.append(r)
T.append(t)
if (H is None and T is None) or R is None:
raise ValueError("Either evaluation data or file should be provided")
if H is None:
target = "head"
if T is None:
target = "tail"
entity2id = self.graph.entity2id
relation2id = self.graph.relation2id
num_sample = len(R)
new_H = np.zeros(num_sample, dtype=np.uint32)
new_T = np.zeros(num_sample, dtype=np.uint32)
if target == "head":
new_R, new_T = self.name_map((relation2id, entity2id), (R, T))
if target == "tail":
new_H, new_R = self.name_map((entity2id, relation2id), (H, R))
assert len(new_R) == len(R), "Can't recognize some entities or relations"
H = np.asarray(new_H, dtype=np.uint32)
R = np.asarray(new_R, dtype=np.uint32)
T = np.asarray(new_T, dtype=np.uint32)
if backend == "graphvite":
recalls = graphvite_predict()
else:
recalls = torch_predict()
id2entity = self.graph.id2entity
new_recalls = []
for recall in recalls:
new_recall = [(id2entity[e], s) for e, s in recall]
new_recalls.append(new_recall)
recalls = new_recalls
if save_file:
extension = os.path.splitext(save_file)[1]
if extension == ".txt":
with open(save_file, "w") as fout:
for recall in recalls:
tokens = ["%s: %g" % x for x in recall]
fout.write("%s\n" % "\t".join(tokens))
elif extension == ".pkl":
with open(save_file, "wb") as fout:
pickle.dump(recalls, fout, protocol=pickle.HIGHEST_PROTOCOL)
else:
raise ValueError("Unknown file extension `%s`" % extension)
else:
return recalls
def link_prediction(self, H=None, R=None, T=None, filter_H=None, filter_R=None, filter_T=None, file_name=None,
filter_files=None, target="both", fast_mode=None, backend=cfg.backend):
"""
Evaluate knowledge graph embeddings on link prediction task.
Parameters:
H (list of str, optional): names of head entities
R (list of str, optional): names of relations
T (list of str, optional): names of tail entities
file_name (str, optional): file of triplets (e.g. validation set)
filter_H (list of str, optional): names of head entities to filter out
filter_R (list of str, optional): names of relations to filter out
filter_T (list of str, optional): names of tail entities to filter out
filter_files (str, optional): files of triplets to filter out (e.g. training / validation / test set)
target (str, optional): 'head', 'tail' or 'both'
fast_mode (int, optional): if specified, only that number of samples will be evaluated
backend (str, optional): 'graphvite' or 'torch'
Returns:
dict: MR, MRR, HITS\@1, HITS\@3 & HITS\@10 of link prediction
"""
def torch_predict():
import torch
entity_embeddings = SharedNDArray(self.solver.entity_embeddings)
relation_embeddings = SharedNDArray(self.solver.relation_embeddings)
num_gpu = len(self.gpus) if self.gpus else torch.cuda.device_count()
work_load = (fast_mode + num_gpu - 1) // num_gpu
settings = []
for i in range(num_gpu):
work_H = H[work_load * i: work_load * (i+1)]
work_R = R[work_load * i: work_load * (i+1)]
work_T = T[work_load * i: work_load * (i+1)]
settings.append((entity_embeddings, relation_embeddings, work_H, work_R, work_T,
exclude_H, exclude_T, target, None, self.solver.model, self.solver.margin))
results = self.gpu_map(triplet_prediction, settings)
return np.concatenate(results)
def graphvite_predict():
num_entity = len(entity2id)
if target == "both":
batch_size = self.get_batch_size(num_entity * 2)
else:
batch_size = self.get_batch_size(num_entity)
rankings = []
for i in range(0, fast_mode, batch_size):
batch_h = H[i: i + batch_size]
batch_r = R[i: i + batch_size]
batch_t = T[i: i + batch_size]
batch = self.generate_one_vs_rest(batch_h, batch_r, batch_t, num_entity, target)
masks = self.generate_mask(batch_h, batch_r, batch_t, exclude_H, exclude_T, num_entity, target)
if target == "head":
positives = batch_h
if target == "tail":
positives = batch_t
if target == "both":
positives = np.asarray([batch_h, batch_t]).transpose()
positives = positives.ravel()
scores = self.solver.predict(batch)
scores = scores.reshape(-1, num_entity)
truths = scores[range(len(positives)), positives]
ranking = np.sum((scores >= truths[:, np.newaxis]) * masks, axis=1)
rankings.append(ranking)
return np.concatenate(rankings)
assert_in(["head", "tail", "both"], target=target)
assert_in(["graphvite", "torch"], backend=backend)
if backend == "torch":
self.solver.clear()
if file_name:
if not (H is None and R is None and T is None):
raise ValueError("Evaluation data and file should not be provided at the same time")
H = []
R = []
T = []
with open(file_name, "r") as fin:
for line in fin:
tokens = self.tokenize(line)
if len(tokens) == 0:
continue
h, r, t = tokens
H.append(h)
R.append(r)
T.append(t)
if H is None or R is None or T is None:
raise ValueError("Either evaluation data or file should be provided")
if filter_files:
if not (filter_H is None and filter_R is None and filter_T is None):
raise ValueError("Filter data and file should not be provided at the same time")
filter_H = []
filter_R = []
filter_T = []
for filter_file in filter_files:
with open(filter_file, "r") as fin:
for line in fin:
tokens = self.tokenize(line)
if len(tokens) == 0:
continue
h, r, t = tokens
filter_H.append(h)
filter_R.append(r)
filter_T.append(t)
elif filter_H is None:
filter_H = []
filter_R = []
filter_T = []
entity2id = self.graph.entity2id
relation2id = self.graph.relation2id
new_H, new_R, new_T = self.name_map((entity2id, relation2id, entity2id), (H, R, T))
logger.info("effective triplets: %d / %d" % (len(new_H), len(H)))
H = np.asarray(new_H, dtype=np.uint32)
R = np.asarray(new_R, dtype=np.uint32)
T = np.asarray(new_T, dtype=np.uint32)
new_H, new_R, new_T = self.name_map((entity2id, relation2id, entity2id), (filter_H, filter_R, filter_T))
logger.info("effective filter triplets: %d / %d" % (len(new_H), len(filter_H)))
filter_H = np.asarray(new_H, dtype=np.uint32)
filter_R = np.asarray(new_R, dtype=np.uint32)
filter_T = np.asarray(new_T, dtype=np.uint32)
exclude_H = defaultdict(set)
exclude_T = defaultdict(set)
for h, r, t in zip(filter_H, filter_R, filter_T):
exclude_H[(t, r)].add(h)
exclude_T[(h, r)].add(t)
num_sample = len(H)
fast_mode = fast_mode or num_sample
indexes = np.random.permutation(num_sample)[:fast_mode]
H = H[indexes]
R = R[indexes]
T = T[indexes]
if backend == "graphvite":
rankings = graphvite_predict()
elif backend == "torch":
rankings = torch_predict()
return {
"MR": np.mean(rankings),
"MRR": np.mean(1 / rankings),
"HITS@1": np.mean(rankings <= 1),
"HITS@3": np.mean(rankings <= 3),
"HITS@10": np.mean(rankings <= 10)
}
def get_batch_size(self, sample_size):
import psutil
memory = psutil.virtual_memory()
batch_size = int(self.SAMPLE_PER_DIMENSION * self.dim * self.graph.num_vertex
* self.solver.num_partition / self.solver.num_worker / sample_size)
# 2 triplet (Python, C++ sample pool) + 1 sample index
mem_per_sample = sample_size * (2 * 3 * np.uint32().itemsize + 1 * np.uint64().itemsize)
max_batch_size = int(memory.available / mem_per_sample / self.MEMORY_SCALE_FACTOR)
if max_batch_size < batch_size:
logger.info("Memory is not enough for optimal prediction batch size. "
"Use the maximal possible size instead.")
batch_size = max_batch_size
return batch_size
def generate_one_vs_rest(self, H, R, T, num_entity, target="both"):
one = np.ones(num_entity, dtype=np.bool)
all = np.arange(num_entity, dtype=np.uint32)
batches = []
for h, r, t in zip(H, R, T):
if target == "head" or target == "both":
batch = np.asarray([all, t * one, r * one]).transpose()
batches.append(batch)
if target == "tail" or target == "both":
batch = np.asarray([h * one, all, r * one]).transpose()
batches.append(batch)
batches = np.concatenate(batches)
return batches
def generate_mask(self, H, R, T, exclude_H, exclude_T, num_entity, target="both"):
one = np.ones(num_entity, dtype=np.bool)
masks = []
for h, r, t in zip(H, R, T):
if target == "head" or target == "both":
mask = one.copy()
mask[list(exclude_H[(t, r)])] = 0
mask[h] = 1
masks.append(mask)
if target == "tail" or target == "both":
mask = one.copy()
mask[list(exclude_T[(h, r)])] = 0
mask[t] = 1
masks.append(mask)
masks = np.asarray(masks)
return masks
def triplet_prediction(args):
import torch
from .network import LinkPredictor
torch.set_grad_enabled(False)
entity_embeddings, relation_embeddings, H, R, T, \
exclude_H, exclude_T, target, k, score_function, margin, device = args
entity_embeddings = np.asarray(entity_embeddings)
relation_embeddings = np.asarray(relation_embeddings)
num_entity = len(entity_embeddings)
score_function = LinkPredictor(score_function, entity_embeddings, relation_embeddings, entity_embeddings,
margin=margin)
if device != "cpu":
try:
score_function = score_function.to(device)
except RuntimeError:
logger.info("Model is too large for GPU evaluation with PyTorch. Switch to CPU evaluation.")
device = "cpu"
if device == "cpu":
del score_function
torch.cuda.empty_cache()
score_function = LinkPredictor(score_function, entity_embeddings, relation_embeddings, entity_embeddings,
margin=margin)
one = torch.ones(num_entity, dtype=torch.long, device=device)
all = torch.arange(num_entity, dtype=torch.long, device=device)
results = [] # rankings or top-k recalls
for h, r, t in zip(H, R, T):
if target == "head" or target == "both":
batch_h = all
batch_r = r * one
batch_t = t * one
score = score_function(batch_h, batch_r, batch_t)
if k: # top-k recalls
score, index = torch.topk(score, k)
score = score.cpu().numpy()
index = index.cpu().numpy()
recall = list(zip(index, score))
results.append(recall)
else: # ranking
mask = torch.ones(num_entity, dtype=torch.uint8, device=device)
index = torch.tensor(list(exclude_H[(t, r)]), dtype=torch.long, device=device)
mask[index] = 0
mask[h] = 1
ranking = torch.sum((score >= score[h]) * mask).item()
results.append(ranking)
if target == "tail" or target == "both":
batch_h = h * one
batch_r = r * one
batch_t = all
score = score_function(batch_h, batch_r, batch_t)
if k: # top-k recalls
score, index = torch.topk(score, k)
score = score.cpu().numpy()
index = index.cpu().numpy()
recall = list(zip(index, score))
results.append(recall)
else: # ranking
mask = torch.ones(num_entity, dtype=torch.uint8, device=device)
index = torch.tensor(list(exclude_T[(h, r)]), dtype=torch.long, device=device)
mask[index] = 0
mask[t] = 1
ranking = torch.sum((score >= score[t]) * mask).item()
results.append(ranking)
if not k: # ranking
results = np.asarray(results)
return results
class VisualizationApplication(ApplicationMixin):