-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsampling.py
61 lines (46 loc) · 1.73 KB
/
sampling.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
61
import sys
sys.path.append("model")
sys.path.append("utils")
import numpy as np
import tensorflow as tf
from train import log_and_print
from vae import VAE
from config import FLAGS
from batchloader import BatchLoader
LOG_DIR = "log/LOG_DIR"
MODEL_DIR = LOG_DIR + "/model"
SAVE_FILE = LOG_DIR + "/sampled_texts.txt"
SAMPLE_NUM = 128
def sampling():
batchloader = BatchLoader()
# gpu memory
sess_conf = tf.ConfigProto(
gpu_options = tf.GPUOptions(
# allow_growth = True,
)
)
with tf.Graph().as_default():
with tf.Session(config=sess_conf) as sess:
with tf.variable_scope("VAE"):
vae_restored = VAE(batchloader, is_training=False, ru=False)
saver = tf.train.Saver()
saver.restore(sess, MODEL_DIR + "/model80.ckpt")
itr = SAMPLE_NUM // FLAGS.BATCH_SIZE
res = SAMPLE_NUM - itr * FLAGS.BATCH_SIZE
# random output
generated_texts = []
for i in range(itr+1):
z = np.random.normal(loc=0.0, scale=1.0,
size=[FLAGS.BATCH_SIZE, FLAGS.LATENT_VARIABLE_SIZE])
sample_logits = sess.run(vae_restored.rnn_logits,
feed_dict = {vae_restored.latent_variables: z})
if i==itr:
sample_num = res
else:
sample_num = FLAGS.BATCH_SIZE
sample_texts = batchloader.logits2str(logits=sample_logits, sample_num=sample_num)
generated_texts.extend(sample_texts)
for i in range(SAMPLE_NUM):
log_and_print(SAVE_FILE, generated_texts[i])
if __name__ == "__main__":
sampling()