<filename>tensorflow_autoencoder.py
# -*- coding: utf-8 -*-
"""Tensorflow_autoencoder.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1Yh4nzukoQqG8BM0U3_741RDlBbmcUrtA
"""

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.layers import fully_connected

mnist=input_data.read_data_sets("/MNIST_data/",one_hot=True)

num_inputs=784    #28x28 pixels
num_hid1=392
num_hid2=196
num_hid3=96
num_hid4=32

num_hid5=num_hid3
num_hid6=num_hid2
num_hid7=num_hid1
num_output=num_inputs
lr=0.001
actf=tf.nn.relu


X=tf.placeholder(tf.float32,shape=[None,num_inputs])
initializer=tf.variance_scaling_initializer()

w1=tf.Variable(initializer([num_inputs,num_hid1]),dtype=tf.float32)
w2=tf.Variable(initializer([num_hid1,num_hid2]),dtype=tf.float32)
w3=tf.Variable(initializer([num_hid2,num_hid3]),dtype=tf.float32)
w4=tf.Variable(initializer([num_hid3,num_hid4]),dtype=tf.float32)
w5=tf.Variable(initializer([num_hid4,num_hid5]),dtype=tf.float32)
w6=tf.Variable(initializer([num_hid5,num_hid6]),dtype=tf.float32)
w7=tf.Variable(initializer([num_hid6,num_hid7]),dtype=tf.float32)
w8=tf.Variable(initializer([num_hid7,num_output]),dtype=tf.float32)


b1=tf.Variable(tf.zeros(num_hid1))
b2=tf.Variable(tf.zeros(num_hid2))
b3=tf.Variable(tf.zeros(num_hid3))
b4=tf.Variable(tf.zeros(num_hid4))
b5=tf.Variable(tf.zeros(num_hid5))
b6=tf.Variable(tf.zeros(num_hid6))
b7=tf.Variable(tf.zeros(num_hid7))
b8=tf.Variable(tf.zeros(num_output))

hid_layer1=actf(tf.matmul(X,w1)+b1)
hid_layer2=actf(tf.matmul(hid_layer1,w2)+b2)
hid_layer3=actf(tf.matmul(hid_layer2,w3)+b3)
hid_layer4=actf(tf.matmul(hid_layer3,w4)+b4)
hid_layer5=actf(tf.matmul(hid_layer4,w5)+b5)
hid_layer6=actf(tf.matmul(hid_layer5,w6)+b6)
hid_layer7=actf(tf.matmul(hid_layer6,w7)+b7)
output_layer=actf(tf.matmul(hid_layer7,w8)+b8)


loss=tf.reduce_mean(tf.square(output_layer-X))

optimizer=tf.train.AdamOptimizer(lr)
train=optimizer.minimize(loss)

init=tf.global_variables_initializer()


num_epoch=100
batch_size=150
num_test_images=10

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(num_epoch):
        
        num_batches=mnist.train.num_examples//batch_size
        for iteration in range(num_batches):
            X_batch,y_batch=mnist.train.next_batch(batch_size)
            sess.run(train,feed_dict={X:X_batch})
            
        train_loss=loss.eval(feed_dict={X:X_batch})
        print("epoch {} loss {}".format(epoch,train_loss))

    results=output_layer.eval(feed_dict={X:mnist.test.images[:num_test_images]})
    
#Comparing original images with reconstructions
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)))