-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_mygo.py
200 lines (170 loc) · 9.44 KB
/
model_mygo.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
import torch
import torch.nn as nn
from model_new import *
class MyGO(nn.Module):
def __init__(
self,
num_ent,
num_rel,
ent_vis_mask,
ent_txt_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(MyGO, 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
visual_tokens = torch.load("tokens/visual.pth")
textual_tokens = torch.load("tokens/textual.pth")
self.visual_token_index = visual_token_index
self.visual_token_embedding = nn.Embedding.from_pretrained(visual_tokens).requires_grad_(False)
self.text_token_index = text_token_index
self.text_token_embedding = nn.Embedding.from_pretrained(textual_tokens).requires_grad_(False)
self.score_function = score_function
self.visual_token_embedding.requires_grad_(False)
self.text_token_embedding.requires_grad_(False)
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))
self.proj_ent_vis = nn.Linear(32, dim_str)
self.proj_ent_txt = nn.Linear(768, dim_str)
# self.proj_rel_vis = nn.Linear(dim_vis * 3, 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 = 256
self.num_vis = ent_vis_mask.shape[1]
if self.score_function == "tucker":
self.tucker_decoder = TuckERLayer(dim_str, dim_str)
else:
pass
self.init_weights()
torch.save(self.visual_token_embedding, open("visual_token.pth", "wb"))
torch.save(self.text_token_embedding, open("textual_token.pth", "wb"))
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_ent_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)
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(self.proj_ent_vis(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(self.proj_ent_txt(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]
rep_rel_str = self.embdr(self.str_rel_ln(self.rel_embeddings))
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(self.proj_ent_vis(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(self.proj_ent_txt(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])
# print(contrastive_loss)
return contrastive_loss
def contrastive_loss_finegrained(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(self.proj_ent_vis(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(self.proj_ent_txt(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: [ent_num, seq_len, embed_dim]
ent_embs = self.ent_encoder(ent_seq, src_key_padding_mask = self.ent_mask)
emb_ent2 = torch.cat([ent_embs[:,0], self.lp_token], dim = 0)
ent_emb3 = torch.cat([torch.mean(ent_embs, dim=1), self.lp_token], dim = 0)
ent_emb4 = torch.cat([torch.mean(ent_embs[:, 2: 2 + self.num_vis, :], dim=1), self.lp_token], dim=0)
ent_emb5 = torch.cat([torch.mean(ent_embs[:, 2 + self.num_vis: -1, :], dim=1), self.lp_token], dim=0)
select_ents = torch.randperm(emb_ent1.shape[0])[: self.num_con]
contrastive_loss = 0
for emb in [emb_ent2, ent_emb3, ent_emb4, ent_emb5]:
contrastive_loss += self.contrastive(emb_ent1[select_ents], emb[select_ents])
contrastive_loss /= 4
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, :]
ctx_emb = output_dec[triplets == self.num_ent + self.num_rel]
# indexs = triplets != self.num_ent + self.num_rel
# indexs[:, 1] = False
# ent_emb = output_dec[indexs]
if self.score_function == "tucker":
tucker_emb = self.tucker_decoder(ctx_emb, rel_emb)
score = torch.mm(tucker_emb, emb_ent[:-1].transpose(1, 0))
else:
# output_dec = self.decoder(dec_seq)
score = torch.inner(ctx_emb, emb_ent[:-1])
return score