-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmodel.py
More file actions
74 lines (60 loc) · 3.41 KB
/
model.py
File metadata and controls
74 lines (60 loc) · 3.41 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
import numpy as np
import torch
import torch.nn as nn
class TransE(nn.Module):
def __init__(self, entity_count, relation_count, device, norm=1, dim=100, margin=1.0):
super(TransE, self).__init__()
self.entity_count = entity_count
self.relation_count = relation_count
self.device = device
self.norm = norm
self.dim = dim
self.entities_emb = self._init_enitity_emb()
self.relations_emb = self._init_relation_emb()
self.criterion = nn.MarginRankingLoss(margin=margin, reduction='none')
def _init_enitity_emb(self):
entities_emb = nn.Embedding(num_embeddings=self.entity_count + 1,
embedding_dim=self.dim,
padding_idx=self.entity_count)
uniform_range = 6 / np.sqrt(self.dim)
entities_emb.weight.data.uniform_(-uniform_range, uniform_range)
return entities_emb
def _init_relation_emb(self):
relations_emb = nn.Embedding(num_embeddings=self.relation_count + 1,
embedding_dim=self.dim,
padding_idx=self.relation_count)
uniform_range = 6 / np.sqrt(self.dim)
relations_emb.weight.data.uniform_(-uniform_range, uniform_range)
# -1 to avoid nan for OOV vector
relations_emb.weight.data[:-1, :].div_(relations_emb.weight.data[:-1, :].norm(p=1, dim=1, keepdim=True))
return relations_emb
def forward(self, positive_triplets: torch.LongTensor, negative_triplets: torch.LongTensor):
"""Return model losses based on the input.
:param positive_triplets: triplets of positives in Bx3 shape (B - batch, 3 - head, relation and tail)
:param negative_triplets: triplets of negatives in Bx3 shape (B - batch, 3 - head, relation and tail)
:return: tuple of the model loss, positive triplets loss component, negative triples loss component
"""
# -1 to avoid nan for OOV vector
self.entities_emb.weight.data[:-1, :].div_(self.entities_emb.weight.data[:-1, :].norm(p=2, dim=1, keepdim=True))
assert positive_triplets.size()[1] == 3
positive_distances = self._distance(positive_triplets)
assert negative_triplets.size()[1] == 3
negative_distances = self._distance(negative_triplets)
return self.loss(positive_distances, negative_distances), positive_distances, negative_distances
def predict(self, triplets: torch.LongTensor):
"""Calculated dissimilarity score for given triplets.
:param triplets: triplets in Bx3 shape (B - batch, 3 - head, relation and tail)
:return: dissimilarity score for given triplets
"""
return self._distance(triplets)
def loss(self, positive_distances, negative_distances):
target = torch.tensor([-1], dtype=torch.long, device=self.device)
return self.criterion(positive_distances, negative_distances, target)
def _distance(self, triplets):
"""Triplets should have shape Bx3 where dim 3 are head id, relation id, tail id."""
assert triplets.size()[1] == 3
heads = triplets[:, 0]
relations = triplets[:, 1]
tails = triplets[:, 2]
return (self.entities_emb(heads) + self.relations_emb(relations) - self.entities_emb(tails)).norm(p=self.norm,
dim=1)