Skip to content

Instantly share code, notes, and snippets.

@mwdchang
Created September 17, 2019 03:04
Show Gist options
  • Save mwdchang/50a165bc23c55aa7c84a338e21278b08 to your computer and use it in GitHub Desktop.
Save mwdchang/50a165bc23c55aa7c84a338e21278b08 to your computer and use it in GitHub Desktop.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import random
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./MNIST/", one_hot=True)
tf.reset_default_graph()
num_inputs = 784 # 28*28
neurons_hid1 = 39
neurons_hid2 = 196
neurons_hid3 = neurons_hid1 # Decoder Begins
num_outputs = num_inputs
learning_rate = 0.01
X = tf.placeholder(tf.float32, shape=[None, num_inputs])
XPrime = tf.placeholder(tf.float32, shape=[None, num_inputs])
initializer = tf.variance_scaling_initializer()
# Layers and architecture
w1 = tf.Variable(initializer([num_inputs, neurons_hid1]), dtype=tf.float32)
w2 = tf.Variable(initializer([neurons_hid1, neurons_hid2]), dtype=tf.float32)
w3 = tf.Variable(initializer([neurons_hid2, neurons_hid3]), dtype=tf.float32)
w4 = tf.Variable(initializer([neurons_hid3, num_outputs]), dtype=tf.float32)
b1 = tf.Variable(tf.zeros(neurons_hid1))
b2 = tf.Variable(tf.zeros(neurons_hid2))
b3 = tf.Variable(tf.zeros(neurons_hid3))
b4 = tf.Variable(tf.zeros(num_outputs))
act_func = tf.nn.relu
hid_layer1 = act_func(tf.matmul(X, w1) + b1)
hid_layer2 = act_func(tf.matmul(hid_layer1, w2) + b2)
hid_layer3 = act_func(tf.matmul(hid_layer2, w3) + b3)
output_layer = tf.matmul(hid_layer3, w4) + b4
# Loss function
loss = tf.reduce_mean(tf.square(output_layer - XPrime))
# loss = tf.reduce_mean(tf.square(output_layer - X))
# loss = tf.reduce_mean(tf.square(output_layer - mnist.test.images[0]))
optimizer = tf.train.AdamOptimizer(learning_rate)
train = optimizer.minimize(loss)
# Training
init = tf.global_variables_initializer()
num_epochs = 5
batch_size = 150
with tf.Session() as sess:
sess.run(init)
for epoch in range(num_epochs):
num_batches = mnist.train.num_examples // batch_size # // int division
for iteration in range(num_batches):
X_batch, y_batch = mnist.train.next_batch(batch_size)
X2 = X_batch + (0.3 * mnist.test.images[epoch])
# X2 = X_batch + (0.8 * mnist.test.images[random.randint(1, 3)])
# X2 = X_batch + (0.5 * mnist.test.images[3])
sess.run(train, feed_dict={X: X_batch, XPrime: X2})
training_loss = loss.eval(feed_dict={X: X_batch, XPrime: X2})
print("Epoch {} Complete. Training Loss: {}".format(epoch,training_loss))
num_test_images = 10
results = output_layer.eval(feed_dict={X:mnist.test.images[:num_test_images]})
f, a = plt.subplots(2, 10, figsize=(20, 4))
for i in range(num_test_images):
a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
a[1][i].imshow(np.reshape(results[i], (28, 28)))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment