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))
However I get the following error:
TypeError: Can't convert Operation '.../Tanh' to Tensor (target dtype=None, name=u'x', as_ref=False)
For this line:
p1 = tf.matmul(tf.expand_dims(tf.reduce_sum(tf.square(logits), 1), 1),
tf.ones(shape=(1, m2)))
How should I fix this?
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
.
Hence the fix is to replace the first line with
logits = tf.get_default_graph().get_tensor_by_name('Tanh:0')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With