"Cannot convert a ndarray into a Tensor or Operation." error when trying to fetch a value from session.run in tensorflow
It looks like you overwrite the Tensor distance = tf.sqrt(...)
with a numpy array distance = sess.run(distance)
.
Your loop is the culprit. Change t_state, distance = sess.run([question1_final_state, distance]
to something like t_state, distance_other = sess.run([question1_final_state, distance]
Related videos on Youtube
Mithun
My linkedin profile: https://www.linkedin.com/in/mithungooty
Updated on September 11, 2022Comments
-
Mithun over 1 year
I have created a siamese network in tensorflow. I am calculating the distance between two tensors using the below code:
distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(question1_predictions, question2_predictions)), reduction_indices=1))
I am able to train the model without any errors. In the inference section, I am retrieving the
distance
tensor as below:test_state, distance = sess.run([question1_final_state, distance], feed_dict=feed)
Tensorflow is throwing an error:
Fetch argument array([....], dtype=float32) has invalid type , must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)
When I print the
distance
tensor, before and after thesession.run
in the training section, it shows as<class 'tensorflow.python.framework.ops.Tensor'>
. So the replacement of tensordistance
with numpydistance
is happening in thesession.run
of inference section. Following the code from the inference section:with graph.as_default(): saver = tf.train.Saver() with tf.Session(graph=graph) as sess: sess.run(tf.global_variables_initializer(), feed_dict={embedding_placeholder: embedding_matrix}) saver.restore(sess, tf.train.latest_checkpoint('checkpoints')) test_state = sess.run(initial_state) for ii, (x1, x2, batch_test_ids) in enumerate(get_test_batches(test_question1_features, test_question2_features, test_ids, batch_size), 1): feed = {question1_inputs: x1, question2_inputs: x2, keep_prob: 1, initial_state: test_state } test_state, distance = sess.run([question1_final_state, distance], feed_dict=feed)
-
Mithun almost 7 yearsYes, that's the reason, but I am not able to figure out the place/operation in the graph that is doing it. I am updating my question with some more details, please take a look. Thank you.
-
Salvador Dali almost 7 years@Mithun the problem is with your loop. I added a few things to my answer