张量流张量的命名

2024-09-27 18:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一个代码片段,它试图命名张量操作的结果 所以我可以在网络被保存和恢复后访问它

   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)

什么时候姓名是'output'我希望打印语句打印出来

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

我真正得到的是

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

有人能解释一下发生了什么事吗。你知道吗

谢谢


Tags: 代码nameself网络outputtfdef命名
1条回答
网友
1楼 · 发布于 2024-09-27 18:24:22

我认为您不熟悉的是TensorFlow中操作的命名。让我们先看看这个:

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'

如您所见,z的名称与操作z的名称不同,但它们都附加了操作名称mul。你知道吗

为了得到你所期望的,你可以这样做:

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()

We discussed how tf.variable_scope governs the names of variables. 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"). For example:

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

相关问题 更多 >

    热门问题