9

I have created an OP in tensorflow where for some processing I need my data to be converted from tensor object to numpy array. I know we can use tf.eval() or sess.run to evaluate any tensor object. What I really want to know is, Is there any way to convert tensor to array without any session running, so in turn we avoid use of .eval() or .run().

Any help is highly appreciated!

def tensor_to_array(tensor1):
    '''Convert tensor object to numpy array'''
    array1 = SESS.run(tensor1) **#====== need to bypass this line**
    return array1.astype("uint8")

def array_to_tensor(array):
    '''Convert numpy array to tensor object'''
    tensor_data = tf.convert_to_tensor(array, dtype=tf.float32)
    return tensor_data
3
  • Unlikely in the general case, since you don't know the answers without running the pipeline Commented Sep 7, 2018 at 5:19
  • There must be some way out , right? Commented Sep 7, 2018 at 6:08
  • @MadPhysicist I don't think you are making a correct statement. Commented Sep 7, 2018 at 6:10

1 Answer 1

6

Updated

# must under eagar mode
def tensor_to_array(tensor1):
    return tensor1.numpy()

example

>>> import tensorflow as tf
>>> tf.enable_eager_execution()
>>> def tensor_to_array(tensor1):
...     return tensor1.numpy()
...
>>> x = tf.constant([1,2,3,4])
>>> tensor_to_array(x)
array([1, 2, 3, 4], dtype=int32)

I believe you can do it without tf.eval() or sess.run by using tf.enable_eager_execution()

example

import tensorflow as tf
import numpy as np
tf.enable_eager_execution()
x = np.array([1,2,3,4])
c = tf.constant([4,3,2,1])
c+x
<tf.Tensor: id=5, shape=(4,), dtype=int32, numpy=array([5, 5, 5, 5], dtype=int32)>

For more details about tensorflow eager mode, checkout here:Tensorflow eager

If without tf.enable_eager_execution():

import tensorflow as tf
import numpy as np
c = tf.constant([4,3,2,1])
x = np.array([1,2,3,4])
c+x
<tf.Tensor 'add:0' shape=(4,) dtype=int32>
Sign up to request clarification or add additional context in comments.

15 Comments

Hi...eager_execution looks interesing, but I don't think it serves my purpose. Please have a look at below snippet.
Can you provide the details of the tensor object that you would like to parse, please?
def tensor_to_array(tensor1): '''Convert tensor object to numpy array''' array1 = SESS.run(tensor1) #== need to replace this line === return array1.astype("uint8") def array_to_tensor(array): '''Convert numpy array to tensor object''' tensor_data = tf.convert_to_tensor(array, dtype=tf.float32) return tensor_data
Thanks @R.yan ...Also refer this link for further information. towardsdatascience.com/eager-execution-tensorflow-8042128ca7be
I'm still facing an issue. If I run all the commands from anaconda prompt, I'm getting the desired output. But when I try to run it as script in spyder, it throws an error of "AttributeError: 'Tensor' object has no attribute 'numpy'". Do you have any idea why so?? I have made sure that spyder runs from same environemnt but the problem still persists. @R.yan
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.