4

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

hamza boulahia
  • 197
  • 1
  • 2
  • 11
  • Does this help? https://stackoverflow.com/questions/49568041/tensorflow-how-do-i-convert-a-eagertensor-into-a-numpy-array – juju89 Oct 17 '20 at 13:58
  • 1
    Well, basically I want to convert to Eagertensor so that I can use .numpy(). – hamza boulahia Oct 17 '20 at 14:10
  • 1
    I have added context to my question, I don't know if that would help. – hamza boulahia Oct 17 '20 at 14:50
  • `EagerTensor`s are implicitly converted to `Tensor`s. More accurately, a new `Tensor` object is created and the values are copied into the new tensor. TF doesn't modify tensor contents at all; it always creates new Tensors. The type of the new tensor depends on if the line creating it is executing in Eager mode. – Susmit Agrawal Oct 17 '20 at 17:54
  • I understand, but then why would the ```.numpy()``` not work? – hamza boulahia Oct 17 '20 at 18:37
  • 1
    Kindly share a reproducible code. –  Oct 20 '20 at 08:58

2 Answers2

2

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.

  • Thanks for the explanation, but I think the error I am getting is due to something else, here is a colab notebook to reproduce the error in similar condition: https://colab.research.google.com/drive/1m1sKw0bkU_sns_tnHqgVPnUUmausucoB?usp=sharing – hamza boulahia Oct 20 '20 at 12:36
  • 1
    @hamza-boulahia - I had look into your code, writing one more answer as a solution for your problem. –  Oct 21 '20 at 17:54
2

Writing one more answer as the same error appears on different scenario.

The error you are getting is because of version issue .i.e. tensorflow version 2.1.0. I ran the code by skipping the first 2 paragraphs that is to install tensorflow==2.1.0 and keras==2.3.1 and the error didn't reappear.

Your issue vanishes in the latest version of the tensorflow version 2.3.0. Run the program on latest versions, that means do not install tensorflow and keras again because Google Colab already has the latest and stable version pre installed.

features.numpy()

Output -

array([[0.       , 0.3728346, 0.       , ..., 1.0103987, 0.       ,
        0.4194043]], dtype=float32)