我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。

谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?


当前回答

Tensorflow使用三种类型的容器来存储/执行过程

Constants:Constants保存典型数据。 变量:数据值将被改变,相应的函数,如cost_function.. 占位符:训练/测试数据将被传递到图表中。

其他回答

除了其他人的答案,他们在Tensoflow网站上的MNIST教程中也解释得很好:

We describe these interacting operations by manipulating symbolic variables. Let's create one: x = tf.placeholder(tf.float32, [None, 784]), x isn't a specific value. It's a placeholder, a value that we'll input when we ask TensorFlow to run a computation. We want to be able to input any number of MNIST images, each flattened into a 784-dimensional vector. We represent this as a 2-D tensor of floating-point numbers, with a shape [None, 784]. (Here None means that a dimension can be of any length.) We also need the weights and biases for our model. We could imagine treating these like additional inputs, but TensorFlow has an even better way to handle it: Variable. A Variable is a modifiable tensor that lives in TensorFlow's graph of interacting operations. It can be used and even modified by the computation. For machine learning applications, one generally has the model parameters be Variables. W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) We create these Variables by giving tf.Variable the initial value of the Variable: in this case, we initialize both W and b as tensors full of zeros. Since we are going to learn W and b, it doesn't matter very much what they initially are.

变量

TensorFlow变量是表示程序操纵的共享持久状态的最佳方式。变量是通过tf操作的。变量类。内部是一个tf。变量存储一个持久张量。特定的操作允许你读取和修改这个张量的值。这些修改在多个tf中可见。会话,因此多个工作人员可以看到tf.Variable的相同值。变量在使用前必须初始化。

例子:

x = tf.Variable(3, name="x")
y = tf.Variable(4, name="y")
f = x*x*y + y + 2

这将创建一个计算图。变量(x和y)可以被初始化,函数(f)在一个tensorflow会话中被计算,如下所示:

with tf.Session() as sess:
     x.initializer.run()
     y.initializer.run()
     result = f.eval()
print(result)
42

占位符

占位符是一个节点(与变量相同),其值可以在将来初始化。这些节点基本上在运行时输出分配给它们的值。占位符节点可以使用tf.placeholder()类来分配,你可以为它提供参数,比如变量的类型和/或它的形状。占位符广泛用于表示机器学习模型中的训练数据集,因为训练数据集不断变化。

例子:

A = tf.placeholder(tf.float32, shape=(None, 3))
B = A + 5

注意:维度的“None”表示“任何大小”。

with tf.Session as sess:
    B_val_1 = B.eval(feed_dict={A: [[1, 2, 3]]})
    B_val_2 = B.eval(feed_dict={A: [[4, 5, 6], [7, 8, 9]]})

print(B_val_1)
[[6. 7. 8.]]
print(B_val_2)
[[9. 10. 11.]
 [12. 13. 14.]]

引用:

https://www.tensorflow.org/guide/variables https://www.tensorflow.org/api_docs/python/tf/placeholder O'Reilly:使用Scikit-Learn和Tensorflow进行动手机器学习

对于TF V1:

常数是有初始值的,在计算中不会发生变化; 变量具有初值,在计算中可以变化;(对于参数来说很好) 占位符没有初始值,在计算中不会改变。(非常适合像预测实例这样的输入)

对于TF V2,同样,但他们试图隐藏占位符(图形模式不是首选)。

占位符:

A placeholder is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In TensorFlow terminology, we then feed data into the graph through these placeholders. Initial values are not required but can have default values with tf.placeholder_with_default) We have to provide value at runtime like : a = tf.placeholder(tf.int16) // initialize placeholder value b = tf.placeholder(tf.int16) // initialize placeholder value use it using session like : sess.run(add, feed_dict={a: 2, b: 3}) // this value we have to assign at runtime

变量:

TensorFlow变量是表示共享的最佳方式, 由程序操纵的持久状态。 变量是通过tf操作的。变量类。一个特遣部队。变量 表示一个张量,其值可以通过对其运行操作来改变。

例如:tf。变量("欢迎来到tensorflow!! ")

博士TL;

变量

为了学习参数 价值观可以从培训中获得 初始值是必需的(通常是随机的)

占位符

为数据分配存储(例如在馈送期间用于图像像素数据) 初始值不是必需的(但可以设置,参见tf.placeholder_with_default)