forked from codekansas/seqgan-text-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
executable file
·60 lines (46 loc) · 1.91 KB
/
sample.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
#!/usr/bin/env python
"""Samples from a trained model.
usage: sample.py [-h] [-t DICTIONARY] [-d LOGDIR] [-c] N
Sample from a trained SeqGAN model.
positional arguments:
N length of sample to generate
optional arguments:
-h, --help show this help message and exit
-t DICTIONARY, --dictionary DICTIONARY
path to dictionary file
-d LOGDIR, --logdir LOGDIR
directory of the trained model
-c, --only_cpu if set, only build weights on cpu
"""
from __future__ import print_function
import utils
from model import SeqGAN
import argparse
import os
import tensorflow as tf
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Sample from a trained SeqGAN model.')
parser.add_argument('sample_len', metavar='N', type=int,
help='length of sample to generate')
parser.add_argument('-t', '--dictionary', default='dictionary.pkl',
type=str, help='path to dictionary file')
parser.add_argument('-d', '--logdir', default='model/', type=str,
help='directory of the trained model')
parser.add_argument('-c', '--only_cpu', default=True, action='store_true',
help='if set, only build weights on cpu')
args = parser.parse_args()
if not os.path.exists(args.dictionary):
raise ValueError('No dictionary file found: "%s". To build it, '
'run train.py' % args.dictionary)
_, rev_dict = utils.get_dictionary(None, dfile=args.dictionary)
num_classes = len(rev_dict)
sess = tf.Session()
model = SeqGAN(sess,
num_classes,
logdir=args.logdir,
only_cpu=args.only_cpu)
model.build()
model.load(ignore_missing=True)
g = model.generate(args.sample_len)
print('Generated text:', utils.detokenize(g, rev_dict))