-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer_style.py
355 lines (289 loc) · 12.5 KB
/
transfer_style.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
import argparse
import codecs
import sys
import torch
import torch.nn as nn
from torch.autograd import Variable
import data
import model
from nltk import word_tokenize
import numpy as np
#parser = argparse.ArgumentParser(description='Language style transfer Beam search')
#parser.add_argument('--seed', type=int, default=1111,
# help='random seed')
#parser.add_argument('--cuda', action='store_true',
# help='use CUDA')
#parser.add_argument('--data', type=str, default='/pio/data/data/mikolov_simple_examples/data/ptb.',
# help='location of the data corpus')
#parser.add_argument('--beam-size', type=int, default=10,
# help='beam size for beam search')
#parser.add_argument('--n-best', type=int, default=10,
# help='N best paths to be returned')
#parser.add_argument('--seed-text', type=str, default='',
# help='text passed to the initializer')
#parser.add_argument('--model', type=str, default='./',
# help='path to model dir containing model.pt and model.info')
#args = parser.parse_args()
#if args.seed_text_generator:
# args.seed_text = eval(args.seed_text_generator)
#
## Set the random seed manually for reproducibility.
#torch.manual_seed(args.seed)
#if torch.cuda.is_available():
# if not args.cuda:
# print("WARNING: You have a CUDA device, so you should probably "
# "run with --cuda")
# else:
# torch.cuda.manual_seed(args.seed)
def _set_word_prob(probs, idx, probable=True):
if probable:
probs.fill_(0.0)
probs[:,idx] = 1.0
else:
probs[:,idx] = 0.0
probs.div_(probs.sum(1, keepdim=True)) # Re-normalize
return probs
# Based on OpenNMT-py (MIT license)
# https://github.com/OpenNMT/OpenNMT-py/blob/master/onmt/Beam.py
class Beam(object):
def __init__(self, size, dictionary, rnn_input, n_best=1, constraints=[],
cuda=False):
self.size = size
self.dictionary = dictionary
self.tt = torch.cuda if cuda else torch
# The score for each translation on the beam.
self.scores = self.tt.FloatTensor(size).zero_()
self.allScores = []
# The backpointers at each time-step.
# Indexes of beams in {1,..,size}
self.prevKs = []
# Has EOS topped the beam yet.
self._eos = self.dictionary.bos_id
self.eosTop = False
# The outputs at each time-step.
# Indexes of words in {1,...,vocab_size}
self.nextYs = [rnn_input.data.squeeze()]
# Time and k pair for finished.
self.finished = []
self.n_best = n_best
self.constraints = constraints
def getCurrentState(self):
"Get the outputs for the current timestep."
return self.nextYs[-1]
def getCurrentOrigin(self):
"Get the backpointers for the current timestep."
return self.prevKs[-1]
def advance(self, wordLk): #, attnOut):
"""
Given prob over words for every last beam `wordLk` and attention
`attnOut`: Compute and update the beam search.
Parameters:
* `wordLk`- probs of advancing from the last step (K x words)
* `attnOut`- attention at the last step
Returns: True if beam search is complete.
"""
numWords = wordLk.size(1)
pos = len(self.prevKs)
for constraint in self.constraints:
wordLk = constraint.apply(wordLk, pos, self.dictionary)
# Sum the previous scores.
if len(self.prevKs) > 0:
beamLk = wordLk + self.scores.unsqueeze(1).expand_as(wordLk)
# Don't let EOS have children.
for i in range(self.nextYs[-1].size(0)):
if self.nextYs[-1][i] == self._eos:
beamLk[i] = -1e20
else:
beamLk = wordLk[0]
flatBeamLk = beamLk.view(-1)
bestScores, bestScoresId = flatBeamLk.topk(self.size, 0, True, True)
self.allScores.append(self.scores)
self.scores = bestScores
# bestScoresId is flattened beam x word array, so calculate which
# word and beam each score came from
prevK = bestScoresId / numWords
self.prevKs.append(prevK)
self.nextYs.append((bestScoresId - prevK * numWords))
# self.attn.append(attnOut.index_select(0, prevK))
for i in range(self.nextYs[-1].size(0)):
if self.nextYs[-1][i] == self._eos:
s = self.scores[i]
self.finished.append((s, len(self.nextYs) - 1, i))
# End condition is when top-of-beam is EOS and no global score.
if self.nextYs[-1][0] == self._eos:
# self.allScores.append(self.scores)
self.eosTop = True
def done(self):
return self.eosTop and len(self.finished) >= self.n_best
def sortFinished(self, minimum=None):
if minimum is not None:
i = 0
# Add from beam until we have minimum outputs.
while len(self.finished) < minimum:
s = self.scores[i]
self.finished.append((s, len(self.nextYs) - 1, i))
self.finished.sort(key=lambda a: -a[0])
scores = [sc for sc, _, _ in self.finished]
ks = [(t, k) for _, t, k in self.finished]
return scores, ks
def getHyp(self, timestep, k):
"""
Walk back to construct the full hypothesis.
"""
hyp = []
for j in range(len(self.prevKs[:timestep]) - 1, -1, -1):
hyp.append(self.nextYs[j+1][k])
k = self.prevKs[j][k]
return hyp[::-1]
class TransferStyle(object):
"""Samples from the model."""
def __init__(self, dictionary, model, beam_size=10, n_best=10, cuda=False):
self.dictionary = dictionary
self.model = model
self.beam_size = beam_size
self.n_best = n_best
self.tt = torch.cuda if cuda else torch
def __call__(self, data, in_style, out_style, seq_lens=None):
in_style = in_style if isinstance(in_style, list) else [in_style]
in_style = self.model.style_encoder(
Variable(self.tt.LongTensor(in_style)))
out_style = out_style if isinstance(out_style, list) else [out_style]
out_style = self.model.style_encoder(
Variable(self.tt.LongTensor(out_style)))
if not isinstance(data, Variable):
data = data if isinstance(data, list) else [data]
data, seq_lens = self._preprocess(data)
assert(seq_lens is not None)
return self._transfer(data, in_style, out_style, seq_lens)
def _preprocess(self, sentences):
data = []
unk_id = self.dictionary.unk_id
for sent in sentences:
tokens = word_tokenize(sent)
data.append(np.array([
self.dictionary.word2idx[token]
if token in self.dictionary.word2idx
else unk_id
for token in tokens]))
seq_lens = np.array([sent.shape[0]+2 for sent in data])
seq_lens_idx = np.argsort(-seq_lens)
seq_lens = seq_lens[seq_lens_idx]
padded_data = np.zeros(
(len(data), seq_lens[0]), dtype='int64')
padded_data = padded_data + self.dictionary.eos_id
for i, sent in enumerate(data):
padded_data[i, 1:sent.shape[0]+1] = sent
padded_data[:, 0] = self.dictionary.bos_id
padded_data = padded_data[seq_lens_idx]
batch = self.tt.from_numpy(padded_data.T).contiguous()
batch = Variable(batch)
seq_lens = Variable(self.tt.from_numpy(seq_lens))
return batch, seq_lens
def _transfer(self, data, in_style, out_style, seq_lens, max_sent_len=80):
encoder_init = self.model.encoder.split_initial_hidden(
self.model.encoder_init_projection(in_style))
# emb = self.model.encoder.embeddings(data)
# encoder_output, _ = self.model.encoder.rnn(emb, encoder_init)
# content = encoder_output[-1]
content = self.model.encoder(encoder_init, data, seq_lens)
generator_init = self.model.generator_init_projection(
torch.cat([out_style, content], 1))
generator_init = self.model.generator.split_initial_hidden(
generator_init)
hidden = generator_init
batch_size = data.size(1)
bos = data.data.new([[self.dictionary.bos_id]*self.beam_size])
beams = [Beam(self.beam_size, self.dictionary,
Variable(bos.clone(), volatile=True),
n_best=self.n_best,
cuda=data.is_cuda)
for _ in range(batch_size)]
for i in range(max_sent_len):
if all((b.done() for b in beams)):
break
# Construct batch x beam_size nxt words.
# Get all the pending current beam words and arrange for forward.
input = Variable(torch.stack([b.getCurrentState() for b in beams])
.t().contiguous().view(1, -1), volatile=True)
# Run one step.
output, _, hidden = self.model.generator(hidden, input)
output = output.squeeze(0)
# output: beam x rnn_size
# Compute a vector of batch*beam word scores.
out = self.model.generator.softmax(
output.div(self.model.generator.gamma)).data
out = out.view(self.beam_size, batch_size, -1)
# Advance each beam
for idx, b in enumerate(beams):
b.advance(out[:, idx])
# LSTM passes hiddens as tuples, others do not
def iter_hidden(hidden):
hidden = (hidden,) if not type(hidden) is tuple else hidden
for h in hidden:
yield h
for h in iter_hidden(hidden):
# Each h is of size (num_layers, beam_size * batch_size, nhid)
a, br, d = h.size()
positions = b.getCurrentOrigin()
sentStates = h.view(a, self.beam_size, br // self.beam_size, d)[:, :, idx]
sentStates.data.copy_(
sentStates.data.index_select(1, positions))
ret = []
for b in beams:
scores, ks = b.sortFinished(minimum=self.n_best)
ret.append([])
for i, (times, k) in enumerate(ks[:self.n_best]):
hyp = b.getHyp(times, k)
ret[-1].append([self.dictionary.idx2word[i] for i in hyp])
return ret
def main():
dictionary = data.Corpus(
args.data, cuda=args.cuda, yield_sentences=True, rng=None).dictionary
if False:
# Old way of loading a model
with open(args.model, 'rb') as f:
mdl = torch.load(f)
print(mdl)
else:
mdl = model.load(args.model)
mdl.softmax = nn.Softmax()
mdl = mdl.cuda() if args.cuda else mdl.cpu()
mdl.eval()
sampler = Sampler(dictionary, mdl)
seed_texts = []
if args.seed_text != '':
seed_texts += [args.seed_text]
if args.seed_file:
with codecs.open(args.seed_file, 'r', 'utf-8') as f:
seed_texts += [line.strip() for line in f]
if seed_texts == []:
seed_texts += ['']
for seed_text in seed_texts:
if args.print_seed_text:
print(seed_text)
if not args.seed_without_eos:
seed_text = '<eos> ' + seed_text + ' <eos>'
constraints = eval(args.constraint_list)
tokenizer_fn = lambda s: dictionary.words_to_ids(
data.tokenize(s, add_bos=False, add_eos=False), cuda=args.cuda)
for c in constraints:
if type(c) is SeedTextDictConstraint:
c.set_seed_text(seed_text, tokenizer_fn, dictionary)
if args.num_words:
# Generate N words
out_file = sys.stdout
out_string = sampler.string(seed_text, args.prefix_text,
args.num_words, constraints=constraints)
for i, word in enumerate(out_string):
out_file.write(word + ('\n' if i % 20 == 19 else ' '))
if i % 20 != 19:
print('')
else:
# Beam search on sentences
for batch in sampler.sentences(seed_text, args.prefix_text,
constraints=constraints):
for sent_tokens in batch:
#print( "len = %d, %s" % (len(sent_tokens), ' '.join(sent_tokens)) )
print(' '.join(sent_tokens))
if __name__ == '__main__':
main()