Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Machine Learning by (47.6k points)

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

1 Answer

0 votes
by (33.1k points)

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.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...