Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 为什么即使对于相同的变量,tensorflow占位符上也有一个计数器?_Python 3.x_Tensorflow_Tensor - Fatal编程技术网

Python 3.x 为什么即使对于相同的变量,tensorflow占位符上也有一个计数器?

Python 3.x 为什么即使对于相同的变量,tensorflow占位符上也有一个计数器?,python-3.x,tensorflow,tensor,Python 3.x,Tensorflow,Tensor,使用iPython控制台在Tensorflow 1.8中进行以下实验: In [2]: x = tf.placeholder(tf.float16, [None, 784]) ...: y = tf.placeholder(tf.float16, [None, 10]) ...: ...: In [3]: x Out[3]: <tf.Tensor 'Placeholder:0' shape=(?, 784) dtype=float16> In [4]: y

使用iPython控制台在Tensorflow 1.8中进行以下实验:

In [2]: x = tf.placeholder(tf.float16, [None, 784])
   ...: y = tf.placeholder(tf.float16, [None, 10])
   ...: 
   ...: 

In [3]: x
Out[3]: <tf.Tensor 'Placeholder:0' shape=(?, 784) dtype=float16>

In [4]: y
Out[4]: <tf.Tensor 'Placeholder_1:0' shape=(?, 10) dtype=float16>

In [5]: x = tf.placeholder(tf.float16, [None, 784])
   ...: y = tf.placeholder(tf.float16, [None, 10])

In [6]: x
Out[6]: <tf.Tensor 'Placeholder_2:0' shape=(?, 784) dtype=float16>

In [7]: y
Out[7]: <tf.Tensor 'Placeholder_3:0' shape=(?, 10) dtype=float16>
[2]中的
x=tf.placeholder(tf.float16[None,784])
…:y=tf.placeholder(tf.float16,[None,10])
...: 
...: 
在[3]:x中
出[3]:
In[4]:y
出[4]:
在[5]中:x=tf.placeholder(tf.float16[None,784])
…:y=tf.placeholder(tf.float16,[None,10])
在[6]:x中
出[6]:
In[7]:y
出[7]:
为什么这个“占位符计数器”存在,原因是什么?我用相同的占位符重新分配变量,所以我想知道为什么它们没有再次编号为“占位符:0”和“占位符_1:0”


谢谢

每次运行
x=tf.placeholder(tf.float16,[None,784])
时,op都会添加到默认图形中。您可以通过以下方式进行检查:

for op in tf.get_default_graph().get_operations():
   print (op.name)
#Placeholder
#Placeholder_1
#Placeholder_2

因此,在
ipython notebook
中,您可以在单元格之前执行
tf.reset\u default\u graph()
,以避免这种行为。

谢谢,我试过了,效果很好。尽管如此,为什么它们会被复制而不是简单地覆盖(正如我想的那样,因为我用相同的名称命名占位符“x”和“y”?为什么它们会作为“操作数”而不是“操作数”添加到默认图形中由于x和y显然是两个操作数,而且一个操作还没有被声明?
x
y
只是Python变量名。它们与分配给它们的对象无关。如果您多次分配
x=tf。某些操作
,每次都会重新创建操作。它们不会被“垃圾收集”或者是因为它们现在是计算图的一部分。请注意,如果要为占位符op命名,可以提供一个
name
参数。