Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a Tensor to Eager tensor in Tensorflow 2.1.0?

I've been trying to convert a tensor of type:

tensorflow.python.framework.ops.Tensor

to an eagertensor:

<class 'tensorflow.python.framework.ops.EagerTensor'>

I've been searching for a solution but couldn't find one. Any help would be appreciated.

Context:

I have obtained the tensor using the feature extraction method from a Keras Sequential model. The output was a tensor of the first mentioned type. However, when I tried to convert it to numpy using .numpy(), it did not work with the following error:

'Tensor' object has no attribute 'numpy'

But then when I try creating a tensor using tf.constant and then using .numpy() to convert it, it works fine!

The only difference I found is that the types of tensors are different: The tensor generated by Keras sequential is of the first type mentionned above, whereas the second tensor that I have created manually is of the second type (Eager tensor).

like image 501
Varazda Avatar asked Sep 13 '25 18:09

Varazda


1 Answers

Could have answered better if you would have shared the reproducible code.

Below is a simple scenario where I have recreated your error. Here I am reading the path of a image file.

Code to recreate the error:

%tensorflow_version 2.x
import tensorflow as tf
import numpy as np

def get_path(file_path):
    print("file_path: ", bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
    return file_path

train_dataset = tf.data.Dataset.list_files('/content/bird.png')
train_dataset = train_dataset.map(lambda x: (get_path(x)))

for one_element in train_dataset:
    print(one_element)

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-2d5db8425f67> in <module>()
      8 
      9 train_dataset = tf.data.Dataset.list_files('/content/bird.png')
---> 10 train_dataset = train_dataset.map(lambda x: (get_path(x)))
     11 
     12 for one_element in train_dataset:

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    256       except Exception as e:  # pylint:disable=broad-except
    257         if hasattr(e, 'ag_error_metadata'):
--> 258           raise e.ag_error_metadata.to_exception(e)
    259         else:
    260           raise

AttributeError: in user code:

    <ipython-input-8-2d5db8425f67>:10 None  *
        train_dataset = train_dataset.map(lambda x: (get_path(x)))
    <ipython-input-8-2d5db8425f67>:6 get_path  *
        print("file_path: ", bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))

    AttributeError: 'Tensor' object has no attribute 'numpy'

Below are the steps I have implemented in the code to fix this error.

  1. Have decorated the map function with tf.py_function(get_path, [x], [tf.string]). You can find more about tf.py_function here.
  2. Now I can get the string part by using bytes.decode(file_path.numpy()) in map function.

Fixed Code:

%tensorflow_version 2.x
import tensorflow as tf
import numpy as np

def get_path(file_path):
    print("file_path: ",bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
    return file_path

train_dataset = tf.data.Dataset.list_files('/content/bird.jpg')
train_dataset = train_dataset.map(lambda x: tf.py_function(get_path, [x], [tf.string]))

for one_element in train_dataset:
    print(one_element)

Output:

file_path:  /content/bird.jpg <class 'str'>
(<tf.Tensor: shape=(), dtype=string, numpy=b'/content/bird.jpg'>,)

Hope this answers your question.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!