首页 > 解决方案 > RuntimeError:跟踪函数时不支持 as_numpy_iterator()

问题描述

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

-------------------------------------------------- ------------------------- RuntimeError Traceback (最近一次调用最后一次) in () ----> 1 image = get_image_data(image_paths)

1 帧 /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() 在为 nest.flatten(self.element_spec) 中的 component_spec 跟踪“ 491 “functions”) 492 时不受支持:

RuntimeError:跟踪函数时不支持 as_numpy_iterator()

我的代码是

    # 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()。我使用了两个文件名数组的图像路径

标签: pythonnumpytensorflow

解决方案


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


推荐阅读