运行时错误:跟踪函数时不支持 as_numpy_iterator() [英] RuntimeError: as_numpy_iterator() is not supported while tracing functions

查看:150
本文介绍了运行时错误:跟踪函数时不支持 as_numpy_iterator()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

<块引用>

--------------------------------------------------------------------------- RuntimeError Traceback(最近一次调用最后) 在 ()----> 1 个图像 = get_image_data(image_paths)

1 帧/tensorflow-2.1.0/python3.6/tensorflow_core/python/data/ops/dataset_ops.py在 as_numpy_iterator(self)第488章第489话--> 490 raise RuntimeError("as_numpy_iterator() is not supported while tracking "第491话第 492 章

运行时错误:跟踪时不支持 as_numpy_iterator()功能

我的代码是

 # 创建一个名为 get_dataset 的函数,该函数从文件路径创建图像数据的数据集.def get_dataset(image_paths):filename_tensor = tf.constant(image_paths)数据集 = tf.data.Dataset.from_tensor_slices(filename_tensor)def_map_fn(文件名):返回解码图像(文件名=文件名)返回 dataset.map(_map_fn)#def get_image_data(image_paths):数据集 = get_dataset(image_paths)返回列表(dataset.as_numpy_iterator())图像 = get_image_data(image_paths)

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

解决方案

这里的错误信息有点令人困惑,因为它谈到了跟踪功能,但我遇到了这个并意识到这是一个 Dataset 功能,仅在 Eager Execution 时支持已启用.它在 TensorFlow 2.x 中默认启用,但您也可以在以后的 1.x 版本中手动启用它.如果启用它,此错误消息应该会消失.

while i was using function as_numpy_iterator() got error

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

my code is

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

it throws error in using dataset.as_numpy_iterator() . I had used image paths of two array of filename

解决方案

The error message here is a bit confusing, since it talks about tracing functions, but I ran into this and realized it's a Dataset feature that's only supported when eager execution is enabled. It's enabled by default in TensorFlow 2.x, but but you can also manually enable it in later 1.x versions. If you enable it, this error message should disappear.

这篇关于运行时错误:跟踪函数时不支持 as_numpy_iterator()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆