/
bert_encoder.py
246 lines (224 loc) · 8.84 KB
/
bert_encoder.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import torch
import torch.nn as nn
from .base_encoder import BaseEncoder
from transformers import BertModel, BertTokenizer
class BERTEncoder(nn.Module):
def __init__(self, max_length, pretrain_path, blank_padding=True, mask_entity=False):
"""
Args:
max_length: max length of sentence
pretrain_path: path of pretrain model
"""
super().__init__()
self.max_length = max_length
self.blank_padding = blank_padding
self.hidden_size = 768
self.mask_entity = mask_entity
self.bert = BertModel.from_pretrained(pretrain_path)
self.tokenizer = BertTokenizer.from_pretrained(pretrain_path)
def forward(self, token, att_mask):
"""
Args:
token: (B, L), index of tokens
att_mask: (B, L), attention mask (1 for contents and 0 for padding)
Return:
(B, H), representations for sentences
"""
_, x = self.bert(token, attention_mask=att_mask)
return x
def tokenize(self, item):
"""
Args:
item: data instance containing 'text' / 'token', 'h' and 't'
Return:
Name of the relation of the sentence
"""
# Sentence -> token
if 'text' in item:
sentence = item['text']
is_token = False
else:
sentence = item['token']
is_token = True
pos_head = item['h']['pos']
pos_tail = item['t']['pos']
if not is_token:
pos_min = pos_head
pos_max = pos_tail
if pos_head[0] > pos_tail[0]:
pos_min = pos_tail
pos_max = pos_head
rev = True
else:
rev = False
sent0 = self.tokenizer.tokenize(sentence[:pos_min[0]])
ent0 = self.tokenizer.tokenize(sentence[pos_min[0]:pos_min[1]])
sent1 = self.tokenizer.tokenize(sentence[pos_min[1]:pos_max[0]])
ent1 = self.tokenizer.tokenize(sentence[pos_max[0]:pos_max[1]])
sent2 = self.tokenizer.tokenize(sentence[pos_max[1]:])
if self.mask_entity:
ent0 = ['[unused4]']
ent1 = ['[unused5]']
if rev:
ent0 = ['[unused5]']
ent1 = ['[unused4]']
pos_head = [len(sent0), len(sent0) + len(ent0)]
pos_tail = [
len(sent0) + len(ent0) + len(sent1),
len(sent0) + len(ent0) + len(sent1) + len(ent1)
]
if rev:
pos_tail = [len(sent0), len(sent0) + len(ent0)]
pos_head = [
len(sent0) + len(ent0) + len(sent1),
len(sent0) + len(ent0) + len(sent1) + len(ent1)
]
tokens = sent0 + ent0 + sent1 + ent1 + sent2
else:
tokens = sentence
# Token -> index
re_tokens = ['[CLS]']
cur_pos = 0
for token in tokens:
token = token.lower()
if cur_pos == pos_head[0] and not self.mask_entity:
re_tokens.append('[unused0]')
if cur_pos == pos_tail[0] and not self.mask_entity:
re_tokens.append('[unused1]')
re_tokens += self.tokenizer.tokenize(token)
if cur_pos == pos_head[1] - 1 and not self.mask_entity:
re_tokens.append('[unused2]')
if cur_pos == pos_tail[1] - 1 and not self.mask_entity:
re_tokens.append('[unused3]')
cur_pos += 1
re_tokens.append('[SEP]')
indexed_tokens = self.tokenizer.convert_tokens_to_ids(re_tokens)
avai_len = len(indexed_tokens)
# Padding
if self.blank_padding:
while len(indexed_tokens) < self.max_length:
indexed_tokens.append(0) # 0 is id for [PAD]
indexed_tokens = indexed_tokens[:self.max_length]
indexed_tokens = torch.tensor(indexed_tokens).long().unsqueeze(0) # (1, L)
# Attention mask
att_mask = torch.zeros(indexed_tokens.size()).long() # (1, L)
att_mask[0, :avai_len] = 1
return indexed_tokens, att_mask
class BERTEntityEncoder(nn.Module):
def __init__(self, max_length, pretrain_path, blank_padding=True):
"""
Args:
max_length: max length of sentence
pretrain_path: path of pretrain model
"""
super().__init__()
self.max_length = max_length
self.blank_padding = blank_padding
self.hidden_size = 768 * 2
self.bert = BertModel.from_pretrained(pretrain_path)
self.tokenizer = BertTokenizer.from_pretrained(pretrain_path)
self.linear = nn.Linear(self.hidden_size, self.hidden_size)
def forward(self, token, att_mask, pos1, pos2):
"""
Args:
token: (B, L), index of tokens
att_mask: (B, L), attention mask (1 for contents and 0 for padding)
pos1: (B, 1), position of the head entity starter
pos2: (B, 1), position of the tail entity starter
Return:
(B, 2H), representations for sentences
"""
hidden, _ = self.bert(token, attention_mask=att_mask)
# Get entity start hidden state
onehot = torch.zeros(hidden.size()[:2]).float() # (B, L)
if torch.cuda.is_available():
onehot = onehot.cuda()
onehot_head = onehot.scatter_(1, pos1, 1)
onehot_tail = onehot.scatter_(1, pos2, 1)
head_hidden = (onehot_head.unsqueeze(2) * hidden).sum(1) # (B, H)
tail_hidden = (onehot_tail.unsqueeze(2) * hidden).sum(1) # (B, H)
x = torch.cat([head_hidden, tail_hidden], 1) # (B, 2H)
x = self.linear(x)
return x
def tokenize(self, item):
"""
Args:
item: data instance containing 'text' / 'token', 'h' and 't'
Return:
Name of the relation of the sentence
"""
# Sentence -> token
if 'text' in item:
sentence = item['text']
is_token = False
else:
sentence = item['token']
is_token = True
pos_head = item['h']['pos']
pos_tail = item['t']['pos']
if not is_token:
pos_min = pos_head
pos_max = pos_tail
if pos_head[0] > pos_tail[0]:
pos_min = pos_tail
pos_max = pos_head
rev = True
else:
rev = False
sent0 = self.tokenizer.tokenize(sentence[:pos_min[0]])
ent0 = self.tokenizer.tokenize(sentence[pos_min[0]:pos_min[1]])
sent1 = self.tokenizer.tokenize(sentence[pos_min[1]:pos_max[0]])
ent1 = self.tokenizer.tokenize(sentence[pos_max[0]:pos_max[1]])
sent2 = self.tokenizer.tokenize(sentence[pos_max[1]:])
pos_head = [len(sent0), len(sent0) + len(ent0)]
pos_tail = [
len(sent0) + len(ent0) + len(sent1),
len(sent0) + len(ent0) + len(sent1) + len(ent1)
]
if rev:
pos_tail = [len(sent0), len(sent0) + len(ent0)]
pos_head = [
len(sent0) + len(ent0) + len(sent1),
len(sent0) + len(ent0) + len(sent1) + len(ent1)
]
tokens = sent0 + ent0 + sent1 + ent1 + sent2
else:
tokens = sentence
# Token -> index
re_tokens = ['[CLS]']
cur_pos = 0
pos1 = 0
pos2 = 0
for token in tokens:
token = token.lower()
if cur_pos == pos_head[0]:
pos1 = len(re_tokens)
re_tokens.append('[unused0]')
if cur_pos == pos_tail[0]:
pos2 = len(re_tokens)
re_tokens.append('[unused1]')
re_tokens += self.tokenizer.tokenize(token)
if cur_pos == pos_head[1] - 1:
re_tokens.append('[unused2]')
if cur_pos == pos_tail[1] - 1:
re_tokens.append('[unused3]')
cur_pos += 1
re_tokens.append('[SEP]')
pos1 = min(self.max_length - 1, pos1)
pos2 = min(self.max_length - 1, pos2)
indexed_tokens = self.tokenizer.convert_tokens_to_ids(re_tokens)
avai_len = len(indexed_tokens)
# Position
pos1 = torch.tensor([[pos1]]).long()
pos2 = torch.tensor([[pos2]]).long()
# Padding
if self.blank_padding:
while len(indexed_tokens) < self.max_length:
indexed_tokens.append(0) # 0 is id for [PAD]
indexed_tokens = indexed_tokens[:self.max_length]
indexed_tokens = torch.tensor(indexed_tokens).long().unsqueeze(
0) # (1, L)
# Attention mask
att_mask = torch.zeros(indexed_tokens.size()).long() # (1, L)
att_mask[0, :avai_len] = 1
return indexed_tokens, att_mask, pos1, pos2