0 votes
1 view
in Machine Learning by (47.8k points)

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

1 Answer

0 votes
by (33.2k 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.

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


Categories

...