根据documentation TensorFlow将“_1”,“_ 2”等附加到tf.Graph命名空间中的名称,以使其唯一。这里我定义了两个卷积运算。预计第一个将被命名为“conv2d”,第二个将被命名为“conv2d_1”。但是当我尝试获取第二个卷积的名称时,它返回“conv2d_2”。我尝试调用get_tensor_by_name时导致错误。这是代码:
import numpy as np
import tensorflow as tf
import os
x = tf.constant(np.random.randn(1,2,2,1), dtype=tf.float32)
kernel_size = (1,1)
no_of_out = 20
strides = (1,1)
conv_out1 = tf.layers.conv2d(x, 10, (1,1), (1,1))
conv_out2 = tf.layers.conv2d(x, 10, (1,1), (1,1))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print conv_out1.name # conv2d/BiasAdd:0 . This value is correct
print conv_out2.name # conv2d_2/BiasAdd:0 . This value is incorrect. It should be conv2d_1/BiasAdd:0
conv_weights1 = tf.get_default_graph().get_tensor_by_name(os.path.split(conv_out1.name)[0] + '/kernel:0')
conv_weights2 = tf.get_default_graph().get_tensor_by_name('conv2d_1/kernel:0')
conv_weights2 = tf.get_default_graph().get_tensor_by_name(os.path.split(conv_out2.name)[0] + '/kernel:0')
我无法理解为什么conv_out2.name会返回“conv2d_2”而不是“conv2d_1”