This repository has been archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsample.py
49 lines (37 loc) · 1.4 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
# -*- coding: utf-8 -*-
from __future__ import print_function
import tensorflow as tf
import numpy as np
import copy
import cPickle
import sys
import os
from models.charrnn import CharRNN
from utils import TextLoader, normalize_unicodes, UNK_ID
def main(_):
if len(sys.argv) < 2:
print("Please enter a prime")
sys.exit()
prime = sys.argv[1]
prime = prime.decode('utf-8')
with open("./log/hyperparams.pkl", 'rb') as f:
config = cPickle.load(f)
if not os.path.exists(config['checkpoint_dir']):
print(" [*] Creating checkpoint directory...")
os.makedirs(config['checkpoint_dir'])
data_loader = TextLoader(os.path.join(config['data_dir'], config['dataset_name']),
config['batch_size'], config['seq_length'])
vocab_size = data_loader.vocab_size
with tf.variable_scope('model'):
model = CharRNN(vocab_size, 1, config['rnn_size'],
config['layer_depth'], config['num_units'],
1, config['keep_prob'],
config['grad_clip'],
is_training=False)
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(config['checkpoint_dir'] + '/' + config['dataset_name'])
tf.train.Saver().restore(sess, ckpt.model_checkpoint_path)
res = model.sample(sess, data_loader.chars, data_loader.vocab, UNK_ID, 100, prime)
print(res)
if __name__ == '__main__':
tf.app.run()