-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
model.py
197 lines (145 loc) · 7.38 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
import torch
import torch.nn as nn
import torch.nn.utils.rnn as rnn_utils
from utils import to_var
class SentenceVAE(nn.Module):
def __init__(self, vocab_size, embedding_size, rnn_type, hidden_size, word_dropout, embedding_dropout, latent_size,
sos_idx, eos_idx, pad_idx, unk_idx, max_sequence_length, num_layers=1, bidirectional=False):
super().__init__()
self.tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.Tensor
self.max_sequence_length = max_sequence_length
self.sos_idx = sos_idx
self.eos_idx = eos_idx
self.pad_idx = pad_idx
self.unk_idx = unk_idx
self.latent_size = latent_size
self.rnn_type = rnn_type
self.bidirectional = bidirectional
self.num_layers = num_layers
self.hidden_size = hidden_size
self.embedding = nn.Embedding(vocab_size, embedding_size)
self.word_dropout_rate = word_dropout
self.embedding_dropout = nn.Dropout(p=embedding_dropout)
if rnn_type == 'rnn':
rnn = nn.RNN
elif rnn_type == 'gru':
rnn = nn.GRU
# elif rnn_type == 'lstm':
# rnn = nn.LSTM
else:
raise ValueError()
self.encoder_rnn = rnn(embedding_size, hidden_size, num_layers=num_layers, bidirectional=self.bidirectional,
batch_first=True)
self.decoder_rnn = rnn(embedding_size, hidden_size, num_layers=num_layers, bidirectional=self.bidirectional,
batch_first=True)
self.hidden_factor = (2 if bidirectional else 1) * num_layers
self.hidden2mean = nn.Linear(hidden_size * self.hidden_factor, latent_size)
self.hidden2logv = nn.Linear(hidden_size * self.hidden_factor, latent_size)
self.latent2hidden = nn.Linear(latent_size, hidden_size * self.hidden_factor)
self.outputs2vocab = nn.Linear(hidden_size * (2 if bidirectional else 1), vocab_size)
def forward(self, input_sequence, length):
batch_size = input_sequence.size(0)
sorted_lengths, sorted_idx = torch.sort(length, descending=True)
input_sequence = input_sequence[sorted_idx]
# ENCODER
input_embedding = self.embedding(input_sequence)
packed_input = rnn_utils.pack_padded_sequence(input_embedding, sorted_lengths.data.tolist(), batch_first=True)
_, hidden = self.encoder_rnn(packed_input)
if self.bidirectional or self.num_layers > 1:
# flatten hidden state
hidden = hidden.view(batch_size, self.hidden_size*self.hidden_factor)
else:
hidden = hidden.squeeze()
# REPARAMETERIZATION
mean = self.hidden2mean(hidden)
logv = self.hidden2logv(hidden)
std = torch.exp(0.5 * logv)
z = to_var(torch.randn([batch_size, self.latent_size]))
z = z * std + mean
# DECODER
hidden = self.latent2hidden(z)
if self.bidirectional or self.num_layers > 1:
# unflatten hidden state
hidden = hidden.view(self.hidden_factor, batch_size, self.hidden_size)
else:
hidden = hidden.unsqueeze(0)
# decoder input
if self.word_dropout_rate > 0:
# randomly replace decoder input with <unk>
prob = torch.rand(input_sequence.size())
if torch.cuda.is_available():
prob=prob.cuda()
prob[(input_sequence.data - self.sos_idx) * (input_sequence.data - self.pad_idx) == 0] = 1
decoder_input_sequence = input_sequence.clone()
decoder_input_sequence[prob < self.word_dropout_rate] = self.unk_idx
input_embedding = self.embedding(decoder_input_sequence)
input_embedding = self.embedding_dropout(input_embedding)
packed_input = rnn_utils.pack_padded_sequence(input_embedding, sorted_lengths.data.tolist(), batch_first=True)
# decoder forward pass
outputs, _ = self.decoder_rnn(packed_input, hidden)
# process outputs
padded_outputs = rnn_utils.pad_packed_sequence(outputs, batch_first=True)[0]
padded_outputs = padded_outputs.contiguous()
_,reversed_idx = torch.sort(sorted_idx)
padded_outputs = padded_outputs[reversed_idx]
b,s,_ = padded_outputs.size()
# project outputs to vocab
logp = nn.functional.log_softmax(self.outputs2vocab(padded_outputs.view(-1, padded_outputs.size(2))), dim=-1)
logp = logp.view(b, s, self.embedding.num_embeddings)
return logp, mean, logv, z
def inference(self, n=4, z=None):
if z is None:
batch_size = n
z = to_var(torch.randn([batch_size, self.latent_size]))
else:
batch_size = z.size(0)
hidden = self.latent2hidden(z)
if self.bidirectional or self.num_layers > 1:
# unflatten hidden state
hidden = hidden.view(self.hidden_factor, batch_size, self.hidden_size)
hidden = hidden.unsqueeze(0)
# required for dynamic stopping of sentence generation
sequence_idx = torch.arange(0, batch_size, out=self.tensor()).long() # all idx of batch
# all idx of batch which are still generating
sequence_running = torch.arange(0, batch_size, out=self.tensor()).long()
sequence_mask = torch.ones(batch_size, out=self.tensor()).bool()
# idx of still generating sequences with respect to current loop
running_seqs = torch.arange(0, batch_size, out=self.tensor()).long()
generations = self.tensor(batch_size, self.max_sequence_length).fill_(self.pad_idx).long()
t = 0
while t < self.max_sequence_length and len(running_seqs) > 0:
if t == 0:
input_sequence = to_var(torch.Tensor(batch_size).fill_(self.sos_idx).long())
input_sequence = input_sequence.unsqueeze(1)
input_embedding = self.embedding(input_sequence)
output, hidden = self.decoder_rnn(input_embedding, hidden)
logits = self.outputs2vocab(output)
input_sequence = self._sample(logits)
# save next input
generations = self._save_sample(generations, input_sequence, sequence_running, t)
# update gloabl running sequence
sequence_mask[sequence_running] = (input_sequence != self.eos_idx)
sequence_running = sequence_idx.masked_select(sequence_mask)
# update local running sequences
running_mask = (input_sequence != self.eos_idx).data
running_seqs = running_seqs.masked_select(running_mask)
# prune input and hidden state according to local update
if len(running_seqs) > 0:
input_sequence = input_sequence[running_seqs]
hidden = hidden[:, running_seqs]
running_seqs = torch.arange(0, len(running_seqs), out=self.tensor()).long()
t += 1
return generations, z
def _sample(self, dist, mode='greedy'):
if mode == 'greedy':
_, sample = torch.topk(dist, 1, dim=-1)
sample = sample.reshape(-1)
return sample
def _save_sample(self, save_to, sample, running_seqs, t):
# select only still running
running_latest = save_to[running_seqs]
# update token at position t
running_latest[:,t] = sample.data
# save back
save_to[running_seqs] = running_latest
return save_to