<jupyter_start><jupyter_text>Generate handwritten digits with trained CVAE net<jupyter_code># Copyright (C) 2018 <NAME> <<EMAIL>>
import matplotlib
matplotlib.use('Agg')
%matplotlib inline
import matplotlib.pyplot as plt

import numpy as np
import tensorflow as tf
from cvae.cvae import cvae
from cvae.utils import utils
from cvae.configs import config_mnist
from cvae.configs import config_train<jupyter_output><empty_output><jupyter_text>Load mnist data<jupyter_code># Step1 load MNITST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=False, validation_size=5000)
cf = config_mnist
tr_cf = config_train<jupyter_output><empty_output><jupyter_text>Build the net<jupyter_code>net = cvae(cf)
net.cvae_build()
# print the net structure
net.cvae_print()<jupyter_output>Layer ID    Layer type    Layer shape
0 Input layer [None, 794]
1 FC layer 0 [None, 512]
2 BN layer 0 [None, 512]
3 FC layer 1 [None, 256]
4 BN layer 1 [None, 256]
5 En_mu [None, 16]
6 En_sigma [None, 16]
7 z [None, 16]
8 FC layer 1 [None, 256]
9 BN layer 1 [None, 256]
10 FC layer 0 [None, 512]
11 BN layer 0 [None, 512]
12 Output layer [None, 784]<jupyter_text>Restore the net<jupyter_code>net.get_learning_rate()
net.get_loss()
net.get_opt()

# load the net
import os
foldname = "./cvae_mnist"
name = 'cvae.pkl'
net.sess, name = utils.load_net(os.path.join(foldname, name))<jupyter_output>INFO:tensorflow:Restoring parameters from ./cvae_mnist/model_cvae.ckpt<jupyter_text>Generate new samples<jupyter_code>n_examples = 10

fig, axs = plt.subplots(cf.numclass, n_examples, figsize=(n_examples, cf.numclass))
for i in range(cf.numclass):
    z_gen = np.random.normal(0, 1, [n_examples, cf.z_length])
    # label
    labels = np.zeros([n_examples, cf.numclass])
    labels[:,i] = 1
    imgs = net.sess.run(
        net.outputs_de, 
        feed_dict = {net.z: z_gen, net.conditions: labels, 
                     net.is_training: False, 
                     net.keep_prob: 1.0})
    img_test = [np.reshape(imgs[i], [cf.rs, cf.rs]) for i in range(len(imgs))]
    for example_i in range(n_examples):
        # original
        axs[i][example_i].imshow(img_test[example_i], cmap='gray')
        axs[i][example_i].axis('off')

fig.show()
plt.draw()
# plt.savefig("fig_ce.png", bbox_inches='tight', dpi=200)<jupyter_output>/home/mzx/.local/lib/python3.5/site-packages/matplotlib/figure.py:459: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "
{'fraction_non_alphanumeric': 0.1191124951342935, 'fraction_numerical': 0.0326975476839237, 'mean_word_length': 4.635964912280702, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '489920', 'license': 'MIT', 'n_tokens_mistral': 1056, 'n_tokens_neox': 960, 'n_words': 245, 'path': 'demo-mnist/notebook-cvae-mnist-gen.ipynb', 'repo_name': 'myinxd/rg-cvae', 'size': '2581'}