forked from adaruna3/continual-kge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcwr_models.py
119 lines (101 loc) · 5.12 KB
/
cwr_models.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
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
from copy import deepcopy
import torch
import torch.nn as nn
from torch.autograd import Variable
class Analogy(nn.Module):
def __init__(self, num_ents, num_rels, hidden_size, device):
super(Analogy, self).__init__()
self.ent_re_embeddings = nn.Embedding(num_ents, int(hidden_size / 2.0)).to(device)
self.ent_im_embeddings = nn.Embedding(num_ents, int(hidden_size / 2.0)).to(device)
self.rel_re_embeddings = nn.Embedding(num_rels, int(hidden_size / 2.0)).to(device)
self.rel_im_embeddings = nn.Embedding(num_rels, int(hidden_size / 2.0)).to(device)
self.ent_embeddings = nn.Embedding(num_ents, int(hidden_size / 2.0)).to(device)
self.rel_embeddings = nn.Embedding(num_rels, int(hidden_size / 2.0)).to(device)
self.criterion = nn.Sigmoid().to(device)
self.device = device
self.init_weights()
# CWR related
self.cw_ent_updates = torch.zeros(num_ents, dtype=torch.float).to(device)
self.cw_rel_updates = torch.zeros(num_rels, dtype=torch.float).to(device)
def init_weights(self):
nn.init.xavier_uniform_(self.ent_re_embeddings.weight.data)
nn.init.xavier_uniform_(self.ent_im_embeddings.weight.data)
nn.init.xavier_uniform_(self.rel_re_embeddings.weight.data)
nn.init.xavier_uniform_(self.rel_im_embeddings.weight.data)
nn.init.xavier_uniform_(self.ent_embeddings.weight.data)
nn.init.xavier_uniform_(self.rel_embeddings.weight.data)
def _calc(self, h_re, h_im, h, t_re, t_im, t, r_re, r_im, r):
return torch.sum(r_re * h_re * t_re + r_re * h_im * t_im + r_im * h_re * t_im - r_im * h_im * t_re,-1) + \
torch.sum(h * t * r, -1)
def loss(self, score, batch_y):
return torch.sum(-torch.log(self.criterion(score * batch_y.float())))
def forward(self, batch_h, batch_r, batch_t, batch_y):
h_re = self.ent_re_embeddings(batch_h)
h_im = self.ent_im_embeddings(batch_h)
h = self.ent_embeddings(batch_h)
t_re = self.ent_re_embeddings(batch_t)
t_im = self.ent_im_embeddings(batch_t)
t = self.ent_embeddings(batch_t)
r_re = self.rel_re_embeddings(batch_r)
r_im = self.rel_im_embeddings(batch_r)
r = self.rel_embeddings(batch_r)
score = self._calc(h_re, h_im, h, t_re, t_im, t, r_re, r_im, r)
return self.loss(score, batch_y)
def predict(self, batch_h, batch_r, batch_t):
h_re = self.ent_re_embeddings(batch_h)
h_im = self.ent_im_embeddings(batch_h)
h = self.ent_embeddings(batch_h)
t_re = self.ent_re_embeddings(batch_t)
t_im = self.ent_im_embeddings(batch_t)
t = self.ent_embeddings(batch_t)
r_re = self.rel_re_embeddings(batch_r)
r_im = self.rel_im_embeddings(batch_r)
r = self.rel_embeddings(batch_r)
score = self._calc(h_re, h_im, h, t_re, t_im, t, r_re, r_im, r)
return -score.cpu().data.numpy()
class TransE(nn.Module):
def __init__(self, num_ents, num_rels, hidden_size, margin, neg_ratio, batch_size, device):
super(TransE, self).__init__()
self.ent_embeddings = nn.Embedding(num_ents, hidden_size).to(device)
self.rel_embeddings = nn.Embedding(num_rels, hidden_size).to(device)
self.criterion = nn.MarginRankingLoss(margin, reduction="sum").to(device)
self.neg_ratio = neg_ratio
self.batch_size = batch_size
self.device = device
self.init_weights()
# CWR related
self.cw_ent_updates = torch.zeros(num_ents, dtype=torch.float).to(device)
self.cw_rel_updates = torch.zeros(num_rels, dtype=torch.float).to(device)
def init_weights(self):
nn.init.xavier_uniform_(self.ent_embeddings.weight.data)
nn.init.xavier_uniform_(self.rel_embeddings.weight.data)
def _calc(self, h, r, t):
h = nn.functional.normalize(h, 2, -1)
r = nn.functional.normalize(r, 2, -1)
t = nn.functional.normalize(t, 2, -1)
return torch.norm(h + r - t, 1, -1)
def loss(self, p_score, n_score):
y = Variable(torch.Tensor([-1])).to(self.device)
return self.criterion(p_score, n_score, y)
def forward(self, batch_h, batch_r, batch_t, batch_y):
h = self.ent_embeddings(batch_h)
r = self.rel_embeddings(batch_r)
t = self.ent_embeddings(batch_t)
score = self._calc(h, r, t)
p_score = self.get_positive_score(score)
n_score = self.get_negative_score(score)
return self.loss(p_score, n_score)
def predict(self, batch_h, batch_r, batch_t):
h = self.ent_embeddings(batch_h)
r = self.rel_embeddings(batch_r)
t = self.ent_embeddings(batch_t)
score = self._calc(h, r, t)
return score.cpu().data.numpy()
def get_positive_score(self, score):
return score[0:len(score):self.neg_ratio+1]
def get_negative_score(self, score):
negs = torch.tensor([], dtype=torch.float32).to(self.device)
for idx in range(0, len(score), self.neg_ratio + 1):
batch_negs = score[idx + 1:idx + self.neg_ratio + 1]
negs = torch.cat((negs, torch.mean(batch_negs,0,keepdim=True)))
return negs