AttributeError: 'Tensor' 对象在 Tensorflow 2.1 中没有属性 'numpy' [英] AttributeError: 'Tensor' object has no attribute 'numpy' in Tensorflow 2.1
问题描述
我正在尝试在 Tensorflow 2.1 中转换 Tensor
的 shape
属性,但出现此错误:
I am trying to convert the shape
property of a Tensor
in Tensorflow 2.1 and I get this error:
AttributeError: 'Tensor' object has no attribute 'numpy'
我已经检查了 tf.executing early()
的输出是 True
,
I already checked that the output of tf.executing eagerly()
is True
,
一点上下文:我从 TFRecords 加载 tf.data.Dataset
,然后应用 map
.映射函数正在尝试将数据集示例 Tensor
之一的 shape
属性转换为 numpy:
A bit of context: I load a tf.data.Dataset
from a TFRecords, then I apply a map
. The maping function is trying to convert the shape
property of one of the dataset sample Tensor
to numpy:
def _parse_and_decode(serialized_example):
""" parse and decode each image """
features = tf.io.parse_single_example(
serialized_example,
features={
'encoded_image': tf.io.FixedLenFeature([], tf.string),
'kp_flat': tf.io.VarLenFeature(tf.int64),
'kp_shape': tf.io.FixedLenFeature([3], tf.int64),
}
)
image = tf.io.decode_png(features['encoded_image'], dtype=tf.uint8)
image = tf.cast(image, tf.float32)
kp_shape = features['kp_shape']
kp_flat = tf.sparse.to_dense(features['kp_flat'])
kp = tf.reshape(kp_flat, kp_shape)
return image, kp
def read_tfrecords(records_dir, batch_size=1):
# Read dataset from tfrecords
tfrecords_files = glob.glob(os.path.join(records_dir, '*'))
dataset = tf.data.TFRecordDataset(tfrecords_files)
dataset = dataset.map(_parse_and_decode, num_parallel_calls=batch_size)
return dataset
def transform(img, labels):
img_shape = img.shape # type: <class 'tensorflow.python.framework.ops.Tensor'>`
img_shape = img_shape.numpy() # <-- Throws the error
# ...
dataset = read_tfrecords(records_dir)
这会引发错误:
dataset.map(transform, num_parallel_calls=1)
虽然这非常有效:
for img, labels in dataset.take(1):
print(img.shape.numpy())
尝试访问 img.numpy()
而不是 img.shape.numpy()
会导致相同的行为变压器和代码就在上面.
trying to access the img.numpy()
instead of img.shape.numpy()
results in the same behavior in the tranformer and the codde just above.
我检查了 img_shape
的类型,它是
.
I checked the type of img_shape
and it is <class 'tensorflow.python.framework.ops.Tensor'>
.
有没有人在新版本的 Tensorflow 中解决过这类问题?
Has anyone solved this sort of issue in new versions of Tensorflow?
推荐答案
你的代码中的问题是你不能在映射到 tf.data 的函数中使用
,因为 ..numpy()
.Datasetsnumpy()
是 Python 代码,而不是纯 TensorFlow 代码.
The problem in your code is that you cannot use .numpy()
inside functions that are mapped onto tf.data.Datasets
, because .numpy()
is Python code not pure TensorFlow code.
当您使用像 my_dataset.map(my_function)
这样的函数时,您只能在 my_function
函数中使用 tf.*
函数.
When you use a function like my_dataset.map(my_function)
, you can only use tf.*
functions inside your my_function
function.
这不是 TensorFlow 2.x 版本的错误,而是出于性能目的在幕后如何生成静态图.
This is not a bug of TensorFlow 2.x versions, but rather on how static graphs are generated behind the scenes for performance purposes.
如果您想在映射到数据集的函数中使用自定义 Python 代码,则必须使用 tf.py_function()
,文档:https://www.tensorflow.org/api_docs/python/tf/py_function.在数据集上映射时,真的没有其他方法可以混合 Python 代码和 TensorFlow 代码.
If you want to use custom Python code inside a function which you map on your dataset, you have to use tf.py_function()
, docs: https://www.tensorflow.org/api_docs/python/tf/py_function. There is really no other way to mix Python code and TensorFlow code when mapping on a dataset.
您也可以咨询此问题以获取更多信息;这是我几个月前问的确切问题:是否有用于自定义 Python 代码的 tf.py_function() 替代方法?
You can also consult this question for further information; it's the exact question that I asked a couple of months ago: Is there an alternative to tf.py_function() for custom Python code?
这篇关于AttributeError: 'Tensor' 对象在 Tensorflow 2.1 中没有属性 'numpy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!