in code/src/model/transformer.py [0:0]
def generate(self, encoded, attr, max_len=200, sample=False, temperature=None):
"""
Generate a sentence from a given initial state.
Input:
- FloatTensor of size (batch_size, hidden_dim) representing
sentences encoded in the latent space
Output:
- LongTensor of size (seq_len, batch_size), word indices
- LongTensor of size (batch_size,), sentence x_len
"""
if self.beam_size > 0:
return self.generate_beam(encoded, attr, self.beam_size, max_len, sample, temperature)
encoder_out = encoded.dec_input
latent = encoder_out['encoder_out']
x_len = encoded.input_len
is_cuda = latent.is_cuda
one_hot = None
# check inputs
assert latent.size() == (x_len.max(), x_len.size(0), self.emb_dim)
assert attr.size() == (x_len.size(0), len(self.attributes))
assert (sample is True) ^ (temperature is None)
# initialize generated sentences batch
slen, bs = latent.size(0), latent.size(1)
assert x_len.max() == slen and x_len.size(0) == bs
cur_len = 1
decoded = torch.LongTensor(max_len, bs).fill_(self.pad_index)
unfinished_sents = torch.LongTensor(bs).fill_(1)
lengths = torch.LongTensor(bs).fill_(1)
if is_cuda:
decoded = decoded.cuda()
unfinished_sents = unfinished_sents.cuda()
lengths = lengths.cuda()
decoded[0] = self.bos_index
incremental_state = {}
while cur_len < max_len:
# previous word embeddings
scores = self.forward(encoded, decoded[:cur_len], attr, one_hot, incremental_state)
scores = scores.data[-1, :, :] # T x B x V -> B x V
# select next words: sample or one-hot
if sample:
next_words = torch.multinomial(F.softmax(scores / temperature, dim=1), 1).squeeze(1)
else:
next_words = torch.topk(scores, 1)[1].squeeze(1)
assert next_words.size() == (bs,)
decoded[cur_len] = next_words * unfinished_sents + self.pad_index * (1 - unfinished_sents)
lengths.add_(unfinished_sents)
unfinished_sents.mul_(next_words.ne(self.eos_index).long())
cur_len += 1
# stop when there is a </s> in each sentence
if unfinished_sents.max() == 0:
break
if cur_len == max_len:
decoded[max_len - 1].masked_fill_(unfinished_sents.byte(), self.eos_index)
assert (decoded == self.eos_index).sum() == bs
return decoded[:cur_len], lengths, one_hot