|
1 | 1 | ###############################################################################
|
2 | 2 | # Language Modeling on Penn Tree Bank
|
3 | 3 | #
|
4 |
| -# With the default parameters, this should achieve ~116 perplexity on the |
5 |
| -# test set. |
| 4 | +# This file generates new sentences sampled from the language model |
| 5 | +# |
6 | 6 | ###############################################################################
|
7 | 7 |
|
8 | 8 | import argparse
|
|
38 | 38 | with open(args.checkpoint, 'rb') as f:
|
39 | 39 | model = torch.load(f)
|
40 | 40 |
|
41 |
| -# Waiting on https://github.com/pytorch/pytorch/issues/188 |
42 |
| -# if args.cuda: |
43 |
| -# model.cuda() |
44 |
| -# else: |
45 |
| -# model.cpu() |
| 41 | +if args.cuda: |
| 42 | + model.cuda() |
| 43 | +else: |
| 44 | + model.cpu() |
46 | 45 |
|
47 | 46 | corpus = data.Corpus(args.data)
|
48 | 47 | ntokens = corpus.dic.ntokens()
|
49 | 48 |
|
50 | 49 | hidden = model.initHidden(1)
|
51 | 50 |
|
52 |
| -input = torch.LongTensor(1,1).fill_(math.floor(torch.rand(1)[0] * ntokens)) |
| 51 | +input = torch.LongTensor(1,1).fill_(int(math.floor(torch.rand(1)[0] * ntokens))) |
53 | 52 | if args.cuda:
|
54 | 53 | input = input.cuda()
|
55 | 54 |
|
56 | 55 | temperature = max(args.temperature, 1e-3)
|
57 | 56 | with open(args.outf, 'w') as outf:
|
58 | 57 | for i in range(args.nwords):
|
59 | 58 |
|
60 |
| - output, hidden = model(Variable(input, requires_grad=False), hidden) |
61 |
| - gen = torch.multinomial(output[0].data.cpu().div(temperature).exp(), 1)[0][0] # FIXME: no multinomial on GPU? |
| 59 | + output, hidden = model(Variable(input, volatile=True), hidden) |
| 60 | + gen = torch.multinomial(output[0].data.div(temperature).exp().cpu(), 1)[0][0] # FIXME: multinomial is only for CPU |
62 | 61 | input.fill_(gen)
|
63 | 62 | word = corpus.dic.idx2word[gen]
|
64 | 63 | outf.write(word)
|
|
0 commit comments