Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow - Can't convert Operation to Tensor

Tags:

tensorflow

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?

like image 774
Sean Saito Avatar asked Dec 11 '17 09:12

Sean Saito


1 Answers

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')

like image 68
Sean Saito Avatar answered Oct 24 '22 10:10

Sean Saito