使用张量流训练自动编码器时,编码器的权重不会改变

时间:2017-03-01 06:40:38

标签: tensorflow autoencoder

实现autoencoder的编码器如下所示:

# One Layer Autoencoder
# Parameters
learning_rate = 0.01
training_epochs = 20
batch_size = 256
display_step = 1
examples_to_show = 10

# Network Parameters
n_hidden= 128 # 1st layer num features
n_input = 784 # MNIST data input (img shape: 28*28)

# tf Graph input (only pictures)
X = tf.placeholder("float", [None, n_input])

weights = {
    'encoder_h': tf.Variable(tf.random_normal([n_input, n_hidden])),
    'decoder_h': tf.Variable(tf.random_normal([n_hidden, n_input])),
}
biases = {
    'encoder_b': tf.Variable(tf.random_normal([n_hidden])),
    'decoder_b': tf.Variable(tf.random_normal([n_input])),
}

# Building the encoder
hidden_layer =             tf.nn.sigmoid(tf.add(tf.matmul(X,weights["encoder_h"]),biases["encoder_b"]))
out_layer = tf.nn.sigmoid(tf.add(tf.matmul(hidden_layer,weights["decoder_h"]),biases["decoder_b"]))


# Prediction
y_pred = out_layer
# Targets (Labels) are the input data.
y_true = X

# Define loss and optimizer, minimize the squared error
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)
# initializing the variables
init = tf.initialize_all_variables()
with tf.device("/gpu:0"):
    with tf.Session(config=config) as sess:
        sess.run(init)
        total_batch = int(mnist.train.num_examples/batch_size)
        print([total_batch,batch_size,mnist.train.num_examples])
        for epoch in range(training_epochs):#each round
            for i in range(total_batch):
                batch_xs, batch_ys = mnist.train.next_batch(batch_size)
                # Run optimization op (backprop) and cost op (to get loss value)
                _, loss_c = sess.run([optimizer, cost], feed_dict={X: batch_xs})

            if epoch % display_step == 0:
                encoder_w = weights["encoder_h"]
                encoder_w_eval = encoder_w.eval()
                print(encoder_w_eval[0,0])
                decoder_w = weights["decoder_h"]
                decoder_w_eval = decoder_w.eval()
                print(decoder_w_eval[0,0])
                print("Epoch:","%04d"%(epoch+1),
                 "cost=","{:.9f}".format(loss_c))
        print("Optimization Finished!")

当我打印编码器时,解码器重量和损耗。 解码器和损失权重在训练时会改变,但编码器重量保持不变,如下所示,我不知道为什么。有人帮忙。

encoder_w -0.00818192

decoder_w -1.48731

Epoch: 0001 cost= 0.132702485

encoder_w -0.00818192

decoder_w -1.4931

Epoch: 0002 cost= 0.089116640

encoder_w -0.00818192

decoder_w -1.49607

Epoch: 0003 cost= 0.080637991

encoder_w -0.00818192

decoder_w -1.49947

Epoch: 0004 cost= 0.073829792

encoder_w -0.00818192

decoder_w -1.50176

...

2 个答案:

答案 0 :(得分:1)

权重总是以这种方式表现。也就是说,它们总是具有高斯分布。请注意,您的输入可以遵循高维度的任何分布。此外,似乎如果您使用不同类型的分布,您最终会得到高斯分布(这来自概率论)。结果,权重的分布将以某种方式推广并且将继续遵循高斯分布。另请注意,Batch Normalization的目的是强制activation functions的输出遵循高斯分布。这是一般的直觉。

此外,l2_regulization在某种程度上迫使权重跟随Gaussian Distribution

最后,正如您所做的那样打印weights是错误的。相反,你应该使用Tensorboard

希望这个答案有所帮助。

答案 1 :(得分:0)

一般情况下,我建议检查Tensorboard上的图形,以确保它看起来像你期望的那样(例如,编码器权重有梯度更新。

在你的情况下,可能encoder_w[0, 0]变化不大,因为它的渐变很小,学习率也是如此。