Invalid factor level, na generated

704    Asked by AbhinayanVerma in Data Science , Asked on Apr 14, 2021

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

Answered by Abhinayan Verma

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]))))
 
Or
>>> sess = tf.InteractiveSession()
>>> print(type(tf.constant([1,2,3]).eval()))

Or
>>> sess = tf.Session()
>>> with sess.as_default():
>>> print(type(tf.constant([1,2,3]).eval()))

Hope this answer helps you convert tensor to NumPy array.

Your Answer

Interviews

Parent Categories