为张量指定名称?

Cla*_*diu 7 python tensorflow

在创建张量后,有没有办法为张量赋值?

我正在循环中逐位构建神经网络,如下所示:

    def build_logit_pipeline(data):
        # X --> *W1 --> +b1 --> relu --> *W2 --> +b2 ... --> softmax etc...
        pipeline = data

        for i in xrange(len(layer_sizes) - 1):
            with tf.name_scope("linear%d" % i):
                pipeline = tf.matmul(pipeline, weights[i])
                pipeline = tf.add(pipeline, biases[i])

            if i != len(layer_sizes) - 2:
                with tf.name_scope("relu%d" % i):
                    pipeline = tf.nn.relu(pipeline)

        return pipeline
Run Code Online (Sandbox Code Playgroud)

我想为整个操作的结果指定一个名称,即最后一个tf.add应该分配一个名称.有没有办法pipeline在返回之前对变量执行此操作,而不是在最后一次添加时检查循环条件的结束,哪个不太优雅?

Oli*_*rot 18

您无法修改Tensor的名称.

但是,一个简单的技巧就是使用tf.identity您想要的名称:

res = build_logit_pipeline(data)
res = tf.identity(res, name="your_name")
Run Code Online (Sandbox Code Playgroud)

  • 该名称设置为“tf.identity”而不是“your_name”。我做错什么了吗? (2认同)
  • 这个答案是为图形模式下的张量流 1 编写的。在急切执行中,张量没有名称。 (2认同)