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.
- Have decorated the map function with
tf.py_function(get_path, [x], [tf.string])
. You can find more about tf.py_function here.
- 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.