<filename>autoencoder_demo/autoencoder_demo.py
"""
TODO: docstring
"""
import mnist_data
import tensorflow

class Autoencoder:
    """
    TODO: docstring
    """
    def __init__(self):
        """
        TODO: docstring
        """
        mnist_width = 28
        n_visible = mnist_width * mnist_width
        n_hidden = 500
        corruption_level = 0.3
        self.X = tensorflow.placeholder('float', [None, n_visible], name='X')
        self.mask = tensorflow.placeholder('float', [None, n_visible], name='mask')
        W_init_max = 4 * numpy.sqrt(6. / (n_visible + n_hidden))
        W_init = tensorflow.random_uniform(
            shape=[n_visible, n_hidden], minval=-W_init_max, maxval=W_init_max)
        self.W = tensorflow.Variable(W_init, name='W')
        self.b = tensorflow.Variable(tensorflow.zeros([n_hidden]), name='b')
        self.W_prime = tensorflow.transpose(self.W)
        self.b_prime = tensorflow.Variable(tensorflow.zeros([n_visible]), name='b_prime')
    
    def __call__(self):
        """
        TODO: docstring
        """
        Z = self.model(self.X, self.mask, self.W, self.b, self.W_prime, self.b_prime)
        cost = tensorflow.reduce_sum(tensorflow.pow(self.X - Z, 2))
        train_op = tensorflow.train.GradientDescentOptimizer(0.02).minimize(cost)
        mnist = mnist_data.InputData().read_data_sets("MNIST_data/", one_hot=True)
        trX, trY = mnist.train.images, mnist.train.labels
        teX, teY = mnist.test.images, mnist.test.labels
        with tensorflow.Session() as sess:
            tensorflow.initialize_all_variables().run()
            for i in range(100):
                for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
                    input_ = trX[start:end]
                    mask_np = numpy.random.binomial(1, 1 - corruption_level, input_.shape)
                    sess.run(train_op, feed_dict={X: input_, mask: mask_np})
                mask_np = numpy.random.binomial(1, 1 - corruption_level, teX.shape)
                print(i, sess.run(cost, feed_dict={X: teX, mask: mask_np}))

    def model(self, X, mask, W, b, W_prime, b_prime):
        """
        DOCSTRING
        """
        tilde_X = mask * X
        Y = tensorflow.nn.sigmoid(tensorflow.matmul(tilde_X, W) + b)
        Z = tensorflow.nn.sigmoid(tensorflow.matmul(Y, W_prime) + b_prime)
        return Z

if __name__ == '__main__':
    autoencoder = Autoencoder()
    autoencoder()

<filename>autoencoder_demo/autoencoder_demo.py
"""
TODO: docstring
"""
import mnist_data
import tensorflow

class Autoencoder:
    """
    TODO: docstring
    """
    def __init__(self):
        """
        TODO: docstring
        """
        mnist_width = 28
        n_visible = mnist_width * mnist_width
        n_hidden = 500
        corruption_level = 0.3
        self.X = tensorflow.placeholder('float', [None, n_visible], name='X')
        self.mask = tensorflow.placeholder('float', [None, n_visible], name='mask')
        W_init_max = 4 * numpy.sqrt(6. / (n_visible + n_hidden))
        W_init = tensorflow.random_uniform(
            shape=[n_visible, n_hidden], minval=-W_init_max, maxval=W_init_max)
        self.W = tensorflow.Variable(W_init, name='W')
        self.b = tensorflow.Variable(tensorflow.zeros([n_hidden]), name='b')
        self.W_prime = tensorflow.transpose(self.W)
        self.b_prime = tensorflow.Variable(tensorflow.zeros([n_visible]), name='b_prime')
    
    def __call__(self):
        """
        TODO: docstring
        """
        Z = self.model(self.X, self.mask, self.W, self.b, self.W_prime, self.b_prime)
        cost = tensorflow.reduce_sum(tensorflow.pow(self.X - Z, 2))
        train_op = tensorflow.train.GradientDescentOptimizer(0.02).minimize(cost)
        mnist = mnist_data.InputData().read_data_sets("MNIST_data/", one_hot=True)
        trX, trY = mnist.train.images, mnist.train.labels
        teX, teY = mnist.test.images, mnist.test.labels
        with tensorflow.Session() as sess:
            tensorflow.initialize_all_variables().run()
            for i in range(100):
                for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
                    input_ = trX[start:end]
                    mask_np = numpy.random.binomial(1, 1 - corruption_level, input_.shape)
                    sess.run(train_op, feed_dict={X: input_, mask: mask_np})
                mask_np = numpy.random.binomial(1, 1 - corruption_level, teX.shape)
                print(i, sess.run(cost, feed_dict={X: teX, mask: mask_np}))

    def model(self, X, mask, W, b, W_prime, b_prime):
        """
        DOCSTRING
        """
        tilde_X = mask * X
        Y = tensorflow.nn.sigmoid(tensorflow.matmul(tilde_X, W) + b)
        Z = tensorflow.nn.sigmoid(tensorflow.matmul(Y, W_prime) + b_prime)
        return Z

if __name__ == '__main__':
    autoencoder = Autoencoder()
    autoencoder()

TODO: docstring TODO: docstring TODO: docstring TODO: docstring DOCSTRING