-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_new.py
232 lines (193 loc) · 9.4 KB
/
model_new.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
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
import torch
import torch.nn as nn
class TuckERLayer(nn.Module):
def __init__(self, dim, r_dim):
super(TuckERLayer, self).__init__()
self.W = nn.Parameter(torch.rand(r_dim, dim, dim))
nn.init.xavier_uniform_(self.W.data)
self.bn0 = nn.BatchNorm1d(dim)
self.bn1 = nn.BatchNorm1d(dim)
# original: 0.3, 0.4, 0.5
self.input_drop = nn.Dropout(0.3)
self.hidden_drop = nn.Dropout(0.4)
self.out_drop = nn.Dropout(0.5)
def forward(self, e_embed, r_embed):
x = self.bn0(e_embed)
x = self.input_drop(x)
x = x.view(-1, 1, x.size(1))
r = torch.mm(r_embed, self.W.view(r_embed.size(1), -1))
r = r.view(-1, x.size(2), x.size(2))
r = self.hidden_drop(r)
x = torch.bmm(x, r)
x = x.view(-1, x.size(2))
x = self.bn1(x)
x = self.out_drop(x)
return x
class Similarity(nn.Module):
"""
Dot product or cosine similarity
"""
def __init__(self, temp):
super().__init__()
self.temp = temp
self.cos = nn.CosineSimilarity(dim=-1)
def forward(self, x, y):
return self.cos(x, y) / self.temp
class ContrastiveLoss(nn.Module):
def __init__(self, temp=0.5):
super().__init__()
self.loss = nn.CrossEntropyLoss()
self.sim_func = Similarity(temp=temp)
def forward(self, emb1, emb2):
batch_sim = self.sim_func(emb1.unsqueeze(1), emb2.unsqueeze(0))
labels = torch.arange(batch_sim.size(0)).long().to('cuda')
return self.loss(batch_sim, labels)
class VISTATucker(nn.Module):
def __init__(
self,
num_ent,
num_rel,
rel_vis,
dim_vis,
rel_txt,
dim_txt,
ent_vis_mask,
ent_txt_mask,
rel_vis_mask,
dim_str,
num_head,
dim_hid,
num_layer_enc_ent,
num_layer_enc_rel,
num_layer_dec,
dropout = 0.1,
emb_dropout = 0.6,
vis_dropout = 0.1,
txt_dropout = 0.1,
visual_token_index = None,
text_token_index = None,
score_function = "tucker"
):
super(VISTATucker, self).__init__()
self.dim_str = dim_str
self.num_head = num_head
self.dim_hid = dim_hid
self.num_ent = num_ent
self.num_rel = num_rel
self.rel_vis = rel_vis
self.rel_txt = None
self.visual_token_index = visual_token_index
self.visual_token_embedding = nn.Embedding(num_embeddings=8193, embedding_dim=self.dim_str)
self.text_token_index = text_token_index
self.text_token_embedding = nn.Embedding(num_embeddings=15000, embedding_dim=self.dim_str)
self.score_function = score_function
false_ents = torch.full((self.num_ent,1),False).cuda()
self.ent_mask = torch.cat([false_ents, false_ents, ent_vis_mask, ent_txt_mask], dim = 1)
# print(self.ent_mask.shape)
false_rels = torch.full((self.num_rel,1),False).cuda()
self.rel_mask = torch.cat([false_rels, false_rels], dim = 1)
self.ent_token = nn.Parameter(torch.Tensor(1, 1, dim_str))
self.rel_token = nn.Parameter(torch.Tensor(1, 1, dim_str))
self.ent_embeddings = nn.Parameter(torch.Tensor(num_ent, 1, dim_str))
self.rel_embeddings = nn.Parameter(torch.Tensor(num_rel, 1 ,dim_str))
self.lp_token = nn.Parameter(torch.Tensor(1,dim_str))
self.str_ent_ln = nn.LayerNorm(dim_str)
self.str_rel_ln = nn.LayerNorm(dim_str)
self.vis_ln = nn.LayerNorm(dim_str)
self.txt_ln = nn.LayerNorm(dim_str)
self.embdr = nn.Dropout(p = emb_dropout)
self.visdr = nn.Dropout(p = vis_dropout)
self.txtdr = nn.Dropout(p = txt_dropout)
self.pos_str_ent = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_vis_ent = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_txt_ent = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_str_rel = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_vis_rel = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_txt_rel = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_head = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_rel = nn.Parameter(torch.Tensor(1,1,dim_str))
self.pos_tail = nn.Parameter(torch.Tensor(1,1,dim_str))
ent_encoder_layer = nn.TransformerEncoderLayer(dim_str, num_head, dim_hid, dropout, batch_first = True)
self.ent_encoder = nn.TransformerEncoder(ent_encoder_layer, num_layer_enc_ent)
rel_encoder_layer = nn.TransformerEncoderLayer(dim_str, num_head, dim_hid, dropout, batch_first = True)
self.rel_encoder = nn.TransformerEncoder(rel_encoder_layer, num_layer_enc_rel)
decoder_layer = nn.TransformerEncoderLayer(dim_str, num_head, dim_hid, dropout, batch_first = True)
self.decoder = nn.TransformerEncoder(decoder_layer, num_layer_dec)
self.contrastive = ContrastiveLoss(temp=0.5)
self.num_con = 512
if self.score_function == "tucker":
self.tucker_decoder = TuckERLayer(dim_str, dim_str)
else:
pass
self.init_weights()
def init_weights(self):
nn.init.xavier_uniform_(self.ent_embeddings)
nn.init.xavier_uniform_(self.rel_embeddings)
# nn.init.xavier_uniform_(self.proj_ent_vis.weight)
# nn.init.xavier_uniform_(self.proj_rel_vis.weight)
# nn.init.xavier_uniform_(self.proj_txt.weight)
nn.init.xavier_uniform_(self.ent_token)
nn.init.xavier_uniform_(self.rel_token)
nn.init.xavier_uniform_(self.lp_token)
nn.init.xavier_uniform_(self.pos_str_ent)
nn.init.xavier_uniform_(self.pos_vis_ent)
nn.init.xavier_uniform_(self.pos_txt_ent)
nn.init.xavier_uniform_(self.pos_str_rel)
nn.init.xavier_uniform_(self.pos_vis_rel)
nn.init.xavier_uniform_(self.pos_txt_rel)
nn.init.xavier_uniform_(self.pos_head)
nn.init.xavier_uniform_(self.pos_rel)
nn.init.xavier_uniform_(self.pos_tail)
nn.init.xavier_uniform_(self.visual_token_embedding.weight)
nn.init.xavier_uniform_(self.text_token_embedding.weight)
# self.proj_ent_vis.bias.data.zero_()
# self.proj_rel_vis.bias.data.zero_()
# self.proj_txt.bias.data.zero_()
def forward(self):
ent_tkn = self.ent_token.tile(self.num_ent, 1, 1)
rep_ent_str = self.embdr(self.str_ent_ln(self.ent_embeddings)) + self.pos_str_ent
entity_visual_tokens = self.visual_token_embedding(self.visual_token_index)
rep_ent_vis = self.visdr(self.vis_ln(entity_visual_tokens)) + self.pos_vis_ent
entity_text_tokens = self.text_token_embedding(self.text_token_index)
rep_ent_txt = self.txtdr(self.txt_ln(entity_text_tokens)) + self.pos_txt_ent
ent_seq = torch.cat([ent_tkn, rep_ent_str, rep_ent_vis, rep_ent_txt], dim = 1)
ent_embs = self.ent_encoder(ent_seq, src_key_padding_mask = self.ent_mask)[:,0]
# rel_tkn = self.rel_token.tile(self.num_rel, 1, 1)
rep_rel_str = self.embdr(self.str_rel_ln(self.rel_embeddings)) # + self.pos_str_rel
# rel_seq = torch.cat([rel_tkn, rep_rel_str], dim = 1)
# rel_embs = self.rel_encoder(rel_seq, src_key_padding_mask = self.rel_mask)[:,0]
return torch.cat([ent_embs, self.lp_token], dim = 0), rep_rel_str.squeeze(dim=1)
def contrastive_loss(self, emb_ent1):
ent_tkn = self.ent_token.tile(self.num_ent, 1, 1)
rep_ent_str = self.embdr(self.str_ent_ln(self.ent_embeddings)) + self.pos_str_ent
entity_visual_tokens = self.visual_token_embedding(self.visual_token_index)
rep_ent_vis = self.visdr(self.vis_ln(entity_visual_tokens)) + self.pos_vis_ent
entity_text_tokens = self.text_token_embedding(self.text_token_index)
rep_ent_txt = self.txtdr(self.txt_ln(entity_text_tokens)) + self.pos_txt_ent
ent_seq = torch.cat([ent_tkn, rep_ent_str, rep_ent_vis, rep_ent_txt], dim = 1)
ent_embs = self.ent_encoder(ent_seq, src_key_padding_mask = self.ent_mask)[:,0]
emb_ent2 = torch.cat([ent_embs, self.lp_token], dim = 0)
select_ents = torch.randperm(emb_ent1.shape[0])[: self.num_con]
contrastive_loss = self.contrastive(emb_ent1[select_ents], emb_ent2[select_ents])
return contrastive_loss
def score(self, emb_ent, emb_rel, triplets):
# args:
# emb_ent: [num_ent, emb_dim]
# emb_rel: [num_rel, emb_dim]
# triples: [batch_size, 3]
# return:
# scores: [batch_size, num_ent]
h_seq = emb_ent[triplets[:,0] - self.num_rel].unsqueeze(dim = 1) + self.pos_head
r_seq = emb_rel[triplets[:,1] - self.num_ent].unsqueeze(dim = 1) + self.pos_rel
t_seq = emb_ent[triplets[:,2] - self.num_rel].unsqueeze(dim = 1) + self.pos_tail
dec_seq = torch.cat([h_seq, r_seq, t_seq], dim = 1)
output_dec = self.decoder(dec_seq)
rel_emb = output_dec[:, 1, :]
ent_emb = output_dec[triplets != self.num_ent + self.num_rel]
if self.score_function == "tucker":
tucker_emb = self.tucker_decoder(ent_emb, rel_emb)
score = torch.mm(tucker_emb, emb_ent[:-1].transpose(1, 0))
else:
output_dec = self.decoder(dec_seq)
score = torch.inner(ent_emb, emb_ent[:-1])
return score