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

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


当前回答

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.

有趣的是,不仅可以提供占位符。您可以将值提供给变量,甚至是常量。

占位符:

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!! ")

Think of Variable in tensorflow as a normal variables which we use in programming languages. We initialize variables, we can modify it later as well. Whereas placeholder doesn’t require initial value. Placeholder simply allocates block of memory for future use. Later, we can use feed_dict to feed the data into placeholder. By default, placeholder has an unconstrained shape, which allows you to feed tensors of different shapes in a session. You can make constrained shape by passing optional argument -shape, as I have done below.

x = tf.placeholder(tf.float32,(3,4))
y =  x + 2

sess = tf.Session()
print(sess.run(y)) # will cause an error

s = np.random.rand(3,4)
print(sess.run(y, feed_dict={x:s}))

在执行机器学习任务时,大多数时候我们不知道行数,但(让我们假设)我们知道特征或列的数量。在这种情况下,我们可以使用None。

x = tf.placeholder(tf.float32, shape=(None,4))

现在,在运行时,我们可以输入任意4列任意行数的矩阵。

此外,占位符用于输入数据(它们是一种我们用来为模型提供信息的变量),其中变量是我们随时间训练的权重等参数。

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。变量为可训练变量,如权重(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。(例子摘自网页。)