Non Linear Autoencoders


In [19]:
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import seaborn as sns

In [20]:
from tensorflow.examples.tutorials.mnist import input_data

In [21]:
mnist = input_data.read_data_sets('../MNIST_data/',one_hot=True)


Extracting ../MNIST_data/train-images-idx3-ubyte.gz
Extracting ../MNIST_data/train-labels-idx1-ubyte.gz
Extracting ../MNIST_data/t10k-images-idx3-ubyte.gz
Extracting ../MNIST_data/t10k-labels-idx1-ubyte.gz

In [22]:
plt.imshow(mnist.train.images[0].reshape((28,28)),cmap='gray',interpolation="nearest")


Out[22]:
<matplotlib.image.AxesImage at 0x12025fe50>

In [47]:
learning_rate = 0.01
n_training_examples,n_features = mnist.train.images.shape
batch_size = 100
n_epochs = 100

In [48]:
tf.reset_default_graph()

In [49]:
with tf.variable_scope("data") as scope:
    input_image = tf.placeholder(dtype=tf.float32,shape=[None,784],name="input")

In [50]:
with tf.variable_scope("hidden_layer") as scope:
    w = tf.get_variable(name="weights",shape=[784,128],initializer=tf.contrib.layers.xavier_initializer())
    b = tf.get_variable(name="biases",shape=[128],initializer=tf.random_normal_initializer())
    encoding = tf.nn.relu(tf.matmul(input_image,w) + b)

In [51]:
with tf.variable_scope("output_layer") as scope:
    w = tf.get_variable(name="weights",shape=[128,784],initializer=tf.contrib.layers.xavier_initializer())
    b = tf.get_variable(name="biases",shape=[784],initializer=tf.random_normal_initializer())
    output_image = tf.matmul(encoding,w) + b

In [52]:
with tf.variable_scope("loss") as scope:
    loss = tf.reduce_mean(tf.squared_difference(input_image,output_image))

In [53]:
with tf.variable_scope("optimizer") as scope:
    optimizer = tf.train.AdagradOptimizer(learning_rate).minimize(loss)

In [46]:
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    writer = tf.summary.FileWriter('graphs/',sess.graph)
    ## Training the network
    for i in range(n_epochs):
        epoch_loss = 0
        for batch in range(n_training_examples/batch_size):
            x_batch,_ = mnist.train.next_batch(batch_size)
            _,l = sess.run([optimizer,loss],feed_dict = {input_image:x_batch})
            epoch_loss += l
        print 'Epoch: {}\t Loss: {}'.format(i+1,epoch_loss)
    
    ## Testing Examples
    print 'Checking the output of network on Test data....'
    output = []
    error = 0
    for i in range(n_testing_examples/batch_size):
        x_batch,_ = mnist.test.next_batch(batch_size)
        l,image = sess.run([loss,output_image],feed_dict = {input_image:x_batch})
        output.append(image)
        error += l
        if (i+1)%10 == 0:
            print 'Error after {} batches: {}'.format(i+1,error)
            error = 0


Epoch: 1	 Loss: 132.44113759
Epoch: 2	 Loss: 46.3421655819
Checking the output of network on Test data....
Error after 10 batches: 0.818022422493
Error after 20 batches: 0.830460265279
Error after 30 batches: 0.811907492578
Error after 40 batches: 0.815589219332
Error after 50 batches: 0.801952071488
Error after 60 batches: 0.813542537391
Error after 70 batches: 0.811015278101
Error after 80 batches: 0.823792785406
Error after 90 batches: 0.800543837249
Error after 100 batches: 0.813860349357

In [38]:
output = np.asarray(output).reshape((10000,784))
print output.shape


(10000, 784)

In [42]:
plt.imshow(output[0].reshape((28,28)),cmap="gray",interpolation="nearest")


Out[42]:
<matplotlib.image.AxesImage at 0x12202b8d0>

In [44]:
np.argmax(mnist.test.labels[0])


Out[44]:
4

In [ ]: