-
Notifications
You must be signed in to change notification settings - Fork 23
/
model.py
361 lines (256 loc) · 14.1 KB
/
model.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import numpy as np
from typing import List, Tuple
import os
import const
import json
import torch.nn as nn
import torch
import torch.nn.functional as F
# torch_bool
try:
torch_bool = torch.bool
except:
torch_bool = torch.uint8
class Encoder(nn.Module):
def __init__(self, config: const.Config, embedding: nn.modules.sparse.Embedding) -> None:
super(Encoder, self).__init__()
self.config = config
self.hidden_size = config.encoder_num_units
self.emb_size = config.embedding_dim
self.words_number = config.words_number
self.maxlen = config.max_sentence_length
self.dropout = nn.Dropout(0.1)
self.embedding = embedding
self.cell_name = config.cell_name
if config.cell_name == 'gru':
self.rnn = nn.GRU(self.emb_size, self.hidden_size, bidirectional=True, batch_first=True)
elif config.cell_name == 'lstm':
self.rnn = nn.LSTM(self.emb_size, self.hidden_size, bidirectional=True, batch_first=True)
else:
raise ValueError('cell name should be gru/lstm!')
def forward(self, sentence: torch.Tensor, lengths: List[int]) -> Tuple[torch.Tensor, torch.Tensor]:
embedded = self.embedding(sentence)
# embedded = self.dropout(embedded)
if lengths:
embedded = nn.utils.rnn.pack_padded_sequence(
embedded, lengths=lengths, batch_first=True)
output, hidden = self.rnn(embedded)
if lengths:
output, _ = nn.utils.rnn.pad_packed_sequence(output, total_length=self.maxlen, batch_first=True)
output = (lambda a: sum(a)/2)(torch.split(output, self.hidden_size, dim=2))
if self.cell_name == 'gru':
hidden = (lambda a: sum(a)/2)(torch.split(hidden, 1, dim=0))
elif self.cell_name == 'lstm':
hidden = tuple(map(lambda state: sum(torch.split(state, 1, dim=0))/2, hidden))
# hidden = (lambda a: sum(a)/2)(torch.split(hidden, 1, dim=0))
return output, hidden
class Decoder(nn.Module):
def __init__(self, config: const.Config, embedding: nn.modules.sparse.Embedding, device) -> None:
super(Decoder, self).__init__()
self.device = device
self.cell_name = config.cell_name
self.decoder_type = config.decoder_type
self.hidden_size = config.decoder_num_units
self.emb_size = config.embedding_dim
self.words_number = config.words_number
self.maxlen = config.max_sentence_length
self.decodelen = config.decoder_output_max_length
self.relation_eos = config.relation_number
self.relation_number = config.relation_number
self.word_embedding = embedding
self.relation_embedding = nn.Embedding(config.relation_number + 1, config.embedding_dim)
self.sos_embedding = nn.Embedding(1, config.embedding_dim)
self.combine_inputs = nn.Linear(self.hidden_size + self.emb_size, self.emb_size)
self.attn = nn.Linear(self.hidden_size * 2, 1)
if self.cell_name == 'gru':
self.rnn = nn.GRU(self.emb_size, self.hidden_size, batch_first=True)
elif self.cell_name == 'lstm':
self.rnn = nn.LSTM(self.emb_size, self.hidden_size, batch_first=True)
self.do_eos = nn.Linear(self.hidden_size, 1)
self.do_predict = nn.Linear(self.hidden_size, self.relation_number)
self.fuse = nn.Linear(self.hidden_size * 2, 100)
self.do_copy_linear = nn.Linear(100, 1)
def calc_context(self, decoder_state: torch.Tensor, encoder_outputs: torch.Tensor) -> torch.Tensor:
# decoder_state.size() == torch.Size([1, 100, 1000])
# -> torch.Size([100, 1, 1000]) -> torch.Size([100, 80, 1000]) -cat-> torch.Size([100, 80, 2000])
attn_weight = torch.cat((decoder_state.permute(1, 0, 2).expand_as(encoder_outputs), encoder_outputs), dim=2)
attn_weight = F.softmax((self.attn(attn_weight)), dim=1)
attn_applied = torch.bmm(attn_weight.permute(0, 2, 1), encoder_outputs).squeeze(1)
return attn_applied
def do_copy(self, output: torch.Tensor, encoder_outputs: torch.Tensor) -> torch.Tensor:
out = torch.cat((output.unsqueeze(1).expand_as(encoder_outputs), encoder_outputs), dim=2)
out = F.selu(self.fuse(F.selu(out)))
out = self.do_copy_linear(out).squeeze(2)
# out = (self.do_copy_linear(out).squeeze(2))
return out
def _decode_step(self, rnn_cell: nn.modules,
emb: torch.Tensor,
decoder_state: torch.Tensor,
encoder_outputs: torch.Tensor,
first_entity_mask: torch.Tensor) \
-> Tuple[Tuple[torch.Tensor, torch.Tensor], torch.Tensor]:
if self.cell_name == 'gru':
decoder_state_h = decoder_state
elif self.cell_name == 'lstm':
decoder_state_h = decoder_state[0]
else:
raise ValueError('cell name should be lstm or gru')
context = self.calc_context(decoder_state_h, encoder_outputs)
output = self.combine_inputs(torch.cat((emb, context), dim=1))
output, decoder_state = rnn_cell(output.unsqueeze(1), decoder_state)
output = output.squeeze()
# eos_logits = F.selu(self.do_eos(output))
# predict_logits = F.selu(self.do_predict(output))
eos_logits = (self.do_eos(output))
predict_logits = (self.do_predict(output))
predict_logits = F.log_softmax(torch.cat((predict_logits, eos_logits), dim=1), dim=1)
copy_logits = self.do_copy(output, encoder_outputs)
# assert copy_logits.size() == first_entity_mask.size()
# original
# copy_logits = copy_logits * first_entity_mask
# copy_logits = copy_logits
copy_logits = torch.cat((copy_logits, eos_logits), dim=1)
copy_logits = F.log_softmax(copy_logits, dim=1)
# # bug fix
# copy_logits = torch.cat((copy_logits, eos_logits), dim=1)
# first_entity_mask = torch.cat((first_entity_mask, torch.ones_like(eos_logits)), dim=1)
#
# copy_logits = F.softmax(copy_logits, dim=1)
# copy_logits = copy_logits * first_entity_mask
# copy_logits = torch.clamp(copy_logits, 1e-10, 1.)
# copy_logits = torch.log(copy_logits)
return (predict_logits, copy_logits), decoder_state
def forward(self, *input):
raise NotImplementedError('abstract method!')
class MultiDecoder(Decoder):
def __init__(self, config: const.Config, embedding: nn.modules.sparse.Embedding, device) \
-> None:
super(MultiDecoder, self).__init__(config=config, embedding=embedding, device=device)
self.decoder_cell_number = config.decoder_output_max_length // 3
if self.cell_name == 'lstm':
self.rnns = nn.ModuleList([nn.LSTM(self.emb_size, self.hidden_size, batch_first=True)
for _ in range(self.decoder_cell_number)])
elif self.cell_name == 'gru':
self.rnns = nn.ModuleList([nn.GRU(self.emb_size, self.hidden_size, batch_first=True)
for _ in range(self.decoder_cell_number)])
else:
raise NameError('lstm or gru!')
def forward(self, sentence: torch.Tensor, decoder_state: torch.Tensor, encoder_outputs: torch.Tensor) \
-> Tuple[List[torch.Tensor], List[torch.Tensor]]:
# sos = go = 0
pred_action_list = []
pred_logits_list = []
go = torch.zeros(sentence.size()[0], dtype=torch.int64).to(self.device)
output = self.sos_embedding(go)
first_entity_mask = torch.ones(go.size()[0], self.maxlen).to(self.device)
if self.cell_name == 'gru':
previous_state = torch.zeros_like(decoder_state)
elif self.cell_name == 'lstm':
previous_state = tuple(map(torch.zeros_like, decoder_state))
encoder_state = decoder_state
for decoder in self.rnns:
if self.cell_name == 'gru':
decoder_state = (encoder_state + previous_state) / 2
elif self.cell_name == 'lstm':
decoder_state = ((encoder_state[0] + previous_state[0])/2, (encoder_state[1] + previous_state[1])/2)
for t in range(3):
bag, decoder_state = self._decode_step(decoder, output, decoder_state, encoder_outputs, first_entity_mask)
predict_logits, copy_logits = bag
if t % 3 == 0:
action_logits = predict_logits
else:
action_logits = copy_logits
max_action = torch.argmax(action_logits, dim=1).detach()
pred_action_list.append(max_action)
pred_logits_list.append(action_logits)
# next time step
if t % 3 == 0:
output = max_action
output = self.relation_embedding(output)
else:
copy_index = torch.zeros_like(sentence).scatter_(1, max_action.unsqueeze(1), 1).to(torch_bool)
output = sentence[copy_index]
output = self.word_embedding(output)
if t % 3 == 1:
first_entity_mask = torch.ones(go.size()[0], self.maxlen + 1).to(self.device)
index = torch.zeros_like(first_entity_mask).scatter_(1, max_action.unsqueeze(1), 1).to(torch_bool)
first_entity_mask[index] = 0
first_entity_mask = first_entity_mask[:, :-1]
else:
first_entity_mask = torch.ones(go.size()[0], self.maxlen).to(self.device)
previous_state = decoder_state
return pred_action_list, pred_logits_list
class OneDecoder(Decoder):
def __init__(self, config: const.Config, embedding: nn.modules.sparse.Embedding, device) \
-> None:
super(OneDecoder, self).__init__(config=config, embedding=embedding, device=device)
def forward(self, sentence: torch.Tensor, decoder_state: torch.Tensor, encoder_outputs: torch.Tensor) \
-> Tuple[List[torch.Tensor], List[torch.Tensor]]:
# sos = go = 0
pred_action_list = []
pred_logits_list = []
go = torch.zeros(sentence.size()[0], dtype=torch.int64).to(self.device)
output = self.sos_embedding(go)
first_entity_mask = torch.ones(go.size()[0], self.maxlen).to(self.device)
for t in range(self.decodelen):
bag, decoder_state = self._decode_step(self.rnn, output, decoder_state, encoder_outputs, first_entity_mask)
predict_logits, copy_logits = bag
if t % 3 == 0:
action_logits = predict_logits
else:
action_logits = copy_logits
max_action = torch.argmax(action_logits, dim=1).detach()
pred_action_list.append(max_action)
pred_logits_list.append(action_logits)
# next time step
if t % 3 == 0:
output = max_action
output = self.relation_embedding(output)
else:
copy_index = torch.zeros_like(sentence).scatter_(1, max_action.unsqueeze(1), 1).to(torch_bool)
output = sentence[copy_index]
output = self.word_embedding(output)
if t % 3 == 1:
first_entity_mask = torch.ones(go.size()[0], self.maxlen + 1).to(self.device)
index = torch.zeros_like(first_entity_mask).scatter_(1, max_action.unsqueeze(1), 1).to(torch_bool)
first_entity_mask[index] = 0
first_entity_mask = first_entity_mask[:, :-1]
else:
first_entity_mask = torch.ones(go.size()[0], self.maxlen).to(self.device)
return pred_action_list, pred_logits_list
class Seq2seq(nn.Module):
def __init__(self, config: const.Config, device, load_emb=False, update_emb=True):
super(Seq2seq, self).__init__()
self.device = device
self.config = config
self.emb_size = config.embedding_dim
self.words_number = config.words_number
self.maxlen = config.max_sentence_length
self.word_embedding = nn.Embedding(self.words_number + 1, self.emb_size)
if load_emb:
self.load_pretrain_emb(config)
self.word_embedding.weight.requires_grad = update_emb
self.encoder = Encoder(config, embedding=self.word_embedding)
if config.decoder_type == 'one':
self.decoder = OneDecoder(config, embedding=self.word_embedding, device=device)
elif config.decoder_type == 'multi':
self.decoder = MultiDecoder(config, embedding=self.word_embedding, device=device)
else:
raise ValueError('decoder type one/multi!!')
self.to(self.device)
def load_pretrain_emb(self, config: const.Config) -> None:
if os.path.isfile(config.words_id2vector_filename):
# logger.info('Word Embedding init from %s' % config.words_id2vector_filename)
print('load_embedding!')
words_id2vec = json.load(open(config.words_id2vector_filename, 'r'))
words_vectors = [0] * (len(words_id2vec) + 1)
for i, key in enumerate(words_id2vec):
words_vectors[int(key)] = words_id2vec[key]
# words_vectors[len(words_id2vec) + 1] = [0] * len(words_id2vec[key])
words_vectors[len(words_id2vec)] = [0] * len(words_id2vec[key])
self.word_embedding.weight.data.copy_(torch.from_numpy(np.array(words_vectors)))
def forward(self, sentence: torch.Tensor, sentence_eos: torch.Tensor, lengths: List[int]) \
-> Tuple[torch.Tensor, torch.Tensor]:
o, h = self.encoder(sentence, lengths)
pred_action_list, pred_logits_list = self.decoder(sentence=sentence_eos, decoder_state=h, encoder_outputs=o)
return pred_action_list, pred_logits_list