【发布时间】:2016-01-20 21:49:08
【问题描述】:
我一直在尝试为 TensorFlow 中的 GradientDescentOptimizer 的每个步骤收集梯度步骤,但是当我尝试将 apply_gradients() 的结果传递给 sess.run() 时,我一直遇到 TypeError。我要运行的代码是:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
x = tf.placeholder(tf.float32,[None,784])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x,W)+b)
y_ = tf.placeholder(tf.float32,[None,10])
cross_entropy = -tf.reduce_sum(y_*log(y))
# note that up to this point, this example is identical to the tutorial on tensorflow.org
gradstep = tf.train.GradientDescentOptimizer(0.01).compute_gradients(cross_entropy)
sess = tf.Session()
sess.run(tf.initialize_all_variables())
batch_x,batch_y = mnist.train.next_batch(100)
print sess.run(gradstep, feed_dict={x:batch_x,y_:batch_y})
请注意,如果我将最后一行替换为print sess.run(train_step,feed_dict={x:batch_x,y_:batch_y}),其中train_step = tf.GradientDescentOptimizer(0.01).minimize(cross_entropy),则不会引发错误。我的困惑源于minimize 调用compute_gradients 的参数与其第一步完全相同。有人可以解释为什么会发生这种行为吗?
【问题讨论】:
标签: python tensorflow