Tensorflow - 无法将操作转换为张量 [英] Tensorflow - Can't convert Operation to Tensor
问题描述
我想计算操作和张量的输出之间的成对欧几里得距离.我正在使用建议的代码 这里.这是我的代码的要点:
I would like to compute the pairwise Euclidean distance between the output of an Operation and a Tensor. I'm using code suggested here. Here's the gist of my code:
# 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))
但是我收到以下错误:
TypeError: Can't convert Operation '.../Tanh' to Tensor (target dtype=None, name=u'x', as_ref=False)
对于这一行:
p1 = tf.matmul(tf.expand_dims(tf.reduce_sum(tf.square(logits), 1), 1),
tf.ones(shape=(1, m2)))
我该如何解决这个问题?
How should I fix this?
推荐答案
通过调用 tf.get_default_graph().get_operation_by_name
,我得到了计算 tanh 激活的操作.但我需要的是该操作的输出,我可以通过调用 tf.get_default_graph().get_tensor_by_name
找到它.
By calling tf.get_default_graph().get_operation_by_name
, I was getting the ops that calculated the tanh activation. But what I need instead is the output of that operation, which I can find by calling tf.get_default_graph().get_tensor_by_name
.
因此修复是用
logits = tf.get_default_graph().get_tensor_by_name('Tanh:0')
这篇关于Tensorflow - 无法将操作转换为张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!