Tutorial Example

Convert Tensor to Numpy Array – TensorFlow Example

A Tensor can be converted to Numpy data type then you can use numpy function to process this data.

Here we write an example to introduce how to convert. You can follow our example to learn how to do.

Key Tips:

array = your_tensor.eval(session=your_session)

Example:

Convert a tensor to numpy array.

import tensorflow as tf;
import numpy as np

A = tf.constant([[1,2,3],[1,3,3],[4,5,6],[7,8,9]], dtype=tf.float32)

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()
with tf.Session() as sess:
    sess.run([init, init_local])
    np.set_printoptions(precision=4, suppress=True)
   
    AN= A.eval()
    U,S,VT = np.linalg.svd(AN)
    
    print 'S='
    print S
    print 'U='
    print U
    print 'VT='
    print VT

The result is: