I wanted to make an autoencoder with just 1 layer, which has 100 hidden units. And, I used MNIST datasets given by tensorflow.
But, it does not work. I don't know what the problem is. When I debugged, my decoder layer just is filled with all 1's.
Is the back-propagation update does not working? Or, single layer autoencoder cannnot be operated?
Give me some help please.
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
if __name__ == "__main__":
# load data
mnist = input_data.read_data_sets("../neural_network/data/mnist", one_hot=True)
# make placeholder
X = tf.placeholder("float32", [None, 784])
# define constant
learning_rate = 0.01
training_epochs = 10
batch_size = 100
display_step = 1
# make variables / encoding,decoding layer
W_encoder = tf.Variable(tf.random_uniform([784, 200], 0.45, 0.55), name="encoder")
W_decoder = tf.Variable(tf.random_uniform([200, 784], 0.45, 0.55), name="decoder")
b_encoder = tf.Variable(tf.random_uniform([200], 0.005, 0.015))
b_decoder = tf.Variable(tf.random_uniform([784], 0.005, 0.015))
# construct encoder / decoder model
encoder_layer = tf.nn.sigmoid(tf.matmul(X, W_encoder) + b_encoder)
decoder_layer = tf.nn.sigmoid(tf.matmul(encoder_layer, W_decoder) + b_decoder)
# predict / optimization
y_pred = decoder_layer
y_true = X
# cost = tf.nn.sigmoid_cross_entropy_with_logits(logits=y_pred, labels=y_true)
cost = tf.reduce_mean(tf.square(y_true - y_pred))
# cost = tf.reduce_mean(-1. * X * tf.log(decoder) - (1. - X)* tf.log(1 - decoder))
optimizer = tf.train.RMSPropOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
total_batch = int(mnist.train.num_examples/batch_size)
# total training cycle
for epoch in range(training_epochs):
# total batch cycle
for i in range(total_batch):
batch_x, batch_y = mnist.train.next_batch(batch_size)
print("before fetch")
print(sess.run(y_pred, feed_dict={X: batch_x}))
_, c = sess.run([optimizer, cost], feed_dict={X : batch_x})
print("after fetch")
print(sess.run(y_pred, feed_dict={X: batch_x}))
if epoch % display_step == 0:
print("Epoch : %04d" % (epoch+1), "cost : {:.9f}".format(c))
print("training finished")
encode_decode =sess.run(y_pred, feed_dict={X : mnist.test.images[:100]})
# 출력.
fig, ax = plt.subplots(nrows=10, ncols=20, figsize=(20, 10))
for i in range(10):
for j in range(10):
ax[i][j].imshow(np.reshape(mnist.test.images[i*10 + j], (28, 28)))
ax[i][j+10].imshow(np.reshape(encode_decode[i*10 + j], (28, 28)))
fig.show()
plt.draw()
plt.waitforbuttonpress()