What's the difference between tf.placeholder and tf.Variable

https://stackoverflow.com/questions/36693740/whats-the-difference-between-tf-placeholder-and-tf-variable

down vote
The difference is that with tf.Variable you have to provide an initial value when you declare it. With tf.placeholder you don’t have to provide an initial value and you can specify it at run time with the feed_dict argument inside Session.run

placeholder 用来表示输入和输出数据,不需要有初始值

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)

Variable 在Seesion训练时候会改变,而且需要有初始值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`tf.placeholder` 和 `tf.Variable` 都是 TensorFlow 中的重要概念,但在使用方式、作用和特点上有所不同。 `tf.placeholder` 是一个占位符,用于在 TensorFlow 的计算图中定义输入数据的位置。它在定义计算图的时候并不需要给定具体的数值,而是在计算图运行时,通过 `feed_dict` 参数传入具体的数值。它通常用于传入训练数据和标签等变量,例如: ``` x = tf.placeholder(tf.float32, shape=[None, 784]) y = tf.placeholder(tf.float32, shape=[None, 10]) ``` 在这个例子中,我们定义了两个 `tf.placeholder`,`x` 和 `y`,分别用于输入训练数据和标签。其中,`shape=[None, 784]` 表示输入数据的形状是一个二维张量,第一个维度可以是任意大小,第二个维度是 784。 相比之下,`tf.Variable` 则是用于在 TensorFlow 的计算图中定义需要优化的变量。它在定义时需要给定初始值,通常是一个随机数值或者全零的数组。在 TensorFlow 的计算过程中,`tf.Variable` 的值会不断地被优化,以使得算法达到更好的结果。例如: ``` W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) ``` 在这个例子中,我们定义了两个 `tf.Variable`,`W` 和 `b`,分别表示权重和偏置。它们的初始值都是全零的数组。 总的来说,`tf.placeholder` 用于传入数据,`tf.Variable` 用于定义需要优化的变量。它们在 TensorFlow 中都有着重要的作用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值