forked from uma-pi1/kge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranse.py
68 lines (58 loc) · 2.21 KB
/
transe.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
import torch
from kge import Config, Dataset
from kge.job import Job
from kge.model.kge_model import RelationalScorer, KgeModel
from torch.nn import functional as F
class TransEScorer(RelationalScorer):
r"""Implementation of the TransE KGE scorer."""
def __init__(self, config: Config, dataset: Dataset, configuration_key=None):
super().__init__(config, dataset, configuration_key)
self._norm = self.get_option("l_norm")
def score_emb(self, s_emb, p_emb, o_emb, combine: str):
n = p_emb.size(0)
if combine == "spo":
out = -F.pairwise_distance(s_emb + p_emb, o_emb, p=self._norm)
elif combine == "sp_":
# we do not use matrix multiplication due to this issue
# https://github.com/pytorch/pytorch/issues/42479
out = -torch.cdist(
s_emb + p_emb,
o_emb,
p=self._norm,
compute_mode="donot_use_mm_for_euclid_dist",
)
elif combine == "_po":
out = -torch.cdist(
o_emb - p_emb,
s_emb,
p=self._norm,
compute_mode="donot_use_mm_for_euclid_dist",
)
else:
return super().score_emb(s_emb, p_emb, o_emb, combine)
return out.view(n, -1)
class TransE(KgeModel):
r"""Implementation of the TransE KGE model."""
def __init__(
self,
config: Config,
dataset: Dataset,
configuration_key=None,
init_for_load_only=False,
):
super().__init__(
config=config,
dataset=dataset,
scorer=TransEScorer,
configuration_key=configuration_key,
init_for_load_only=init_for_load_only,
)
def prepare_job(self, job: Job, **kwargs):
super().prepare_job(job, **kwargs)
from kge.job import TrainingJobNegativeSampling
if (
isinstance(job, TrainingJobNegativeSampling)
and job.config.get("negative_sampling.implementation") == "auto"
):
# TransE with batch currently tends to run out of memory, so we use triple.
job.config.set("negative_sampling.implementation", "triple", log=True)