我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。
谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?
我是TensorFlow的新手。我搞不懂tf的区别。占位符和tf.Variable。在我看来,tf。占位符用于输入数据,tf。变量用于存储数据的状态。这就是我所知道的一切。
谁能给我详细解释一下他们的不同之处吗?特别是,什么时候使用tf。变量和何时使用tf.placeholder?
当前回答
示例代码片段:
import numpy as np
import tensorflow as tf
### Model parameters ###
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], tf.float32)
### Model input and output ###
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
### loss ###
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
### optimizer ###
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
### training data ###
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
### training loop ###
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
sess.run(train, {x:x_train, y:y_train})
顾名思义,占位符是稍后提供一个值的承诺。
变量只是训练参数(W(矩阵),b(偏差),与您在日常编程中使用的正常变量相同,培训师在每次运行/步骤中更新/修改。
虽然占位符不需要任何初始值,当你创建x和y时,TF不分配任何内存,相反,当你在sesss .run()中使用feed_dict提供占位符时,TensorFlow将为它们分配适当大小的内存(x和y) -这种不受约束的特性允许我们提供任何大小和形状的数据。
简而言之:
Variable -是一个你希望训练器(例如GradientDescentOptimizer)在每一步之后更新的参数。
占位符演示-
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b # + provides a shortcut for tf.add(a, b)
执行:
print(sess.run(adder_node, {a: 3, b:4.5}))
print(sess.run(adder_node, {a: [1,3], b: [2, 4]}))
结果是输出
7.5
[ 3. 7.]
在第一种情况下,3和4.5将分别传递给a和b,然后传递给adder_node输出7。在第二种情况下,有一个提要列表,第一步1和2将被添加,接下来的3和4 (a和b)。
相关阅读:
特遣部队。占位符doc。 特遣部队。变量doc。 变量VS占位符。
其他回答
简而言之,使用tf。变量为可训练变量,如权重(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')
特遣部队。占位符用于提供实际的训练示例。
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)
你的助教。变量将被训练(修改)作为这个训练的结果。
详见https://www.tensorflow.org/versions/r0.7/tutorials/mnist/tf/index.html。(例子摘自网页。)
变量
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。变量,在声明时必须提供初始值。特遣部队。占位符,你不必提供初始值,你可以在运行时在Session.run中使用feed_dict参数指定它
Tensorflow 2.0兼容答案:占位符的概念,tf。占位符在Tensorflow 2中不可用。x(>= 2.0),因为默认执行模式为“主动执行”。
但是,我们可以在图形模式下使用它们(禁用急切执行)。
版本2中TF占位符的等效命令。X是tf. compatat .v1.placeholder。
版本2中TF变量的等效命令。X等于tf。变量和如果您想从1迁移代码。X到2。X,等效命令为
tf.compat.v2.Variable。
有关Tensorflow 2.0版本的更多信息,请参阅此Tensorflow页面。
有关从版本1迁移的更多信息,请参阅迁移指南。X到2。X。
最明显的区别是。变量和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.
有趣的是,不仅可以提供占位符。您可以将值提供给变量,甚至是常量。