简体   繁体   English

张量流张量的命名

[英]Naming of tensorflow tensors

Below is a code snippet which attempts to name the result of a tensor operation so I can access it after the network has been saved and restored 下面是一个代码片段,尝试命名张量操作的结果,以便在网络保存和恢复后可以访问它

   def createForward(self):

    # forward propogation

    Z      = tf.add(tf.matmul(self.W,self.prevLayer.A),self.b)
    self.Z = tf.nn.dropout(Z,self.keepProb,name = self.name+'_Z')
    print(self.name+'_Z',self.Z)

When self.name is 'output' I am expecting the print statement to print out 当self.name为'output'时,我期望打印语句打印出来

output_Z Tensor("output_Z:0", shape=(3, ?), dtype=float32)

What I actually get is 我真正得到的是

output_Z Tensor("output_Z/mul:0", shape=(3, ?), dtype=float32)

Could somebody explain what is happening. 有人可以解释发生了什么。

Thanks 谢谢

I think what you are not familiar with is the naming of the operation in TensorFlow. 我认为您不熟悉TensorFlow中的操作命名。 Let's first see this: 我们首先来看一下:

In [2]: import tensorflow as tf      
In [4]: w = tf.Variable([[1,2,3], [4,5,6], [7,8,9], [3,1,5], [4,1,7]], dtype=tf.float32) 
In [6]: z = tf.nn.dropout(w, 0.4, name="output_Z")
In [7]: z.op.name
Out[7]: u'output_Z/mul' 
In [8]: z.name
Out[8]: u'output_Z/mul:0'

As you can see the name of z differs from the name of the operation z but all of them have the operation name mul appended. 如您所见,z的名称与操作z的名称不同,但是所有名称都附加了操作名称mul

To get what you expected you can do like this: 要获得您期望的结果,您可以这样做:

In [12]: Z = tf.identity(z, name="output_Z")
In [13]: Z.op.name
Out[13]: u'output_Z'
In [14]: Z.name
Out[14]: u'output_Z:0'

Reference : 参考

Names of ops in tf.variable_scope() tf.variable_scope()中的操作名称

We discussed how tf.variable_scope governs the names of variables. 我们讨论了tf.variable_scope如何管理变量的名称。 But how does it influence the names of other ops in the scope? 但是它如何影响范围内其他操作的名称? It is natural that ops created inside a variable scope should also share that name. 在变量范围内创建的操作也应该共享该名称,这是很自然的。 For this reason, when we do with tf.variable_scope("name"), this implicitly opens a tf.name_scope("name"). 因此,当我们使用tf.variable_scope(“ name”)时,将隐式打开一个tf.name_scope(“ name”)。 For example: 例如:

with tf.variable_scope("foo"):
    x = 1.0 + tf.get_variable("v", [1])
assert x.op.name == "foo/add"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM