Tensorflow-无法将操作转换为Tensor

Sea*_*ito 5 tensorflow

我想计算操作输出和张量之间的成对欧几里得距离。我正在使用这里建议的代码。这是我的代码的要点:

    # Suppose logits has shape [32, 128]
    logits = tf.get_default_graph().get_operation_by_name('Tanh')
    y = tf.placeholder(tf.float32, shape=[10, 128])

    m1, m2, k = 32, tf.shape(y)[0], latent_dim

    # Get the pairwise distances
    p1 = tf.matmul(tf.expand_dims(tf.reduce_sum(tf.square(logits), 1), 1),
                  tf.ones(shape=(1, m2)))
    p2 = tf.transpose(tf.matmul(
        tf.reshape(tf.reduce_sum(tf.square(y), 1), shape=[-1, 1]),
        tf.ones(shape=(m1, 1)),
        transpose_b=True
    ))
    distance_predictions = tf.sqrt(tf.add(p1, p2) - 2 * 
         tf.matmul(logits, y, transpose_b=True))        
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

TypeError: Can't convert Operation '.../Tanh' to Tensor (target dtype=None, name=u'x', as_ref=False)
Run Code Online (Sandbox Code Playgroud)

对于此行:

p1 = tf.matmul(tf.expand_dims(tf.reduce_sum(tf.square(logits), 1), 1),
              tf.ones(shape=(1, m2)))
Run Code Online (Sandbox Code Playgroud)

我该如何解决?

Sea*_*ito 5

通过调用tf.get_default_graph().get_operation_by_name,我得到了计算tanh激活的操作。但是,我需要的是该操作的输出,可以通过调用来找到tf.get_default_graph().get_tensor_by_name

因此,解决方法是将第一行替换为

logits = tf.get_default_graph().get_tensor_by_name('Tanh:0')