[36b44b]: / torchdrug / layers / functional / embedding.py

Download this file

269 lines (221 with data), 9.9 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
import os
import torch
from torch import autograd
from torchdrug import utils
backend = "fast"
path = os.path.join(os.path.dirname(__file__), "extension")
embedding = utils.load_extension("embedding",
[os.path.join(path, "embedding.cpp"), os.path.join(path, "embedding.cu")])
class TransEFunction(autograd.Function):
@staticmethod
def forward(ctx, entity, relation, h_index, t_index, r_index):
if entity.device.type == "cuda":
forward = embedding.transe_forward_cuda
else:
forward = embedding.transe_forward_cpu
score = forward(entity, relation, h_index, t_index, r_index)
ctx.save_for_backward(entity, relation, h_index, t_index, r_index)
return score
@staticmethod
def backward(ctx, score_grad):
if score_grad.device.type == "cuda":
backward = embedding.transe_backward_cuda
else:
backward = embedding.transe_backward_cpu
entity_grad, relation_grad = backward(*ctx.saved_tensors, score_grad)
return entity_grad, relation_grad, None, None, None
class DistMultFunction(autograd.Function):
@staticmethod
def forward(ctx, entity, relation, h_index, t_index, r_index):
if entity.device.type == "cuda":
forward = embedding.distmult_forward_cuda
else:
forward = embedding.distmult_forward_cpu
score = forward(entity, relation, h_index, t_index, r_index)
ctx.save_for_backward(entity, relation, h_index, t_index, r_index)
return score
@staticmethod
def backward(ctx, score_grad):
if score_grad.device.type == "cuda":
backward = embedding.distmult_backward_cuda
else:
backward = embedding.distmult_backward_cpu
entity_grad, relation_grad = backward(*ctx.saved_tensors, score_grad)
return entity_grad, relation_grad, None, None, None
class ComplExFunction(autograd.Function):
@staticmethod
def forward(ctx, entity, relation, h_index, t_index, r_index):
if entity.device.type == "cuda":
forward = embedding.complex_forward_cuda
else:
forward = embedding.complex_forward_cpu
score = forward(entity, relation, h_index, t_index, r_index)
ctx.save_for_backward(entity, relation, h_index, t_index, r_index)
return score
@staticmethod
def backward(ctx, score_grad):
if score_grad.device.type == "cuda":
backward = embedding.complex_backward_cuda
else:
backward = embedding.complex_backward_cpu
entity_grad, relation_grad = backward(*ctx.saved_tensors, score_grad)
return entity_grad, relation_grad, None, None, None
class SimplEFunction(autograd.Function):
@staticmethod
def forward(ctx, entity, relation, h_index, t_index, r_index):
if entity.device.type == "cuda":
forward = embedding.simple_forward_cuda
else:
forward = embedding.simple_forward_cpu
score = forward(entity, relation, h_index, t_index, r_index)
ctx.save_for_backward(entity, relation, h_index, t_index, r_index)
return score
@staticmethod
def backward(ctx, score_grad):
if score_grad.device.type == "cuda":
backward = embedding.simple_backward_cuda
else:
backward = embedding.simple_backward_cpu
entity_grad, relation_grad = backward(*ctx.saved_tensors, score_grad)
return entity_grad, relation_grad, None, None, None
class RotatEFunction(autograd.Function):
@staticmethod
def forward(ctx, entity, relation, h_index, t_index, r_index):
if entity.device.type == "cuda":
forward = embedding.rotate_forward_cuda
else:
forward = embedding.rotate_forward_cpu
score = forward(entity, relation, h_index, t_index, r_index)
ctx.save_for_backward(entity, relation, h_index, t_index, r_index)
return score
@staticmethod
def backward(ctx, score_grad):
if score_grad.device.type == "cuda":
backward = embedding.rotate_backward_cuda
else:
backward = embedding.rotate_backward_cpu
entity_grad, relation_grad = backward(*ctx.saved_tensors, score_grad)
return entity_grad, relation_grad, None, None, None
def transe_score(entity, relation, h_index, t_index, r_index):
"""
TransE score function from `Translating Embeddings for Modeling Multi-relational Data`_.
.. _Translating Embeddings for Modeling Multi-relational Data:
https://proceedings.neurips.cc/paper/2013/file/1cecc7a77928ca8133fa24680a88d2f9-Paper.pdf
Parameters:
entity (Tensor): entity embeddings of shape :math:`(|V|, d)`
relation (Tensor): relation embeddings of shape :math:`(|R|, d)`
h_index (LongTensor): index of head entities
t_index (LongTensor): index of tail entities
r_index (LongTensor): index of relations
"""
if backend == "native":
h = entity[h_index]
r = relation[r_index]
t = entity[t_index]
score = (h + r - t).norm(p=1, dim=-1)
elif backend == "fast":
score = TransEFunction.apply(entity, relation, h_index, t_index, r_index)
else:
raise ValueError("Unknown embedding backend `%s`" % backend)
return score
def distmult_score(entity, relation, h_index, t_index, r_index):
"""
DistMult score function from `Embedding Entities and Relations for Learning and Inference in Knowledge Bases`_.
.. _Embedding Entities and Relations for Learning and Inference in Knowledge Bases:
https://arxiv.org/pdf/1412.6575.pdf
Parameters:
entity (Tensor): entity embeddings of shape :math:`(|V|, d)`
relation (Tensor): relation embeddings of shape :math:`(|R|, d)`
h_index (LongTensor): index of head entities
t_index (LongTensor): index of tail entities
r_index (LongTensor): index of relations
"""
if backend == "native":
h = entity[h_index]
r = relation[r_index]
t = entity[t_index]
score = (h * r * t).sum(dim=-1)
elif backend == "fast":
score = DistMultFunction.apply(entity, relation, h_index, t_index, r_index)
else:
raise ValueError("Unknown embedding backend `%s`" % backend)
return score
def complex_score(entity, relation, h_index, t_index, r_index):
"""
ComplEx score function from `Complex Embeddings for Simple Link Prediction`_.
.. _Complex Embeddings for Simple Link Prediction:
http://proceedings.mlr.press/v48/trouillon16.pdf
Parameters:
entity (Tensor): entity embeddings of shape :math:`(|V|, 2d)`
relation (Tensor): relation embeddings of shape :math:`(|R|, 2d)`
h_index (LongTensor): index of head entities
t_index (LongTensor): index of tail entities
r_index (LongTensor): index of relations
"""
if backend == "native":
h = entity[h_index]
r = relation[r_index]
t = entity[t_index]
h_re, h_im = h.chunk(2, dim=-1)
r_re, r_im = r.chunk(2, dim=-1)
t_re, t_im = t.chunk(2, dim=-1)
x_re = h_re * r_re - h_im * r_im
x_im = h_re * r_im + h_im * r_re
x = x_re * t_re + x_im * t_im
score = x.sum(dim=-1)
elif backend == "fast":
score = ComplExFunction.apply(entity, relation, h_index, t_index, r_index)
else:
raise ValueError("Unknown embedding backend `%s`" % backend)
return score
def simple_score(entity, relation, h_index, t_index, r_index):
"""
SimplE score function from `SimplE Embedding for Link Prediction in Knowledge Graphs`_.
.. _SimplE Embedding for Link Prediction in Knowledge Graphs:
https://papers.nips.cc/paper/2018/file/b2ab001909a8a6f04b51920306046ce5-Paper.pdf
Parameters:
entity (Tensor): entity embeddings of shape :math:`(|V|, 2d)`
relation (Tensor): relation embeddings of shape :math:`(|R|, d)`
h_index (LongTensor): index of head entities
t_index (LongTensor): index of tail entities
r_index (LongTensor): index of relations
"""
if backend == "native":
h = entity[h_index]
r = relation[r_index]
t = entity[t_index]
t_flipped = torch.cat(t.chunk(2, dim=-1)[::-1], dim=-1)
score = (h * r * t_flipped).sum(dim=-1)
elif backend == "fast":
score = SimplEFunction.apply(entity, relation, h_index, t_index, r_index)
else:
raise ValueError("Unknown embedding backend `%s`" % backend)
return score
def rotate_score(entity, relation, h_index, t_index, r_index):
"""
RotatE score function from `RotatE: Knowledge Graph Embedding by Relational Rotation in Complex Space`_.
.. _RotatE\: Knowledge Graph Embedding by Relational Rotation in Complex Space:
https://arxiv.org/pdf/1902.10197.pdf
Parameters:
entity (Tensor): entity embeddings of shape :math:`(|V|, 2d)`
relation (Tensor): relation embeddings of shape :math:`(|R|, d)`
h_index (LongTensor): index of head entities
t_index (LongTensor): index of tail entities
r_index (LongTensor): index of relations
"""
if backend == "native":
h = entity[h_index]
r = relation[r_index]
t = entity[t_index]
h_re, h_im = h.chunk(2, dim=-1)
r_re, r_im = torch.cos(r), torch.sin(r)
t_re, t_im = t.chunk(2, dim=-1)
x_re = h_re * r_re - h_im * r_im - t_re
x_im = h_re * r_im + h_im * r_re - t_im
x = torch.stack([x_re, x_im], dim=-1)
score = x.norm(p=2, dim=-1).sum(dim=-1)
elif backend == "fast":
score = RotatEFunction.apply(entity, relation, h_index, t_index, r_index)
else:
raise ValueError("Unknown embedding backend `%s`" % backend)
return score