Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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 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'>


>>> sess = tf.InteractiveSession() 

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

<class 'numpy.ndarray'>


>>> 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.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories