python - 运行时错误: as_numpy_iterator() is not supported while tracing functions

标签 python numpy tensorflow

当我使用函数as_numpy_iterator()时出现错误

--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) in () ----> 1 image = get_image_data(image_paths)

1 frames /tensorflow-2.1.0/python3.6/tensorflow_core/python/data/ops/dataset_ops.py in as_numpy_iterator(self) 488 """ 489 if not context.executing_eagerly(): --> 490 raise RuntimeError("as_numpy_iterator() is not supported while tracing " 491 "functions") 492 for component_spec in nest.flatten(self.element_spec):

RuntimeError: as_numpy_iterator() is not supported while tracing functions

我的代码是

    # creating a function called get_dataset, which creates a dataset of image data from file paths.
def get_dataset(image_paths):
  filename_tensor = tf.constant(image_paths)
  dataset = tf.data.Dataset.from_tensor_slices(filename_tensor)
  def _map_fn(filename):
    return decode_image(filename=filename)
  return dataset.map(_map_fn)
#
def get_image_data(image_paths):
  dataset = get_dataset(image_paths)
  return list(dataset.as_numpy_iterator())
image = get_image_data(image_paths)

使用 dataset.as_numpy_iterator() 时会抛出错误。我使用了两个文件名数组的图像路径

最佳答案

这里的错误消息有点令人困惑,因为它讨论了跟踪函数,但我遇到了这个问题并意识到这是一个数据集功能,仅在启用急切执行时才受支持。它在 TensorFlow 2.x 中默认启用,但您也可以在更高版本的 1.x 中手动启用它。如果启用它,此错误消息应该消失。

关于python - 运行时错误: as_numpy_iterator() is not supported while tracing functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60045971/

相关文章:

javascript - 在 python 中实现 SJCL .frombits

python - Django 在 cookie 中没有 sessionid

python - 如何根据匹配日期连接两个数据框?

python - NumPy 中矩阵列的 nanmean

python - TensorFlow 中矩阵和向量的高效逐元素乘法

python - ctypes - 从 C 到 python 对象的引用

python - Arnaud Legoux 移动平均线和 numpy

python - 通过沿对角线复制给定数组在 numpy 中创建矩阵的有效方法

Tensorflow 编译加速 CPU

python - key 错误 : "Invalid split train[:80%]. Available splits are: [' train']"