<jupyter_start><jupyter_text>Train a rescvae for the handwritten digits simulation<jupyter_code># Copyright (C) 2018 <NAME> <<EMAIL>>
import matplotlib
matplotlib.use('Agg')
%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
from rescvae.rescvae import rescvae
from rescvae.utils import utils
from rescvae.configs import config_mnist
from rescvae.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=True, validation_size=5000)
cf = config_mnist
tr_cf = config_train<jupyter_output><empty_output><jupyter_text>Build the net and train<jupyter_code>net = rescvae(cf)
net.cvae_build()
net.net_print()
# train the net
net.cvae_train_mnist(mnist=mnist, train_configs=tr_cf)<jupyter_output>[2018-11-27: 21:03:46]: Epochs    Trn_loss_recon    Val_loss_recon    Trn_loss_latent    Val_loss_latent
[2018-11-27: 21:04:04]:      0         179.77888         159.67807           14.03365            5.72141
[2018-11-27: 21:04:20]:      1         148.00918         141.92128            3.34084            2.86715
[2018-11-27: 21:04:37]:      2         136.28592         130.87835            1.81385            1.43260
[2018-11-27: 21:04:53]:      3         127.01765         122.70629            1.10188            0.73859
[2018-11-27: 21:05:09]:      4         118.81099         114.61035            0.67470            0.40714
[2018-11-27: 21:05:25]:      5         111.00614         106.98079            0.48631            0.35588
[2018-11-27: 21:05:41]:      6         104.22800         100.12917            0.47662            0.41641
[2018-11-27: 21:05:58]:      7          97.90905          93.50042            0.51720            0.46186
[2018-11-27: 21:06:15]:      8          91.94878       [...]<jupyter_text>Test the net<jupyter_code>n_examples = 10
img_test, label_test = mnist.test.next_batch(n_examples)
label_test_input = utils.condition_reshape(
    label=label_test, numclass=10, imgshape=(28,28))
img_est = net.sess.run(
    net.output_flatten_de, 
    feed_dict={net.inputs: img_test.reshape(-1,cf.rs,cf.rs,1), 
               net.outputs: img_test.reshape(-1,cf.rs,cf.rs,1),
               net.conditions: label_test,
               net.conditions_input: label_test_input,
               net.is_training: False,
               net.keep_prob:1.0
               })
import numpy as np
fig, axs = plt.subplots(2, n_examples, figsize=(n_examples, 2))
for example_i in range(n_examples):
    # original
    axs[0][example_i].imshow(np.reshape(img_test[example_i, :], (28, 28)), cmap='gray')
    axs[0][example_i].axis('off')
    # reconstructed
    axs[1][example_i].imshow(np.reshape(img_est[example_i, :], (28, 28)), cmap='gray')
    axs[1][example_i].axis('off')

fig.show()
plt.draw()<jupyter_output>/home/mzx/.local/lib/python3.6/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using module://ipykernel.pylab.backend_inline, which is a non-GUI backend, so cannot show the figure.
  % get_backend())<jupyter_text>Save the net<jupyter_code>import os
foldname = "./rescvae_mnist"
name = "rescvae.pkl"
netname = "model_rescvae.ckpt"
namepath = os.path.join(foldname, name)
netpath = os.path.join(foldname, netname)
savedict = {'netpath': netpath}
if os.path.exists(foldname):
    os.system("rm -r %s" % (foldname))
os.mkdir(foldname)
utils.save_net(sess=net.sess,
               namepath=namepath,
               netpath=netpath,
               savedict=savedict)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup