-
Notifications
You must be signed in to change notification settings - Fork 1
/
seq2seq.py
390 lines (264 loc) · 12.6 KB
/
seq2seq.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import torch
import torch.nn as nn
import torch.autograd as autograd
from torch.autograd import Variable
import torch.nn.functional as F
from model import TransformerModel, SharedTransformerModel, TransformerDecoder, DEFAULT_CONFIG, DEFAULT_SHARED_CONFIG
import random
import numpy as np
import math
import sys
class Seq2Seq(nn.Module):
def __init__(self, mask, hps):
super(Seq2Seq, self).__init__()
self.hps = hps
self.vocab_size = hps.vocab_size
self.emb_dim = hps.emb_dim
self.max_len = hps.max_len
self.batch_size = hps.batch_size
self.test_batch_size = hps.test_batch_size
self.mask = mask
args = DEFAULT_CONFIG
shared_args = DEFAULT_SHARED_CONFIG
self.irony_encoder = TransformerModel(args, self.vocab_size + self.max_len, self.max_len)
self.non_encoder = TransformerModel(args, self.vocab_size + self.max_len, self.max_len)
self.shared_encoder = SharedTransformerModel(shared_args, self.vocab_size + self.max_len, self.max_len)
self.shared_decoder = SharedTransformerModel(shared_args, self.vocab_size + self.max_len, self.max_len)
self.irony_decoder = TransformerDecoder(args, self.vocab_size + self.max_len, self.max_len, True)
self.non_decoder = TransformerDecoder(args, self.vocab_size + self.max_len, self.max_len, True)
def forward(self, src, trg, lengths, tags):
pass
def test_ae(self, src, tags):
batch_size = src.size(0)
max_len = src.size(2)
trg_vocab_size = self.vocab_size
tag = tags[0].item()
if tag == 0:
encoder = self.non_encoder
decoder = self.non_decoder
elif tag == 1:
encoder = self.irony_encoder
decoder = self.irony_decoder
output = encoder(src)
output = self.shared_encoder(output)
output = self.shared_decoder(output)
output, _ = decoder(output)
bias = torch.FloatTensor(self.test_batch_size, self.vocab_size).fill_(0).cuda()
for i in range(output.size(1)):
output[:, i, :] += bias
indices = torch.argmax(output[:, i, :], 1)
for j in range(self.test_batch_size):
idx = indices[j]
if self.mask[idx] == 1:
bias[j][idx] = -1e30
del bias
outputs = output.argmax(2).squeeze(0) #[max_len, ]
return outputs
def test_tran(self, src, tags):
batch_size = src.size(0)
max_len = src.size(2)
trg_vocab_size = self.vocab_size
tag = tags[0].item()
if tag == 0:
encoder = self.non_encoder
decoder = self.irony_decoder
elif tag == 1:
encoder = self.irony_encoder
decoder = self.non_decoder
output = encoder(src)
output = self.shared_encoder(output)
output = self.shared_decoder(output)
output, _ = decoder(output)
bias = torch.FloatTensor(self.test_batch_size, self.vocab_size).fill_(0).cuda()
for i in range(output.size(1)):
output[:, i, :] += bias
indices = torch.argmax(output[:, i, :], 1)
for j in range(self.test_batch_size):
idx = indices[j]
if self.mask[idx] == 1:
bias[j][idx] = -1e30
del bias
outputs = output.argmax(2).squeeze(0) #[max_len, ]
return outputs
def find_first_eos(self, sents):
# sents [batch, len]
lengths = []
for s in sents:
length = len(s)
idx = 0
for w in s:
if w.item() == 3:
break
idx += 1
if idx != length:
idx += 1
lengths.append(idx)
return lengths
def batchBT(self, src, trg, tags):
batch_size = src.size(0)
max_len = src.size(2)
trg_vocab_size = self.vocab_size
tag = tags[0].item()
if tag == 0:
encoder = self.non_encoder
decoder = self.irony_decoder
elif tag == 1:
encoder = self.irony_encoder
decoder = self.non_decoder
output = encoder(src)
output = self.shared_encoder(output)
output = self.shared_decoder(output)
_, h = decoder(output)
output = self.shared_decoder(h)
output, _ = decoder(output)
loss_fn = nn.CrossEntropyLoss(ignore_index=0)
loss = loss_fn(output.view(-1, trg_vocab_size), trg[:,:,:,0].contiguous().view(-1))
return loss
def batchNLLLoss(self, src, trg, tags):
#src = [batch size,src sent len]
#trg = [batch size,trg sent len]
batch_size = src.size(0)
max_len = src.size(2)
trg_vocab_size = self.vocab_size
tag = tags[0].item()
if tag == 0:
encoder = self.non_encoder
decoder = self.non_decoder
elif tag == 1:
encoder = self.irony_encoder
decoder = self.irony_decoder
output = encoder(src)
output = self.shared_encoder(output)
output = self.shared_decoder(output)
output, _ = decoder(output)
# bias = torch.FloatTensor(self.batch_size, self.vocab_size).fill_(0).cuda()
# for i in range(output.size(1)):
# output[:, i, :] += bias
# indices = torch.argmax(output[:, i, :], 1)
# for j in range(self.batch_size):
# idx = indices[j]
# if self.mask[idx] == 1:
# bias[j][idx] = -1e30
# del bias
loss_fn = nn.CrossEntropyLoss(ignore_index=0)
loss = loss_fn(output.view(-1, trg_vocab_size), trg[:,:,:,0].contiguous().view(-1))
return loss
def get_reward(self, src, sample_y, base_y, dis, senti, non_senti, idx2word, mode):
# src [batch, len]
# sample_y base_y [batch, len]
threshold1 = 0.35 # irony senti
threshold2 = 0.25 # non senti
sample_lengths = self.find_first_eos(sample_y)
base_lengths = self.find_first_eos(base_y)
sample_zeros = torch.LongTensor(self.batch_size, self.max_len).fill_(0)
base_zeros = torch.LongTensor(self.batch_size, self.max_len).fill_(0)
for i in range(self.batch_size):
sample_zeros[i, :sample_lengths[i]] = sample_y[i, :sample_lengths[i]]
base_zeros[i, :base_lengths[i]] = base_y[i, :base_lengths[i]]
sample_y = sample_zeros.cuda()
base_y = base_zeros.cuda()
del sample_zeros
del base_zeros
# irony
output_src = F.softmax(dis(src), dim=-1)[:, 1]
output_base = F.softmax(dis(base_y), dim=-1)[:, 1]
output_sample = F.softmax(dis(sample_y), dim=-1)[:, 1]
# senti
if mode == 0:
senti_output_src = F.softmax(senti(src), dim=-1)[:, 1]
senti_output_base = F.softmax(non_senti(base_y), dim=-1)[:, 1]
senti_output_sample = F.softmax(non_senti(sample_y), dim=-1)[:, 1]
elif mode == 1:
senti_output_src = F.softmax(non_senti(src), dim=-1)[:, 1]
senti_output_base = F.softmax(senti(base_y), dim=-1)[:, 1]
senti_output_sample = F.softmax(senti(sample_y), dim=-1)[:, 1]
reward_RL_sample = 0
reward_RL_base = 0
#reward
if mode == 0:
tmp1 = output_src - output_sample
tmp2 = output_src - output_base
del output_src
del output_sample
del output_base
tmp3 = 1 - abs(senti_output_src - threshold1 - senti_output_sample + threshold2)
tmp4 = 1 - abs(senti_output_src - threshold1 - senti_output_base + threshold2)
del senti_output_src
del senti_output_sample
del senti_output_base
elif mode == 1:
tmp1 = output_sample - output_src
tmp2 = output_base - output_src
del output_src
del output_sample
del output_base
tmp3 = 1 - abs(senti_output_src - threshold1 - senti_output_sample + threshold2)
tmp4 = 1 - abs(senti_output_src - threshold1 - senti_output_base + threshold2)
del senti_output_src
del senti_output_sample
del senti_output_base
beta = 0.5
reward_RL_sample = (1 + beta * beta) * tmp1 * tmp3 / (beta * beta * tmp3 + tmp1)
reward_RL_base = (1 + beta * beta) * tmp2 * tmp4 / (beta * beta * tmp4 + tmp2)
reward_RL_sample = Variable(reward_RL_sample, requires_grad = False)
reward_RL_base = Variable(reward_RL_base, requires_grad = False)
irony_rw_sample = (torch.sum(tmp1) / tmp1.shape[0]).item()
irony_rw_base = (torch.sum(tmp2) / tmp2.shape[0]).item()
senti_rw_sample = (torch.sum(tmp3) / tmp3.shape[0]).item()
senti_rw_base = (torch.sum(tmp4) / tmp4.shape[0]).item()
return sample_y, reward_RL_sample, reward_RL_base, irony_rw_sample, irony_rw_base, senti_rw_sample, senti_rw_base
# trg
def batchPGLoss(self, src, dis, senti, non_senti, idx2word, mode):
# src [batch, 1, len ,2]
if mode == 1:
encoder = self.non_encoder
decoder = self.irony_decoder
elif mode == 0:
encoder = self.irony_encoder
decoder = self.non_decoder
output = encoder(src)
output = self.shared_encoder(output)
output = self.shared_decoder(output)
scores, sample_y = self.decode_sample(decoder, output) # [batch, len, vocab] [batch, len]
base_y = self.decode_baseline(decoder, output) # [batch, len]
with torch.no_grad():
sample_y, reward_RL_sample, reward_RL_base, irony_rw_sample, irony_rw_base, senti_rw_sample, senti_rw_base = self.get_reward(src[:,:,:,0].squeeze(1), sample_y, base_y, dis, senti, non_senti, idx2word, mode)
reward_RL_sample = reward_RL_sample.view(1, self.batch_size, 1)
reward_RL_base = reward_RL_base.view(1, self.batch_size, 1)
vocab_mask = torch.ones(self.vocab_size)
vocab_mask[0] = 0
cross_entropy_loss = nn.CrossEntropyLoss(weight=vocab_mask, size_average=False, reduce=False).cuda()
word_loss = cross_entropy_loss(scores.view(-1, scores.size(2)), sample_y.view(-1)).view(scores.size(1), scores.size(0), 1)
word_loss = -word_loss * (reward_RL_base - reward_RL_sample)
word_loss = torch.sum(word_loss)
loss = word_loss / self.batch_size
return loss, irony_rw_sample, irony_rw_base, senti_rw_sample, senti_rw_base
def decode_sample(self, decoder, output):
output, _ = decoder(output)
# bias = torch.FloatTensor(self.batch_size, self.vocab_size).fill_(0).cuda()
# for i in range(output.size(1)):
# output[:, i, :] += bias
# indices = torch.argmax(output[:, i, :], 1)
# for j in range(self.batch_size):
# idx = indices[j]
# if self.mask[idx] == 1:
# bias[j][idx] = -1e30
# del bias
s1, s2, s3 = output.size()
sample_y = torch.LongTensor(s1, s2, 1).fill_(0).cuda()
for i in range(s2):
sample_y[:, i, :] = torch.multinomial(F.softmax(output[:, i, :], dim=-1), 1)
return output, sample_y.squeeze(2)
def decode_baseline(self, decoder, output):
output, _ = decoder(output)
# bias = torch.FloatTensor(self.batch_size, self.vocab_size).fill_(0).cuda()
# for i in range(output.size(1)):
# output[:, i, :] += bias
# indices = torch.argmax(output[:, i, :], 1)
# for j in range(self.batch_size):
# idx = indices[j]
# if self.mask[idx] == 1:
# bias[j][idx] = -1e30
# del bias
base_y = torch.argmax(output, 2)
return base_y