我想制作一个只有1层的自动编码器,它有100个隐藏单元。而且,我使用了tensorflow给出的MNIST数据集。
但是,它不起作用。我不知道问题是什么。 当我调试时,我的解码器层只填充了所有1个。
反向传播更新是否无效? 或者,单层自动编码器无法操作?
请给我一些帮助。
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()
答案 0 :(得分:0)
您是否可以尝试将权重图层设置为具有负数的随机统一数字?
W_decoder = tf.Variable(tf.random_uniform([200, 784], -0.45, 0.55), name="decoder")
此外,尝试稍微清理一下代码,以便我们更好地了解发生了什么。
祝你好运,让我知道它是否有效。