我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。
谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?
我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。
谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?
当前回答
占位符:
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!! ")
其他回答
因为张量计算由图组成,所以最好用图来解释这两者。
以简单的线性回归为例
WX+B=Y
其中W和B代表权重和偏差,X代表观测数据的输入,Y代表观测数据的输出。
显然X和Y是同一性质(显变量),而W和B是潜变量。X和Y是样本(观测值)的值,因此需要填充一个位置,而W和B是权重和偏差,图中的变量(前一个值影响后者),应该使用不同的X和Y对进行训练。我们在占位符中放置不同的样本来训练变量。
我们只需要保存或恢复变量(在检查点)来保存或重新构建代码图。
占位符主要是不同数据集的占位符(例如训练数据或测试数据)。然而,变量在训练过程中被训练为特定的任务,即预测输入的结果或将输入映射到所需的标签。它们保持不变,直到你使用不同或相同的样本重新训练或微调模型,通常通过字典填充占位符。例如:
session.run(a_graph, dict = {a_placeholder_name : sample_values})
占位符也作为参数传递给设置模型。
如果你在训练过程中改变了模型的占位符(添加、删除、改变形状等),你仍然可以重新加载检查点,而不需要任何其他修改。但是如果保存的模型的变量发生了变化,您应该相应地调整检查点以重新加载它并继续训练(图中定义的所有变量都应该在检查点中可用)。
总而言之,如果值来自样本(您已经拥有的观察结果),您可以安全地设置一个占位符来保存它们,而如果您需要训练一个参数,则利用一个变量(简单地说,为您想使用TF自动获得的值设置变量)。
在一些有趣的模型中,比如样式转换模型,输入像素将被优化,通常被称为模型变量是固定的,然后我们应该将输入(通常是随机初始化的)作为在该链接中实现的变量。
要了解更多信息,请参考这个简单明了的文档。
最明显的区别是。变量和tf。占位符是
使用变量保存和更新参数。变量是 包含张量的内存缓冲区。它们必须明确 已初始化,可以在培训期间和培训结束后保存到磁盘。你 可以稍后恢复保存的值以练习或分析模型。
变量的初始化使用sess.run(tf.global_variables_initializer())完成。另外,在创建变量时,你需要将一个Tensor作为它的初始值传递给variable()构造函数,当你创建一个变量时,你总是知道它的形状。
另一方面,您不能更新占位符。它们也不应该被初始化,但因为它们是一个有一个张量的承诺,你需要将值输入到它们sess.run(<op>, {a: <some_val>})。最后,与变量相比,占位符可能不知道形状。您可以提供部分维度,也可以什么都不提供。
还有其他区别:
the values inside the variable can be updated during optimizations variables can be shared, and can be non-trainable the values inside the variable can be stored after training when the variable is created, 3 ops are added to a graph (variable op, initializer op, ops for the initial value) placeholder is a function, Variable is a class (hence an uppercase) when you use TF in a distributed environment, variables are stored in a special place (parameter server) and are shared between the workers.
有趣的是,不仅可以提供占位符。您可以将值提供给变量,甚至是常量。
博士TL;
变量
为了学习参数 价值观可以从培训中获得 初始值是必需的(通常是随机的)
占位符
为数据分配存储(例如在馈送期间用于图像像素数据) 初始值不是必需的(但可以设置,参见tf.placeholder_with_default)
除了其他人的答案,他们在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.
区别在于tf。变量,在声明时必须提供初始值。特遣部队。占位符,你不必提供初始值,你可以在运行时在Session.run中使用feed_dict参数指定它