gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:04:51 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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 用于提供实际的训练示例。

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

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

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 将作为本次训练的结果进行训练(修改)。

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

关于tensorflow - tf.placeholder 和 tf.Variable 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36693740/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com