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 the session.run
in the training section, it shows as <class 'tensorflow.python.framework.ops.Tensor'>
. So the replacement of tensor distance
with numpy distance
is happening in the session.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)
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]
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