forked from frsong/tf-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchar_rnn_test.py
More file actions
56 lines (41 loc) · 1.5 KB
/
char_rnn_test.py
File metadata and controls
56 lines (41 loc) · 1.5 KB
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
"""
Simple char-rnn based on
https://github.com/sherjilozair/char-rnn-tensorflow
Original article:
http://karpathy.github.io/2015/05/21/rnn-effectiveness/
"""
import os
import pickle
import tensorflow as tf
from char_rnn_model import Model
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('save_dir', 'save/char-rnn', "save directory")
tf.app.flags.DEFINE_string('start_text', " ", "start text")
tf.app.flags.DEFINE_integer('num_chars', 500, "number of characters to sample")
tf.app.flags.DEFINE_integer('seed', 0, "random number generator seed")
def sample():
# Load characters
filename = os.path.join(FLAGS.save_dir, 'chars_vocab.pkl')
with open(filename, 'rb') as f:
chars, vocab = pickle.load(f)
vocab_size = len(chars)
# Model
model = Model(vocab_size)
# Saver
saver = tf.train.Saver(tf.global_variables())
with tf.Session() as sess:
# Load model
ckpt = tf.train.get_checkpoint_state(FLAGS.save_dir)
if ckpt and ckpt.model_checkpoint_path:
print("Loading", ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path)
else:
raise Exception("No checkpoint available.")
# Generate sample
return model.sample(sess, chars, vocab,
FLAGS.start_text, FLAGS.num_chars, FLAGS.seed)
#///////////////////////////////////////////////////////////////////////////////
def main(_):
print(sample())
if __name__ == '__main__':
tf.app.run()