Tensorflow: Convert Tensor to numpy array WITHOUT .eval() or sess.run()

13,691

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.

Share:
13,691
araman
Author by

araman

Updated on July 01, 2022

Comments

  • araman
    araman almost 2 years

    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.

  • Bruce
    Bruce about 5 years
    its should be a comment instead of answering, because the user has already accepted the answer
  • Syed Alam Abbas
    Syed Alam Abbas about 5 years
    .numpy() works only for eager tensor not normal tensor.