python - 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()
。我使用了两个文件名数组的图像路径
解决方案
这里的错误消息有点令人困惑,因为它谈到了跟踪功能,但我遇到了这个并意识到它是一个数据集功能,只有在启用急切执行时才支持。它在 TensorFlow 2.x 中默认启用,但您也可以在以后的 1.x 版本中手动启用它。如果启用它,此错误消息应该会消失。
推荐阅读
- hibernate - @Enumerated(EnumType.STRING) 因@AttributeOverride 而丢失
- javascript - JavaScript 中 new Array() 和 Array() 初始化语法的区别
- c# - 有没有办法在瀑布对话框中接受语音输入
- sql-server - 在 SQL Server 上获取电子邮件域名(无 TLD)
- c# - 如何将类似 C#“单一案例联合类型”的对象序列化为其基础值并返回?
- python - Python 模块 ortools 显然安装不完整
- mysql - 如何使用 MySql 从数据库中的两个表中获取数据
- java - 如何限制 Jenkins 同时运行的 Selenium 浏览器?
- python - Send_keys 函数触发错误消息:'消息:元素不可交互'
- javascript - 尝试添加垂直 div 和其他内容被下推