<jupyter_start><jupyter_text>Train a dnnae for 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 dnnae.dnn import dnn
from dnnae.utils import utils
from dnnae.configs import config_mnist_bn
from dnnae.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_bn
tr_cf = config_train<jupyter_output><empty_output><jupyter_text>Build the net and train<jupyter_code>net = dnn(cf)
net.dnn_build()
# print the net structure
net.dnn_print()
# train the net
net.dnn_train_mnist_cmb(mnist=mnist, train_configs=tr_cf)<jupyter_output>[2018-03-29: 21:09:59]: Epochs    Trn_loss_mse    Val_loss_mse    Trn_loss_ce    Val_loss_ce    Trn_acc    Val_acc
[2018-03-29: 21:10:03]: 0    0.09327182    0.06087573    0.63936099    0.81755777    0.8162    0.8876
[2018-03-29: 21:10:22]: 5    0.02536053    0.02362961    0.11718460    0.11021183    0.9679    0.9678
[2018-03-29: 21:10:42]: 10    0.01803466    0.01691318    0.07674887    0.08720252    0.9790    0.9736
[2018-03-29: 21:11:01]: 15    0.01420287    0.01288835    0.06028467    0.07941795    0.9835    0.9766
[2018-03-29: 21:11:21]: 20    0.01226631    0.01090664    0.04678404    0.07463818    0.9881    0.9776
[2018-03-29: 21:11:40]: 25    0.01107594    0.00970362    0.03774372    0.07588881    0.9900    0.9766
[2018-03-29: 21:12:00]: 30    0.01026935    0.00899743    0.03066264    0.07746560    0.9926    0.9772
[2018-03-29: 21:12:20]: 35    0.00970752    0.00846974    0.02615600    0.08132840    0.9933    0.9758
[2018-03-29: 21:12:39]: 40    0.00931584    0.00807546    0.022[...]<jupyter_text>Test the net<jupyter_code>n_examples = 10
img_test, _ = mnist.test.next_batch(n_examples)
img_est = net.sess.run(
    net.outputs_de, 
    feed_dict={net.inputs: img_test, 
               net.outputs: img_test,
               net.is_training: False
               })
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.5/site-packages/matplotlib/figure.py:403: UserWarning: matplotlib is currently using a non-GUI backend, so cannot show the figure
  "matplotlib is currently using a non-GUI backend, "<jupyter_text>Save the net<jupyter_code>import os
foldname = "./dnnae_mnist"
name = "dnnae.pkl"
netname = "model_dnnae.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>
{'fraction_non_alphanumeric': 0.12556458897922312, 'fraction_numerical': 0.17344173441734417, 'mean_word_length': 3.6722925457102673, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '89696', 'license': 'MIT', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1354, 'n_words': 254, 'path': 'demo-mnist/notebook-dnnae-mnist.ipynb', 'repo_name': 'myinxd/dnnae-gmm', 'size': '3333'}