Reputation: 103
How can you convert a tensor into a Numpy ndarray, without using eval or sess.run()?
I need to pass a tensor into a feed dictionary and I already have a session running.
Upvotes: 8
Views: 14018
Reputation: 3232
@jasekp answer helped me a lot. I have faced the tensor->ndarray conversion in the specific case of tensors representing (adversarial) images.
I think that my question/answer (here) may be an helpful example for the specific case or may help newbies to better understand @jasekp answer.
My example also covers the matplotlib
image visualization part, but this is OT.
Upvotes: 0
Reputation: 1010
The fact that you say "already have a session running" implies a misunderstanding of what sess.run() actually does.
If you have a tf.Session() initiated, you should be able to use it to retrieve any tensor using sess.run(). If you need to retrieve a variable or constant tensor this is very straight forward.
value = sess.run(tensor_to_retrieve)
If the tensor is the result of operations on placeholder tensors, you will need to pass them in with feed_dict.
value = sess.run(tensor, feed_dict={input_placeholder: input_value})
There is nothing preventing you from calling sess.run() more than once.
Upvotes: 5