深度学习-Autoencoder

#-*- coding:utf-8 -*-
#author:zhangwei

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets('mnist_data' , one_hot=True)

learning_rate = 0.0001
training_epochs = 20
batch_size = 256
display_step = 1
examples_to_show = 10
n_inputs = 784

x = tf.placeholder(tf.float32 , [None , n_inputs])
# y = tf.placeholder(tf.float32 , [None , examples_to_show])

n_hidden_1 = 256
n_hidden_2 = 128
n_hidden_3 = 10
n_hidden_4 = 2

weights = {'encoder_h1':tf.Variable(tf.random_normal([n_inputs , n_hidden_1])) ,
           'encoder_h2':tf.Variable(tf.random_normal([n_hidden_1 , n_hidden_2])) ,
           'encoder_h3':tf.Variable(tf.random_normal([n_hidden_2 , n_hidden_3])) ,
           'encoder_h4':tf.Variable(tf.random_normal([n_hidden_3 , n_hidden_4])) ,

           'decoder_h1':tf.Variable(tf.random_normal([n_hidden_4 , n_hidden_3])) ,
           'decoder_h2':tf.Variable(tf.random_normal([n_hidden_3 , n_hidden_2])) ,
           'decoder_h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])),
           'decoder_h4': tf.Variable(tf.random_normal([n_hidden_1, n_inputs]))
           }
biases = {'encoder_h1':tf.Variable(tf.random_normal([n_hidden_1])) ,
           'encoder_h2':tf.Variable(tf.random_normal([n_hidden_2])) ,

           'decoder_h1':tf.Variable(tf.random_normal([n_hidden_1])) ,
           'decoder_h2':tf.Variable(tf.random_normal([n_inputs]))}

def encoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x , weights['encoder_h1']) , biases['encoder_h1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1 , weights['encoder_h2']) , biases['encoder_h2']))
    return layer_2
def decoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x , weights['decoder_h1']) , biases['decoder_h1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1 , weights['decoder_h2']) , biases['decoder_h2']))
    return layer_2

encode_op = encoder(x)
decode_op = decoder(encode_op)

y_prediction = decode_op
y_true = x

loss_op = tf.reduce_mean(tf.pow(y_prediction - y_true , 2))
train_op = tf.train.AdamOptimizer(learning_rate).minimize(loss_op)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        batch_xs , batch_ys = mnist.train.next_batch(batch_size)
        _ , loss = sess.run([train_op , loss_op] , feed_dict={x :batch_xs})
        if epoch % 10 == 0:
            print 'Loss : ' + str(loss)
    encoder_decoder = sess.run(y_prediction , feed_dict={x : mnist.test.images[:examples_to_show]})
    f ,a = plt.subplots(2,10 , figsize=(10,2))     #figsize代表的是每个子图的大小,宽度和高度,大小是英寸;
    for i in range(examples_to_show):
        a[0][i].imshow(np.reshape(mnist.test.images[i] , newshape=[28,28]))
        a[1][i].imshow(np.reshape(mnist.test.images[i] , newshape=[28,28]))
    plt.show()

猜你喜欢

转载自blog.csdn.net/xwei1226/article/details/80667178