<jupyter_start><jupyter_text>Import tensorflow, and reset graph<jupyter_code>#
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('datasets\mnist')
#
tf.reset_default_graph()
#
n_inputs = 28 * 28
n_hidden1 = 300
n_hidden2 = 150  # codings
n_hidden3 = n_hidden1
n_outputs = n_inputs

learning_rate = 0.01<jupyter_output><empty_output><jupyter_text>Add noise to data<jupyter_code>X = tf.placeholder(tf.float32, shape=[None, n_inputs])
training = tf.placeholder_with_default(False, shape=(), name='training')
# Add gaussin noise
noise_level = 1.0
X_noisy = X + noise_level * tf.random_normal(tf.shape(X))


# use drop out
# drop_rate = 0.3
# X_noisy = tf.layers.dropout(X, drop_rate, training=training)<jupyter_output><empty_output><jupyter_text>Version 1: no dropout<jupyter_code>with tf.name_scope('nn'):
    hidden1 = tf.layers.dense(X_noisy, n_hidden1, activation=tf.nn.relu,
                              name="hidden1")
    hidden2 = tf.layers.dense(hidden1, n_hidden2, activation=tf.nn.relu, 
                              name="hidden2")                       
    hidden3 = tf.layers.dense(hidden2, n_hidden3, activation=tf.nn.relu,
                              name="hidden3")                           
    outputs = tf.layers.dense(hidden3, n_outputs, name="outputs") 

with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.square(outputs - X))
with tf.name_scope('train'):
    optimizer = tf.train.AdamOptimizer()
    training_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
n_epoches = 20
batch_size = 50
n_step = mnist.train.num_examples // batch_size
with tf.Session() as sess:
    sess.run(init)
    for epoch in range(n_epoches):
        for step in range(n_step):
            X_batch, y_batch = mnist.train.next_batch(batch_size)
            sess.run(training_op, feed_dict={X: X_batch, training: True})
        loss_train = loss.eval(feed_dict={X: X_batch})
        print("Epoch: {}".format(epoch), "Train MSE:", loss_train)
        saver.save(sess, "tmp/my_model_stacked_denoising.ckpt")
with tf.Session() as sess:
    saver.restore(sess, 'tmp/my_model_stacked_denoising.ckpt')
    X_test = mnist.test.images[:2]
    output_val = outputs.eval(feed_dict={X: X_test})
fig, axes = plt.subplots(2, 2, figsize=(10, 10))
axes[0, 0].imshow(mnist.test.images[0, :].reshape([-1, 28]), cmap='gray')
axes[0, 1].imshow(output_val[0, :].reshape([-1, 28]), cmap='gray')
axes[1, 0].imshow(mnist.test.images[1, :].reshape([-1, 28]), cmap='gray')
axes[1, 1].imshow(output_val[1, :].reshape([-1, 28]), cmap='gray')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14301389904901243, 'fraction_numerical': 0.02596927578639356, 'mean_word_length': 3.9190647482014387, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '375362', 'license': 'MIT', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1006, 'n_words': 181, 'path': 'Chapter15_DenoisingAutoencoder.ipynb', 'repo_name': 'VandyChris/HandsOnMachineLearning', 'size': '2734'}