在TensorFlow中动态更改权重

时间:2016-05-02 15:34:43

标签: machine-learning neural-network tensorflow

在TensorFlow中,我试图在训练期间改变权重,但结果没有变化。我试图破坏权重(设置为零),但似乎什么也没做(除了需要更长时间才能完成)。我错过了什么?在会话期间有没有办法像常规矩阵/张量一样操纵W?

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]), trainable=True)
W2 = tf.Variable(tf.zeros([784,10]), trainable=False)
b = tf.Variable(tf.zeros([10]))

sess.run(tf.initialize_all_variables())

y = tf.nn.softmax(tf.matmul(x,W) + b)
loss = tf.reduce_mean(tf.square(y_ - y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

for i in range(1000):
#try to change W during training
  W = W2

  W = tf.Variable(tf.zeros([784,10]))

  W.assign(tf.Variable(tf.zeros([784,10])))

  batch = mnist.train.next_batch(1)

  train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

准确度保持不变(0.82)。

2 个答案:

答案 0 :(得分:0)

我不确定这是个好主意,但是如果你想在W.assign之后更新W,你需要对它进行评估。

W = tf.get_variable("W", shape=[784, 10],
       initializer=tf.contrib.layers.xavier_initializer())

此外,由于TensorFlow和大多数神经网络使用前向/后向传播来计算值/梯度以更新权重,因此使用0初始化权重会导致所有前向值和梯度失效。这不是一个好主意。

您可以尝试使用小的随机数初始化它们:     tf.Variable(tf.random_normal([784,10],stddev = 0.01))

或使用xavier初始化程序

user_type

答案 1 :(得分:0)

使用tf.assign()时,需要为此操作指定名称:

W = W.assign(tf.Variable(tf.zeros([784,10])))

然后当您再次使用W时,将执行分配操作。