How to convert a tensor into a numpy array when using Tensorflow with Python bindings?

0 votes

For your problem, Tensor returned by Session.run() or tf.eval() is already a NumPy array, except for Sparse tensor, they return Sparse value.

**For example:**

>>> print(type(tf.Session().run(tf.constant([1,2,3]))))

<class 'numpy.ndarray'>

Or

>>> sess = tf.InteractiveSession()

>>> print(type(tf.constant([1,2,3]).eval()))

<class 'numpy.ndarray'>

Or

>>> sess = tf.Session()

>>> with sess.as_default():

>>> print(type(tf.constant([1,2,3]).eval()))

<class 'numpy.ndarray'>

Hope this answer helps.

If you wish to learn more about Python, visit Python tutorial and Python course by Intellipaat.