-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdecoders.py
60 lines (49 loc) · 2.04 KB
/
decoders.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
import torch
import model_utils
import torch.nn as nn
import torch.nn.functional as F
class bag_of_words(nn.Module):
def __init__(self, ysize, zsize, mlp_layer, hidden_size,
vocab_size, dropout, *args, **kwargs):
super(bag_of_words, self).__init__()
self.hid2vocab = model_utils.get_mlp(
ysize + zsize,
hidden_size,
vocab_size,
mlp_layer,
dropout)
def forward(self, yvecs, zvecs, tgts, tgts_mask):
input_vecs = torch.cat([yvecs, zvecs], -1)
logits = F.log_softmax(self.hid2vocab(input_vecs), -1)
return -(torch.sum(logits * tgts, 1) / tgts.sum(1)).mean()
class lstm(nn.Module):
def __init__(self, ysize, zsize, vocab_size, mlp_hidden_size,
mlp_layer, hidden_size, dropout,
*args, **kwargs):
super(lstm, self).__init__()
self.cell = nn.LSTM(
zsize, hidden_size,
bidirectional=False, batch_first=True)
self.hid2vocab = model_utils.get_mlp(
hidden_size + ysize,
hidden_size,
vocab_size,
mlp_layer,
dropout)
def forward(self, yvecs, zvecs, tgts, tgts_mask,
*args, **kwargs):
bs, sl = tgts_mask.size()
ex_input_vecs = zvecs.unsqueeze(1).expand(-1, sl, -1)
ex_output_vecs = yvecs.unsqueeze(1).expand(-1, sl, -1)
ori_output_seq, _ = model_utils.get_rnn_vecs(
ex_input_vecs, tgts_mask, self.cell)
output_seq = torch.cat([ori_output_seq, ex_output_vecs], -1)
# batch size x seq len x vocab size
pred = self.hid2vocab(output_seq)[:, :-1, :]
batch_size, seq_len, vocab_size = pred.size()
pred = pred.contiguous().view(batch_size * seq_len, vocab_size)
logloss = F.cross_entropy(
pred, tgts[:, 1:].contiguous().view(-1).long(), reduce=False)
logloss = (logloss.view(batch_size, seq_len) *
tgts_mask[:, 1:]).sum(-1) / tgts_mask[:, 1:].sum(-1)
return logloss.mean()