2017-03-16 58 views
0

我想製作一個只有1層的自動編碼器,它有100個隱藏單元。而且,我使用了由tensorflow給出的MNIST數據集。爲什麼我用tensorflow做的1個隱藏層autoencoder不起作用?

但是,它不起作用。我不知道問題是什麼。 當我調試時,我的解碼器層只填滿了全部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

你能爲我做點什麼嗎? 你有你的權重隨機均勻初始化: https://www.tensorflow.org/api_docs/python/tf/random_uniform

你能試試設定的權重層具有隨機統一編號,同時也是負面的?

W_decoder = tf.Variable(tf.random_uniform([200, 784], -0.45, 0.55), name="decoder") 

此外,嘗試清理你的代碼有點,所以我們可以有一個更好的主意是怎麼回事。

祝你好運,讓我知道它是否有效。

+0

對不起,代碼不清晰:(我初始化,因爲MNIST的數據是灰度的,所以我認爲0.5附近的隨機值表現良好,我使用隨機正常初始化,但結果是一樣的。 –