tf.placeholder 和 tf.Variable 有什么区别?

tf.placeholder 和 tf.Variable 有什么区别?

问题描述:

我是 TensorFlow 的新手.我对 tf.placeholdertf.Variable 之间的区别感到困惑.在我看来,tf.placeholder 用于输入数据,tf.Variable 用于存储数据的状态.这就是我所知道的.

I'm a newbie to TensorFlow. I'm confused about the difference between tf.placeholder and tf.Variable. In my view, tf.placeholder is used for input data, and tf.Variable is used to store the state of data. This is all what I know.

有人可以更详细地向我解释他们的差异吗?特别是,何时使用 tf.Variable 以及何时使用 tf.placeholder?

Could someone explain to me more in detail about their differences? In particular, when to use tf.Variable and when to use tf.placeholder?

简而言之,您将 tf.Variable 用于可训练变量,例如模型的权重 (W) 和偏差 (B).

In short, you use tf.Variable for trainable variables such as weights (W) and biases (B) for your model.

weights = tf.Variable(
    tf.truncated_normal([IMAGE_PIXELS, hidden1_units],
                    stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), name='weights')

biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')

tf.placeholder 用于提供实际的训练示例.

tf.placeholder is used to feed actual training examples.

images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_PIXELS))
labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))

这是您在训练期间提供训练示例的方式:

This is how you feed the training examples during the training:

for step in xrange(FLAGS.max_steps):
    feed_dict = {
       images_placeholder: images_feed,
       labels_placeholder: labels_feed,
     }
    _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

您的 tf.variables 将作为本次培训的结果进行培训(修改).

Your tf.variables will be trained (modified) as the result of this training.

https://www.tensorflow.org/versions/查看更多信息r0.7/tutorials/mnist/tf/index.html.(示例取自网页.)

See more at https://www.tensorflow.org/versions/r0.7/tutorials/mnist/tf/index.html. (Examples are taken from the web page.)