我是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。

其他回答

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。

占位符:

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使用三种类型的容器来存储/执行过程

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

博士TL;

变量

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

占位符

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