Skip to content

Instantly share code, notes, and snippets.

@matheushent
Created April 6, 2019 15:14
Show Gist options
  • Save matheushent/b33c3e7b9a9a462c27a313c2c6478267 to your computer and use it in GitHub Desktop.
Save matheushent/b33c3e7b9a9a462c27a313c2c6478267 to your computer and use it in GitHub Desktop.
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
mnist = input_data.read_data_sets('mnist/', one_hot=True)
X = mnist.train.images
# 784 -> 128 -> 64 -> 128 -> 784
# encoder
in_neuron = 784
hidden_neuron1 = 128
# coded data
hidden_neuron2 = 64
# decoder
hidden_neuron3 = hidden_neuron1
out_neuron = in_neuron
tf.reset_default_graph()
xph = tf.placeholder(tf.float32, [None, in_neuron])
# Xavier: sigmoid
# He: ReLU
initializer = tf.variance_scaling_initializer()
Weight = {'hidden_encoder1': tf.Variable(initializer([in_neuron, hidden_neuron1])),
'hidden_encoder2': tf.Variable(initializer([hidden_neuron1, hidden_neuron2])),
'hidden_decoded1': tf.Variable(initializer([hidden_neuron2, hidden_neuron3])),
'hidden_decoded2': tf.Variable(initializer([hidden_neuron3, out_neuron]))}
bias = {'hidden_encoder1': tf.Variable(initializer([hidden_neuron1])),
'hidden_encoder2': tf.Variable(initializer([hidden_neuron2])),
'hidden_decoded1': tf.Variable(initializer([hidden_neuron3])),
'hidden_decoded2': tf.Variable(initializer([out_neuron]))}
hidden_layer1 = tf.nn.relu(tf.add(tf.matmul(xph, Weight['hidden_encoder1']), bias['hidden_encoder1']))
hidden_layer2 = tf.nn.relu(tf.add(tf.matmul(hidden_layer1, Weight['hidden_encoder2']), bias['hidden_encoder2']))
hidden_layer3 = tf.nn.relu(tf.add(tf.matmul(hidden_layer2, Weight['hidden_decoded1']), bias['hidden_decoded1']))
out_layer = tf.nn.relu(tf.add(tf.matmul(hidden_layer3, Weight['hidden_decoded2']), bias['hidden_decoded2']))
error = tf.losses.mean_squared_error(xph, out_layer)
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
training = optimizer.minimize(error)
# 4, 582, 0.00014395, 0.000000001, 0.88712016, 0.86614952
batch_size = 128
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(50):
batch_num = mnist.train.num_examples // batch_size
for i in range(batch_num):
X_batch, _ = mnist.train.next_batch(batch_size)
cost, _ = sess.run([error, optimizer], feed_dict={xph: X_batch})
print('Epoch: ' + str(epoch + 1) + ' error: ' + str(cost))
coded_images = sess.run(hidden_layer2, feed_dict={xph: X})
decoded_images = sess.run(out_layer, feed_dict={xph: X})
image_num = 5
test_image = np.random.randint(X.shape[0], size=image_num)
print()
print(test_image)
print()
plt.figure(figsize=(18, 18))
for i, image_index in enumerate(test_image):
ax = plt.subplot(10, 5, i + 1)
plt.imshow(X[image_index].reshape(28, 28))
plt.yticks(())
plt.xticks(())
plt.show()
ax = plt.subplot(10, 5, i + 1 + image_num)
plt.imshow(coded_images[image_index].reshape(8, 8))
plt.yticks(())
plt.xticks(())
plt.show()
ax = plt.subplot(10, 5, i + 1 + image_num * 2)
plt.imshow(coded_images[image_index].reshape(28, 28))
plt.yticks(())
plt.xticks(())
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment